From ebe8a78c7f244bc9edcad543b4e5673eb1237a5b Mon Sep 17 00:00:00 2001 From: Eric Doughty-Papassideris Date: Mon, 23 Sep 2024 02:56:15 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20oo-connector:=20use=20driv?= =?UTF-8?q?e=5Ffile=5Fid=20as=20much=20as=20possible=20in=20OO=20flow=20(#?= =?UTF-8?q?525)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/browser-editor.controller.ts | 4 +++- .../src/controllers/onlyoffice.controller.ts | 17 +++++++++++++++-- .../src/interfaces/editor.interface.ts | 3 ++- .../onlyoffice-connector/src/routes/index.ts | 10 +++++++++- .../src/services/editor.service.ts | 4 ++-- .../onlyoffice-connector/src/views/index.eta | 9 +++++---- 6 files changed, 36 insertions(+), 11 deletions(-) diff --git a/tdrive/connectors/onlyoffice-connector/src/controllers/browser-editor.controller.ts b/tdrive/connectors/onlyoffice-connector/src/controllers/browser-editor.controller.ts index 7b912612..d2c82065 100644 --- a/tdrive/connectors/onlyoffice-connector/src/controllers/browser-editor.controller.ts +++ b/tdrive/connectors/onlyoffice-connector/src/controllers/browser-editor.controller.ts @@ -22,6 +22,7 @@ interface RequestEditorQuery { office_token: string; company_id: string; file_id: string; + drive_file_id: string; } /** @@ -102,6 +103,7 @@ class BrowserEditorController { makeURLTo.editorAbsolute({ token, file_id, + drive_file_id, editing_session_key: editingSessionKey, company_id, preview, @@ -131,7 +133,7 @@ class BrowserEditorController { throw new Error('Cant start editing without "editing session key"'); } - const initResponse = await editorService.init(company_id, file_name, file_id, user, preview, drive_file_id || file_id); + const initResponse = await editorService.init(company_id, file_name, file_id, user, preview, drive_file_id); const inPageToken = jwt.sign( { diff --git a/tdrive/connectors/onlyoffice-connector/src/controllers/onlyoffice.controller.ts b/tdrive/connectors/onlyoffice-connector/src/controllers/onlyoffice.controller.ts index a5186f88..1f94d87f 100644 --- a/tdrive/connectors/onlyoffice-connector/src/controllers/onlyoffice.controller.ts +++ b/tdrive/connectors/onlyoffice-connector/src/controllers/onlyoffice.controller.ts @@ -11,6 +11,7 @@ import * as Utils from '@/utils'; interface RequestQuery { company_id: string; file_id: string; + drive_file_id: string; token: string; } @@ -29,15 +30,27 @@ class OnlyOfficeController { const { token } = req.query; const officeTokenPayload = jwt.verify(token, CREDENTIALS_SECRET) as OfficeToken; - const { company_id, file_id, in_page_token } = officeTokenPayload; + const { company_id, file_id, drive_file_id, in_page_token } = officeTokenPayload; // check token is an in_page_token if (!in_page_token) throw new Error('Invalid token, must be a in_page_token'); + let fileId = file_id; + if (drive_file_id) { + //Get the drive file + const driveFile = await driveService.get({ + company_id, + drive_file_id, + }); + if (driveFile) { + fileId = driveFile?.item?.last_version_cache?.file_metadata?.external_id; + } + } + if (!file_id) throw new Error(`File id is missing in the last version cache for ${JSON.stringify(file_id)}`); const file = await fileService.download({ company_id, - file_id: file_id, + file_id: fileId, }); file.pipe(res); diff --git a/tdrive/connectors/onlyoffice-connector/src/interfaces/editor.interface.ts b/tdrive/connectors/onlyoffice-connector/src/interfaces/editor.interface.ts index 7aef5de3..c23aedb4 100644 --- a/tdrive/connectors/onlyoffice-connector/src/interfaces/editor.interface.ts +++ b/tdrive/connectors/onlyoffice-connector/src/interfaces/editor.interface.ts @@ -17,7 +17,7 @@ export type EditConfigInitResult = { onlyoffice_server: string; color: string; company_id: string; - file_id: string; + drive_file_id: string; file_version_id: string; filename: string; file_type: string; @@ -33,6 +33,7 @@ export interface IEditorService { user: UserType, preview: boolean, file_id: string, + drive_file_id: string, ) => Promise; } diff --git a/tdrive/connectors/onlyoffice-connector/src/routes/index.ts b/tdrive/connectors/onlyoffice-connector/src/routes/index.ts index 930c6e12..c930d71a 100644 --- a/tdrive/connectors/onlyoffice-connector/src/routes/index.ts +++ b/tdrive/connectors/onlyoffice-connector/src/routes/index.ts @@ -41,7 +41,15 @@ export function mountRoutes(app: Application) { export const makeURLTo = { rootAbsolute: () => Utils.joinURL([SERVER_ORIGIN, SERVER_PREFIX]), assets: () => Utils.joinURL([SERVER_PREFIX, 'assets']), - editorAbsolute(params: { token: string; file_id: string; editing_session_key: string; company_id: string; preview: string; office_token: string }) { + editorAbsolute(params: { + token: string; + drive_file_id: string; + file_id: string; + editing_session_key: string; + company_id: string; + preview: string; + office_token: string; + }) { return Utils.joinURL([SERVER_ORIGIN ?? '', SERVER_PREFIX, 'editor'], params); }, }; diff --git a/tdrive/connectors/onlyoffice-connector/src/services/editor.service.ts b/tdrive/connectors/onlyoffice-connector/src/services/editor.service.ts index 5cd82f71..12794a0a 100644 --- a/tdrive/connectors/onlyoffice-connector/src/services/editor.service.ts +++ b/tdrive/connectors/onlyoffice-connector/src/services/editor.service.ts @@ -10,7 +10,7 @@ class EditorService implements IEditorService { file_version_id: string, user: UserType, preview: boolean, - file_id: string, + drive_file_id: string, ): Promise => { const { color, mode: fileMode } = this.getFileMode(file_name); let [, extension] = Utils.splitFilename(file_name); @@ -18,7 +18,7 @@ class EditorService implements IEditorService { extension = extension.toLocaleLowerCase(); return { color, - file_id, + drive_file_id, file_version_id, file_type: extension, filename: file_name, diff --git a/tdrive/connectors/onlyoffice-connector/src/views/index.eta b/tdrive/connectors/onlyoffice-connector/src/views/index.eta index 6d9b9ec0..8cb20ad1 100644 --- a/tdrive/connectors/onlyoffice-connector/src/views/index.eta +++ b/tdrive/connectors/onlyoffice-connector/src/views/index.eta @@ -22,12 +22,13 @@ $('#onlyoffice_container').html("
"); + const callbackQueryString = '?drive_file_id=<%= it.drive_file_id %>&company_id=<%= it.company_id %>&token=<%= it.token %>'; let doc = { title: "<%= it.filename %>", - url: `${window.baseURL}read?file_id=<%= it.file_id %>&company_id=<%= it.company_id %>&token=<%= it.token %>`, + url: `${window.baseURL}read${callbackQueryString}`, fileType: "<%= it.file_type %>", key: "<%= it.docId %>", - token: "<%= it.file_id %>", + token: "<%= it.drive_file_id %>", permissions: { download: true, edit: <%= it.editable %>, @@ -41,10 +42,10 @@ height: '100%', documentType: window.mode, document: doc, - token: "<%= it.file_id %>", + token: "<%= it.drive_file_id %>", type: screen.width < 600 ? 'mobile' : 'desktop', editorConfig: { - callbackUrl: `${window.baseURL}callback?file_id=<%= it.file_id %>&company_id=<%= it.company_id %>&token=<%= it.token %>`, + callbackUrl: `${window.baseURL}callback${callbackQueryString}`, lang: window.user.language, user: { id: window.user.id,