Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
feat(frontend:files): add binary probe for unknown text files
johavenfea9e17e9387
diff --git a/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.html b/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.htmlindex be5120dc..dd699e40 100644--- a/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.html+++ b/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.html@@ -328,17 +328,17 @@@if (warnOnUnsavedChanges()) {<button (click)="save(true)"- class="btn btn-primary me-2 ms-auto"+ class="btn btn-sm btn-primary me-2 ms-auto"type="button"l10nTranslate>Save And Exit</button><button (click)="onClose()"- class="btn btn-danger me-2"+ class="btn btn-sm btn-danger me-2"data-dismiss="modal" type="button"l10nTranslate>Close Without Saving</button><button (click)="warnOnUnsavedChanges.set(false)"- class="d-none d-sm-block btn btn-default"+ class="d-none d-sm-block btn btn-sm btn-default"data-dismiss="modal" type="button"l10nTranslate>Cancel</button>diff --git a/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.html b/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.htmlindex 168e58fa..63dc00d0 100644--- a/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.html+++ b/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.html@@ -59,17 +59,17 @@</button>@if (warnOnUnsavedChanges()) {<button (click)="save(true)"- class="btn btn-primary me-2 ms-auto"+ class="btn btn-sm btn-primary me-2 ms-auto"type="button"l10nTranslate>Save And Exit</button><button (click)="onClose()"- class="btn btn-danger me-2"+ class="btn btn-sm btn-danger me-2"data-dismiss="modal" type="button"l10nTranslate>Close Without Saving</button><button (click)="warnOnUnsavedChanges.set(false)"- class="d-none d-sm-block btn btn-default"+ class="d-none d-sm-block btn btn-sm btn-default"data-dismiss="modal" type="button"l10nTranslate>Cancel</button>diff --git a/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.ts b/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.tsindex 8a75298e..e0b9f375 100644--- a/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.ts+++ b/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.ts@@ -103,7 +103,7 @@ export class FilesViewerTextComponent extends FilesViewerEditableBase implements}}- async ngOnInit() {+ ngOnInit() {const language: LanguageDescription = LanguageDescription.matchFilename(languages, this.file().name)this.currentLanguage = language?.namethis.isSupported.set(true)diff --git a/frontend/src/app/applications/files/files.constants.ts b/frontend/src/app/applications/files/files.constants.tsindex 71d3c6b0..f6e06420 100644--- a/frontend/src/app/applications/files/files.constants.ts+++ b/frontend/src/app/applications/files/files.constants.ts@@ -17,7 +17,7 @@ export const logoDarkUrl = `${assetsUrl}/logo-dark${mimeExtension}`export const logoUrl = `${assetsUrl}/logo${mimeExtension}`export const linkProtected = `${assetsUrl}/protected.png`export const defaultMimeUrl = getAssetsMimeUrl(mimeFile)-export const MAX_TEXT_FILE_SIZE = 10485760 // 10 MB+export const MAX_TEXT_FILE_SIZE = 25 * 1024 * 1024 // 25 MBexport const COMPRESSIBLE_MIMES = new Set(['application-gzip', 'application-zip', 'application-x-tar'])export const SHORT_MIME = {DOCUMENT: 'document',@@ -30,28 +30,119 @@ export const SHORT_MIME = {export const UNSUPPORTED_VIEW_EXTENSIONS = new Set(['rar','7z',- 'iso','zip','tar',+ 'tgz','gz',+ 'gzip','bz2',+ 'tbz',+ 'tbz2','xz',+ 'txz',+ 'zst',+ 'zstd',+ 'br',+ 'lz',+ 'lzma',+ 'lzo',+ 'iso',+ 'dmg',+ 'img',+ 'toast',+ 'vhd',+ 'vhdx',+ 'vdi',+ 'vmdk',+ 'qcow2',+ 'hdd',+ 'sparseimage','exe',+ 'com','dll','msi',- 'cmd',+ 'scr','apk',+ 'ipa','appimage',- 'dmg',- 'img',+ 'deb',+ 'rpm',+ 'pkg',+ 'snap','bin',- 'vhd',- 'vmdk',+ 'elf',+ 'wasm',+ 'class',+ 'pyc',+ 'pyo',+ 'dex',+ 'odex','so','o','a','lib','sys','drv',- 'cab'+ 'cab',+ 'jar',+ 'war',+ 'ear',+ 'whl',+ 'nupkg',+ 'db',+ 'db3',+ 'sqlite',+ 'sqlite3',+ 'mdb',+ 'accdb',+ 'ttf',+ 'otf',+ 'woff',+ 'woff2',+ 'eot',+ 'doc',+ 'docm',+ 'docx',+ 'dot',+ 'dotm',+ 'dotx',+ 'xls',+ 'xlsb',+ 'xlsm',+ 'xlsx',+ 'xlt',+ 'xltm',+ 'xltx',+ 'ppt',+ 'pptm',+ 'pptx',+ 'pot',+ 'potm',+ 'potx',+ 'pps',+ 'ppsm',+ 'ppsx',+ 'odt',+ 'ods',+ 'odp',+ 'pages',+ 'numbers',+ 'key',+ 'epub',+ 'vsd',+ 'vsdm',+ 'vsdx',+ 'vss',+ 'vssm',+ 'vssx',+ 'vst',+ 'vstm',+ 'vstx',+ 'dwg',+ 'sketch',+ 'fig',+ 'psd',+ 'ai',+ 'indd',+ 'blend'])diff --git a/frontend/src/app/applications/files/models/file.model.ts b/frontend/src/app/applications/files/models/file.model.tsindex c563e957..65878e2d 100644--- a/frontend/src/app/applications/files/models/file.model.ts+++ b/frontend/src/app/applications/files/models/file.model.ts@@ -78,6 +78,7 @@ export class FileModel implements File {isImage = falseisViewable = falseisEditable = false+ isTextProbeRequired = falseisCompressible = trueisBeingDeleted = falseisSelected = false@@ -167,28 +168,16 @@ export class FileModel implements File {return this.getType(inShare)}- if (!mime || mime === mimeFile) {- this.isViewable = true- this.shortMime = SHORT_MIME.TEXT- return this.getType(inShare)- }-- if (mime.includes(SHORT_MIME.MARKDOWN)) {- this.shortMime = SHORT_MIME.MARKDOWN- this.isViewable = true- this.isEditable = true- return mime- }-const extension = this.getExtension()- const dash = mime.indexOf('-')- const temporaryMime = dash >= 0 ? mime.slice(0, dash) : mime++ const dash = mime?.indexOf('-') ?? -1+ const temporaryMime = mime && dash >= 0 ? mime.slice(0, dash) : mimeif (extension === SHORT_MIME.PDF) {this.shortMime = SHORT_MIME.PDFthis.isViewable = truethis.isEditable = editorConfig.onlyoffice === true- return mime+ return mime || mimeFile}if (@@ -198,6 +187,25 @@ export class FileModel implements File {this.shortMime = SHORT_MIME.DOCUMENTthis.isEditable = truethis.isViewable = true+ return mime || mimeFile+ }++ if ((!mime || mime === mimeFile) && UNSUPPORTED_VIEW_EXTENSIONS.has(extension)) {+ return this.getType(inShare)+ }++ if (!mime || mime === mimeFile) {+ this.isViewable = true+ this.isEditable = true+ this.isTextProbeRequired = true+ this.shortMime = SHORT_MIME.TEXT+ return this.getType(inShare)+ }++ if (mime.includes(SHORT_MIME.MARKDOWN)) {+ this.shortMime = SHORT_MIME.MARKDOWN+ this.isViewable = true+ this.isEditable = truereturn mime}@@ -233,9 +241,8 @@ export class FileModel implements File {this.shortMime = SHORT_MIME.TEXTthis.isViewable = truethis.isEditable = true- return mime+ this.isTextProbeRequired = temporaryMime !== SHORT_MIME.TEXT}-return mime}diff --git a/frontend/src/app/applications/files/services/files.service.ts b/frontend/src/app/applications/files/services/files.service.tsindex 7965e979..1caac9aa 100644--- a/frontend/src/app/applications/files/services/files.service.ts+++ b/frontend/src/app/applications/files/services/files.service.ts@@ -54,6 +54,8 @@ import { FileRecentModel } from '../models/file-recent.model'import { FileModel } from '../models/file.model'import { FilesTasksService } from './files-tasks.service'+type ViewerHookResult = { action: 'open'; shortMime: string } | { action: 'download'; message?: string }+@Injectable({ providedIn: 'root' })export class FilesService {// Tree section@@ -63,6 +65,8 @@ export class FilesService {public clipboardAction: 'copyPaste' | 'cutPaste' = 'copyPaste'// Filespublic currentRoute: string+ private readonly textFileSizeLimitExceededMessage = 'File size limit exceeded'+ private readonly textBinaryProbeBytes = 4096private readonly http = inject(HttpClient)private readonly layout = inject(LayoutService)private readonly store = inject(StoreService)@@ -292,81 +296,130 @@ export class FilesService {}async openViewerDialog(file: FileModel, directoryFiles: FileModel[], permissions: string): Promise<void> {+ if (this.useTextProbeAsAvailabilityCheck(file)) {+ await this.openViewerAfterAvailabilityCheck(file, directoryFiles, permissions).catch((e) => this.sendOpenDocumentError(file, e))+ return+ }this.http.head(file.dataUrl).subscribe({- next: async () => {- // This check is only used for the text viewer; other viewers are read-only or enforce permissions on the backend.- const isWriteable = !file?.lock?.isExclusive && permissions.includes(SPACE_OPERATION.MODIFY)- const mode: FILE_MODE = isWriteable ? FILE_MODE.EDIT : FILE_MODE.VIEW-- let hookedShortMime: string- try {- hookedShortMime = await this.viewerHook(file)- if (file?.lock?.isExclusive) {- this.layout.sendNotification('info', 'The file is locked', fileLockPropsToString(file.lock))- }- } catch {- // No office editors are enabled, falling back to download- this.download(file)- return- }+ next: () => this.openViewerAfterAvailabilityCheck(file, directoryFiles, permissions).catch((e) => this.sendOpenDocumentError(file, e)),+ error: (e: HttpErrorResponse | any) => this.sendOpenDocumentError(file, e)+ })+ }++ private async openViewerAfterAvailabilityCheck(file: FileModel, directoryFiles: FileModel[], permissions: string): Promise<void> {+ // This check is only used for the text viewer; other viewers are read-only or enforce permissions on the backend.+ const isWriteable = !file?.lock?.isExclusive && permissions.includes(SPACE_OPERATION.MODIFY)+ const mode: FILE_MODE = isWriteable ? FILE_MODE.EDIT : FILE_MODE.VIEW- const editorProvider: FileEditorProviders = { collabora: false, onlyoffice: false }- if (hookedShortMime === SHORT_MIME.DOCUMENT) {- if (this.store.server().files.editors.collabora && this.store.server().files.editors.onlyoffice) {- // Case with multiple editors- const collaboraHasExtension = COLLABORA_ONLINE_EXTENSIONS.has(file.getExtension())- const onlyofficeHasExtension = ONLY_OFFICE_EXTENSIONS.has(file.getExtension())- if (collaboraHasExtension && onlyofficeHasExtension) {- // Get user's saved preference- const userEditorPreference = this.userService.getEditorProviderPreference()- if (userEditorPreference && userEditorPreference in editorProvider) {- editorProvider[userEditorPreference] = true- } else {- // Both editors support this file extension, let the user choose- await this.openSelectViewerDialog(file, editorProvider)- if (!editorProvider.onlyoffice && !editorProvider.collabora) return- }- } else {- // Based on the supported extension- editorProvider.collabora = collaboraHasExtension- editorProvider.onlyoffice = onlyofficeHasExtension- }+ let hookResult: ViewerHookResult+ try {+ hookResult = await this.viewerHook(file)+ } catch (e) {+ this.sendOpenDocumentError(file, e)+ return+ }++ if (hookResult.action === 'download') {+ if (hookResult.message) {+ this.layout.sendNotification('warning', 'Download', hookResult.message)+ }+ this.download(file)+ return+ }++ const hookedShortMime = hookResult.shortMime+ if (file?.lock?.isExclusive) {+ this.layout.sendNotification('info', 'The file is locked', fileLockPropsToString(file.lock))+ }++ const editorProvider: FileEditorProviders = { collabora: false, onlyoffice: false }+ if (hookedShortMime === SHORT_MIME.DOCUMENT) {+ if (this.store.server().files.editors.collabora && this.store.server().files.editors.onlyoffice) {+ // Case with multiple editors+ const collaboraHasExtension = COLLABORA_ONLINE_EXTENSIONS.has(file.getExtension())+ const onlyofficeHasExtension = ONLY_OFFICE_EXTENSIONS.has(file.getExtension())+ if (collaboraHasExtension && onlyofficeHasExtension) {+ // Get user's saved preference+ const userEditorPreference = this.userService.getEditorProviderPreference()+ if (userEditorPreference && userEditorPreference in editorProvider) {+ editorProvider[userEditorPreference] = true} else {- // Based on availability- editorProvider.collabora = this.store.server().files.editors.collabora- editorProvider.onlyoffice = this.store.server().files.editors.onlyoffice+ // Both editors support this file extension, let the user choose+ await this.openSelectViewerDialog(file, editorProvider)+ if (!editorProvider.onlyoffice && !editorProvider.collabora) return}+ } else {+ // Based on the supported extension+ editorProvider.collabora = collaboraHasExtension+ editorProvider.onlyoffice = onlyofficeHasExtension}-- this.layout.openDialog(FilesViewerDialogComponent, 'full', {- id: file.id, // only used to manage the modal- initialState: {- currentFile: file,- directoryFiles: directoryFiles,- mode: mode,- isWriteable: isWriteable,- hookedShortMime: hookedShortMime,- editorProvider: editorProvider- } satisfies Partial<FilesViewerDialogComponent>- })- },- error: (e: HttpErrorResponse | any) => {- // HEAD requests do not include a body or custom message- e.message = e.status in SEND_FILE_ERROR_MSG ? SEND_FILE_ERROR_MSG[e.status] : e.statusText- this.layout.sendNotification('error', 'Unable to open document', file?.name, e)+ } else {+ // Based on availability+ editorProvider.collabora = this.store.server().files.editors.collabora+ editorProvider.onlyoffice = this.store.server().files.editors.onlyoffice}+ }++ this.layout.openDialog(FilesViewerDialogComponent, 'full', {+ id: file.id, // only used to manage the modal+ initialState: {+ currentFile: file,+ directoryFiles: directoryFiles,+ mode: mode,+ isWriteable: isWriteable,+ hookedShortMime: hookedShortMime,+ editorProvider: editorProvider+ } satisfies Partial<FilesViewerDialogComponent>})}- private async viewerHook(file: FileModel): Promise<string> {+ private sendOpenDocumentError(file: FileModel, e: HttpErrorResponse | any): void {+ // Availability checks do not include a body or custom message.+ if (e?.status) {+ e.message = e.status in SEND_FILE_ERROR_MSG ? SEND_FILE_ERROR_MSG[e.status] : e.statusText+ }+ this.layout.sendNotification('error', 'Unable to open document', file?.name, e)+ }++ private async viewerHook(file: FileModel): Promise<ViewerHookResult> {if (file.shortMime === SHORT_MIME.TEXT || file.shortMime === SHORT_MIME.MARKDOWN) {- if (file.size < MAX_TEXT_FILE_SIZE) {- return file.shortMime+ if (file.shortMime === SHORT_MIME.TEXT && (await this.hasBinaryContent(file))) {+ return { action: 'download' }+ }+ if (file.size >= MAX_TEXT_FILE_SIZE) {+ // Download if too large+ return { action: 'download', message: this.textFileSizeLimitExceededMessage }+ }+ return { action: 'open', shortMime: file.shortMime }+ }+ return { action: 'open', shortMime: file.shortMime }+ }++ private useTextProbeAsAvailabilityCheck(file: FileModel): boolean {+ return file.shortMime === SHORT_MIME.TEXT && file.isTextProbeRequired && file.size > 0+ }++ private async hasBinaryContent(file: FileModel): Promise<boolean> {+ if (!file.isTextProbeRequired || file.size === 0) return false+ const content = await firstValueFrom(+ this.http.get(file.dataUrl, {+ headers: { Range: `bytes=0-${this.textBinaryProbeBytes - 1}` },+ responseType: 'arraybuffer'+ })+ )+ return this.looksBinary(new Uint8Array(content))+ }++ private looksBinary(bytes: Uint8Array): boolean {+ if (!bytes.length) return false+ let controlBytes = 0+ for (const byte of bytes) {+ if (byte === 0) return true+ if (byte < 32 && byte !== 9 && byte !== 10 && byte !== 13) {+ controlBytes++}- // Download if too large- throw new Error('No editor found')}- return file.shortMime+ return controlBytes / bytes.length > 0.02}private isValidName(fileName: string): boolean {diff --git a/frontend/src/i18n/fr.json b/frontend/src/i18n/fr.jsonindex 259c3cf4..5583b4a2 100644--- a/frontend/src/i18n/fr.json+++ b/frontend/src/i18n/fr.json@@ -373,7 +373,7 @@"The destination already exists": "La destination existe déjà","This item is already selected": "Ce fichier est déjà sélectionné","Parent item is already selected": "L'élément parent est déjà sélectionné",- "This file contains binary data that can not be read": "Ce fichier contient des données binaires qu\"il n\"est pas possible de lire",+ "This file contains binary data that can not be read": "Ce fichier contient des données binaires qu'il n'est pas possible de lire","Navigation Tree": "Arborescence de navigation","item": "élément","items": "éléments",