Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

feat(files): optimize document opening to avoid extra API calls

johavenbf57d93dcaea
diff --git a/backend/src/applications/files/constants/routes.ts b/backend/src/applications/files/constants/routes.ts
index 492a5e98..549f8f02 100644
--- a/backend/src/applications/files/constants/routes.ts
+++ b/backend/src/applications/files/constants/routes.ts
@@ -18,7 +18,8 @@ export const FILES_ROUTE = {
ONLY_OFFICE: 'onlyoffice',
ONLY_OFFICE_SETTINGS: 'settings',
ONLY_OFFICE_DOCUMENT: 'document',
- ONLY_OFFICE_CALLBACK: 'callback'
+ ONLY_OFFICE_CALLBACK: 'callback',
+ ONLY_OFFICE_STATUS: 'status'
} as const
export const API_FILES_OPERATION = `${FILES_ROUTE.BASE}/${FILES_ROUTE.OPERATION}`
@@ -37,3 +38,4 @@ export const API_FILES_ONLY_OFFICE = `${FILES_ROUTE.BASE}/${FILES_ROUTE.ONLY_OFF
export const API_FILES_ONLY_OFFICE_SETTINGS = `${API_FILES_ONLY_OFFICE}/${FILES_ROUTE.ONLY_OFFICE_SETTINGS}`
export const API_FILES_ONLY_OFFICE_DOCUMENT = `${API_FILES_ONLY_OFFICE}/${FILES_ROUTE.ONLY_OFFICE_DOCUMENT}`
export const API_FILES_ONLY_OFFICE_CALLBACK = `${API_FILES_ONLY_OFFICE}/${FILES_ROUTE.ONLY_OFFICE_CALLBACK}`
+export const API_FILES_ONLY_OFFICE_STATUS = `${API_FILES_ONLY_OFFICE}/${FILES_ROUTE.ONLY_OFFICE_STATUS}`
diff --git a/backend/src/applications/files/files-only-office.controller.ts b/backend/src/applications/files/files-only-office.controller.ts
index 4ecb37d9..954777b4 100644
--- a/backend/src/applications/files/files-only-office.controller.ts
+++ b/backend/src/applications/files/files-only-office.controller.ts
@@ -7,6 +7,7 @@
import { Body, Controller, Get, HttpCode, HttpStatus, Post, Query, Request, Res, StreamableFile, UseInterceptors } from '@nestjs/common'
import { FastifyReply } from 'fastify'
import { ContextInterceptor } from '../../infrastructure/context/interceptors/context.interceptor'
+import { SkipSpaceGuard } from '../spaces/decorators/space-skip-guard.decorator'
import { GetSpace } from '../spaces/decorators/space.decorator'
import { FastifySpaceRequest } from '../spaces/interfaces/space-request.interface'
import { SpaceEnv } from '../spaces/models/space-env.model'
@@ -47,4 +48,10 @@ export class FilesOnlyOfficeController {
onlyOfficeCallBack(@GetUser() user: UserModel, @GetSpace() space: SpaceEnv, @Body('token') token: string, @Query('fid') fileId: string) {
return this.filesOnlyOfficeManager.callBack(user, space, token, fileId)
}
+
+ @Get(FILES_ROUTE.ONLY_OFFICE_STATUS)
+ @SkipSpaceGuard()
+ onlyOfficeStatus(): { enabled: boolean } {
+ return this.filesOnlyOfficeManager.getStatus()
+ }
}
diff --git a/backend/src/applications/files/services/files-only-office-manager.service.ts b/backend/src/applications/files/services/files-only-office-manager.service.ts
index ae714285..dace0923 100644
--- a/backend/src/applications/files/services/files-only-office-manager.service.ts
+++ b/backend/src/applications/files/services/files-only-office-manager.service.ts
@@ -60,6 +60,10 @@ export class FilesOnlyOfficeManager {
private readonly filesQueries: FilesQueries
) {}
+ getStatus(): { enabled: boolean } {
+ return { enabled: configuration.applications.files.onlyoffice.enabled }
+ }
+
async getSettings(user: UserModel, space: SpaceEnv, mode: 'edit' | 'view', req: FastifySpaceRequest): Promise<OnlyOfficeReqConfig> {
if (!(await isPathExists(space.realPath))) {
throw new HttpException('Document not found', HttpStatus.NOT_FOUND)
diff --git a/frontend/src/app/applications/files/components/dialogs/files-viewer-dialog.component.html b/frontend/src/app/applications/files/components/dialogs/files-viewer-dialog.component.html
index 619fc542..79085b27 100644
--- a/frontend/src/app/applications/files/components/dialogs/files-viewer-dialog.component.html
+++ b/frontend/src/app/applications/files/components/dialogs/files-viewer-dialog.component.html
@@ -12,9 +12,9 @@
<button (click)="onMinimize()" aria-label="Minimize" class="btn-minimize btn-minimize-white" type="button"></button>
<button (click)="onClose()" aria-label="Close" class="btn-close btn-close-white ms-3" type="button"></button>
</div>
-@if (canAccess && currentFile) {
+@if (currentFile) {
<div class="modal-body">
- @switch (currentFile.shortMime) {
+ @switch (shortMime) {
@case ('image') {
<div class="d-flex justify-content-center bg-black" [style.height.px]="currentHeight">
<img [src]="currentFile.dataUrl" [style.max-height.px]="currentHeight" alt="" class="img-fluid align-self-center">
@@ -29,9 +29,6 @@
@case ('document') {
<app-files-viewer-document [currentHeight]="currentHeight" [file]="currentFile" [mode]="mode"></app-files-viewer-document>
}
- @case ('html') {
- <app-files-viewer-html [currentHeight]="currentHeight" [fileUrl]="currentFile.dataUrl"></app-files-viewer-html>
- }
@default {
<app-files-viewer-text [currentHeight]="currentHeight" [file]="currentFile"></app-files-viewer-text>
}
diff --git a/frontend/src/app/applications/files/components/dialogs/files-viewer-dialog.component.ts b/frontend/src/app/applications/files/components/dialogs/files-viewer-dialog.component.ts
index 97a7f382..1973c661 100644
--- a/frontend/src/app/applications/files/components/dialogs/files-viewer-dialog.component.ts
+++ b/frontend/src/app/applications/files/components/dialogs/files-viewer-dialog.component.ts
@@ -4,63 +4,35 @@
* See the LICENSE file for licensing details
*/
-import { HttpClient, HttpErrorResponse } from '@angular/common/http'
import { Component, inject, Input, OnDestroy, OnInit } from '@angular/core'
import { Subscription } from 'rxjs'
import { LayoutService } from '../../../../layout/layout.service'
import { FileModel } from '../../models/file.model'
import { FilesViewerDocumentComponent } from '../viewers/files-viewer-document.component'
-import { FilesViewerHtmlComponent } from '../viewers/files-viewer-html.component'
import { FilesViewerMediaComponent } from '../viewers/files-viewer-media.component'
import { FilesViewerPdfComponent } from '../viewers/files-viewer-pdf.component'
import { FilesViewerTextComponent } from '../viewers/files-viewer-text.component'
@Component({
selector: 'app-files-viewer-dialog',
- imports: [FilesViewerPdfComponent, FilesViewerMediaComponent, FilesViewerHtmlComponent, FilesViewerTextComponent, FilesViewerDocumentComponent],
+ imports: [FilesViewerPdfComponent, FilesViewerMediaComponent, FilesViewerTextComponent, FilesViewerDocumentComponent],
templateUrl: 'files-viewer-dialog.component.html'
})
export class FilesViewerDialogComponent implements OnInit, OnDestroy {
- @Input() currentFile: FileModel
- @Input() mode: 'view' | 'edit' = 'view'
- protected canAccess = false
+ @Input({ required: true }) currentFile: FileModel
+ @Input({ required: true }) mode: 'view' | 'edit'
+ @Input({ required: true }) shortMime: string
protected currentHeight: number
- private readonly http = inject(HttpClient)
private readonly layout = inject(LayoutService)
- private subscription: Subscription = null
+ private readonly subscription: Subscription = this.layout.resizeEvent.subscribe(() => this.onResize())
private readonly offsetTop = 42
- private hookShortMime = null
ngOnInit() {
- this.subscription = this.layout.resizeEvent.subscribe(() => this.onResize())
- this.http.head(this.currentFile.dataUrl).subscribe({
- next: () => {
- if (this.mode === 'view' && this.currentFile.shortMime === 'document' && this.currentFile.mime === 'text-plain') {
- this.hookShortMime = this.currentFile.shortMime
- this.currentFile.shortMime = null
- } else if (this.mode === 'view' && this.currentFile.shortMime === 'text' && this.currentFile.mime === 'text-html') {
- this.hookShortMime = this.currentFile.shortMime
- this.currentFile.shortMime = 'html'
- } else if (this.mode === 'edit' && this.currentFile.shortMime === 'pdf') {
- this.hookShortMime = this.currentFile.shortMime
- this.currentFile.shortMime = 'document'
- }
- this.onResize()
- this.canAccess = true
- },
- error: (err: HttpErrorResponse) => {
- console.error(err.message)
- this.layout.sendNotification('error', 'Unable to open document', this.currentFile?.name)
- this.onClose()
- }
- })
+ this.onResize()
}
ngOnDestroy() {
this.subscription.unsubscribe()
- if (this.hookShortMime) {
- this.currentFile.shortMime = this.hookShortMime
- }
}
onClose() {
diff --git a/frontend/src/app/applications/files/files.constants.ts b/frontend/src/app/applications/files/files.constants.ts
index 4af8fb6f..9ab391d4 100644
--- a/frontend/src/app/applications/files/files.constants.ts
+++ b/frontend/src/app/applications/files/files.constants.ts
@@ -25,4 +25,33 @@ export const linkProtected = `${assetsUrl}/protected.png`
export const defaultMimeUrl = getAssetsMimeUrl(mimeFile)
export const excludeFromMedias = new Set(['matroska', 'mpegurl', 'msvideo'])
export const compressibleMimes = new Set(['application-gzip', 'application-zip', 'application-x-tar'])
-export const notViewableExtensions = new Set(['rar', '7z', 'iso', 'numbers', 'pages', 'dmg'])
+export const notViewableExtensions = new Set([
+ 'rar',
+ '7z',
+ 'iso',
+ 'zip',
+ 'tar',
+ 'gz',
+ 'bz2',
+ 'xz',
+ 'exe',
+ 'dll',
+ 'msi',
+ 'bat',
+ 'cmd',
+ 'sh',
+ 'apk',
+ 'appimage',
+ 'dmg',
+ 'img',
+ 'bin',
+ 'vhd',
+ 'vmdk',
+ 'so',
+ 'o',
+ 'a',
+ 'lib',
+ 'sys',
+ 'drv',
+ 'cab'
+])
diff --git a/frontend/src/app/applications/files/models/file.model.ts b/frontend/src/app/applications/files/models/file.model.ts
index 47ba2dd9..8e761ec5 100644
--- a/frontend/src/app/applications/files/models/file.model.ts
+++ b/frontend/src/app/applications/files/models/file.model.ts
@@ -147,7 +147,7 @@ export class FileModel implements File {
this.isViewable = true
this.shortMime = 'media'
this.haveThumbnail = true
- } else if (this.shortMime === 'image' && extension !== 'svg') {
+ } else if (this.shortMime === 'image') {
this.isImage = true
this.isViewable = true
this.haveThumbnail = true
diff --git a/frontend/src/app/applications/files/services/files.service.ts b/frontend/src/app/applications/files/services/files.service.ts
index eec54c19..2e71c7bc 100644
--- a/frontend/src/app/applications/files/services/files.service.ts
+++ b/frontend/src/app/applications/files/services/files.service.ts
@@ -10,6 +10,7 @@ import { inject, Injectable } from '@angular/core'
import { DomSanitizer } from '@angular/platform-browser'
import { FILE_OPERATION } from '@sync-in-server/backend/src/applications/files/constants/operations'
import {
+ API_FILES_ONLY_OFFICE_STATUS,
API_FILES_OPERATION_MAKE,
API_FILES_RECENTS,
API_FILES_SEARCH,
@@ -36,6 +37,7 @@ import { downloadWithAnchor } from '../../../common/utils/functions'
import { TAB_MENU } from '../../../layout/layout.interfaces'
import { LayoutService } from '../../../layout/layout.service'
import { StoreService } from '../../../store/store.service'
+import { FilesViewerDialogComponent } from '../components/dialogs/files-viewer-dialog.component'
import { FileContentModel } from '../models/file-content.model'
import { FileRecentModel } from '../models/file-recent.model'
import { FileModel } from '../models/file.model'
@@ -208,6 +210,61 @@ export class FilesService {
)
}
+ async openViewerDialog(mode: 'view' | 'edit', currentFile: FileModel) {
+ this.http.head(currentFile.dataUrl).subscribe({
+ next: async () => {
+ let shortMime: string
+ try {
+ shortMime = await this.viewerHook(mode, currentFile)
+ } catch {
+ // OnlyOffice not enabled, falling back to download
+ this.download(currentFile)
+ return
+ }
+ this.layout.openDialog(FilesViewerDialogComponent, 'full', {
+ id: currentFile.id,
+ initialState: { currentFile: currentFile, mode: mode, shortMime: shortMime } as FilesViewerDialogComponent
+ })
+ },
+ error: (err: HttpErrorResponse) => {
+ console.error(err.message)
+ this.layout.sendNotification('error', 'Unable to open document', currentFile?.name)
+ }
+ })
+ }
+
+ private async viewerHook(mode: 'view' | 'edit', currentFile: FileModel): Promise<string> {
+ const onlyOfficeEnabled = await this.getOnlyOfficeStatus()
+ if (currentFile.shortMime === 'document' && !onlyOfficeEnabled) {
+ if (currentFile.mime.startsWith('text-')) {
+ return 'text'
+ }
+ throw new Error('Feature not enabled')
+ }
+ if (currentFile.shortMime === 'pdf') {
+ if (mode === 'edit' && onlyOfficeEnabled) {
+ return 'document'
+ }
+ }
+ return currentFile.shortMime
+ }
+
+ private async getOnlyOfficeStatus(): Promise<boolean> {
+ if (this.store.filesOnlyOffice().enabled !== null) {
+ return this.store.filesOnlyOffice().enabled
+ }
+ try {
+ const status = await firstValueFrom(this.http.get<{ enabled: boolean }>(API_FILES_ONLY_OFFICE_STATUS))
+ // if the request succeeds, "enabled" will always be true
+ this.store.filesOnlyOffice.set(status)
+ return status.enabled
+ } catch {
+ const fallback = { enabled: false }
+ this.store.filesOnlyOffice.set(fallback)
+ return fallback.enabled
+ }
+ }
+
private isValidName(fileName: string): boolean {
try {
isValidFileName(fileName)
diff --git a/frontend/src/app/applications/spaces/components/spaces-browser.component.ts b/frontend/src/app/applications/spaces/components/spaces-browser.component.ts
index ed28b356..9943414f 100644
--- a/frontend/src/app/applications/spaces/components/spaces-browser.component.ts
+++ b/frontend/src/app/applications/spaces/components/spaces-browser.component.ts
@@ -68,7 +68,6 @@ import { FilesCompressionDialogComponent } from '../../files/components/dialogs/
import { FilesNewDialogComponent } from '../../files/components/dialogs/files-new-dialog.component'
import { FilesTrashDialogComponent } from '../../files/components/dialogs/files-trash-dialog.component'
import { FilesTrashEmptyDialogComponent } from '../../files/components/dialogs/files-trash-empty-dialog.component'
-import { FilesViewerDialogComponent } from '../../files/components/dialogs/files-viewer-dialog.component'
import { FilePermissionsComponent } from '../../files/components/utils/file-permissions.component'
import { FileEvent } from '../../files/interfaces/file-event.interface'
import { FileModel } from '../../files/models/file.model'
@@ -696,10 +695,7 @@ export class SpacesBrowserComponent implements OnInit, AfterViewInit, OnDestroy
}
private openViewerDialog(mode: 'view' | 'edit') {
- this.layout.openDialog(FilesViewerDialogComponent, 'full', {
- id: this.selection[0].id,
- initialState: { currentFile: this.selection[0], mode: mode } as FilesViewerDialogComponent
- })
+ this.filesService.openViewerDialog(mode, this.selection[0]).catch(console.error)
}
private focusOn(select: string) {
diff --git a/frontend/src/app/store/store.service.ts b/frontend/src/app/store/store.service.ts
index 95dc12c9..59394452 100644
--- a/frontend/src/app/store/store.service.ts
+++ b/frontend/src/app/store/store.service.ts
@@ -42,6 +42,8 @@ export class StoreService {
public filesActiveTasks = new BehaviorSubject<FileTask[]>([])
public filesEndedTasks = new BehaviorSubject<FileTask[]>([])
public filesRecents: WritableSignal<FileRecentModel[]> = signal<FileRecentModel[]>([])
+ // OnlyOffice
+ public filesOnlyOffice: WritableSignal<{ enabled: boolean }> = signal({ enabled: null })
// Search
public currentSearch: WritableSignal<SearchFilesDto> = signal<SearchFilesDto>({ content: '', fullText: false })
public filesSearch: WritableSignal<FileContentModel[]> = signal<FileContentModel[]>([])