Skip to content

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

fix(files): handle document-open error messages for HEAD requests

johaven328d8235497c
diff --git a/backend/src/applications/files/constants/operations.ts b/backend/src/applications/files/constants/operations.ts
index 9956ce63..87373bd6 100644
--- a/backend/src/applications/files/constants/operations.ts
+++ b/backend/src/applications/files/constants/operations.ts
@@ -27,3 +27,9 @@ export enum FILE_REPOSITORY {
}
export const FORCE_AS_FILE_OWNER = 'forceAsFileOwner' as const
+
+export const SEND_FILE_ERROR_MSG = {
+ 400: 'The location is a directory',
+ 404: 'Location not found',
+ 405: 'The location is not readable'
+} as const
diff --git a/backend/src/applications/files/services/files-event-manager.service.ts b/backend/src/applications/files/services/files-event-manager.service.ts
index 9c1f9b58..5ae40432 100644
--- a/backend/src/applications/files/services/files-event-manager.service.ts
+++ b/backend/src/applications/files/services/files-event-manager.service.ts
@@ -9,8 +9,9 @@ import { quotaCacheKeyFromSpace } from '../utils/quota'
@Injectable()
export class FilesEventManager implements OnModuleDestroy {
/* Used to:
- - process cache events (storage usage updates, new file indexing) in the main worker
- - handle versioning
+ - store cached events for storage usage updates
+ - todo: handle indexing
+ - todo: handle versioning
*/
private readonly MAX_BUFFER_SIZE = 1_000
private readonly MAX_BUFFER_DELAY_MS = 20_000
diff --git a/backend/src/applications/files/utils/send-file.ts b/backend/src/applications/files/utils/send-file.ts
index 2776fc65..b42ae0ad 100644
--- a/backend/src/applications/files/utils/send-file.ts
+++ b/backend/src/applications/files/utils/send-file.ts
@@ -4,6 +4,7 @@ import { FastifyReply, FastifyRequest } from 'fastify'
import { DEFAULT_HIGH_WATER_MARK } from '../constants/files'
import { FileError } from '../models/file-error'
import { fileName, isPathExists, isPathIsDir, isPathIsReadable } from './files'
+import { SEND_FILE_ERROR_MSG } from '../constants/operations'
export function makeContentDispositionAttachment(fileName: string) {
const downloadName = fileName.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
@@ -30,13 +31,13 @@ export class SendFile {
async checks() {
if (!(await isPathExists(this.filePath))) {
- throw new FileError(HttpStatus.NOT_FOUND, 'Location not found')
+ throw new FileError(HttpStatus.NOT_FOUND, SEND_FILE_ERROR_MSG[HttpStatus.NOT_FOUND])
}
if (await isPathIsDir(this.filePath)) {
- throw new FileError(HttpStatus.BAD_REQUEST, 'The location is a directory')
+ throw new FileError(HttpStatus.BAD_REQUEST, SEND_FILE_ERROR_MSG[HttpStatus.BAD_REQUEST])
}
if (!(await isPathIsReadable(this.filePath))) {
- throw new FileError(HttpStatus.METHOD_NOT_ALLOWED, 'The location is not readable')
+ throw new FileError(HttpStatus.METHOD_NOT_ALLOWED, SEND_FILE_ERROR_MSG[HttpStatus.METHOD_NOT_ALLOWED])
}
}
diff --git a/frontend/src/app/applications/files/services/files.service.ts b/frontend/src/app/applications/files/services/files.service.ts
index 041c1853..3ee12cb6 100644
--- a/frontend/src/app/applications/files/services/files.service.ts
+++ b/frontend/src/app/applications/files/services/files.service.ts
@@ -2,7 +2,12 @@ import type { TreeNode } from '@ali-hm/angular-tree-component'
import { HttpClient, HttpErrorResponse, HttpParams } from '@angular/common/http'
import { inject, Injectable } from '@angular/core'
import { DomSanitizer } from '@angular/platform-browser'
-import { FILE_MODE, FILE_OPERATION, FORCE_AS_FILE_OWNER } from '@sync-in-server/backend/src/applications/files/constants/operations'
+import {
+ FILE_MODE,
+ FILE_OPERATION,
+ FORCE_AS_FILE_OWNER,
+ SEND_FILE_ERROR_MSG
+} from '@sync-in-server/backend/src/applications/files/constants/operations'
import {
API_FILES_OPERATION,
API_FILES_OPERATION_MAKE,
@@ -345,7 +350,9 @@ export class FilesService {
} satisfies Partial<FilesViewerDialogComponent>
})
},
- error: (e: HttpErrorResponse) => {
+ 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)
}
})
diff --git a/frontend/src/app/layout/layout.service.ts b/frontend/src/app/layout/layout.service.ts
index 91c92edc..e9b084c1 100644
--- a/frontend/src/app/layout/layout.service.ts
+++ b/frontend/src/app/layout/layout.service.ts
@@ -232,12 +232,11 @@ export class LayoutService {
override: any = {}
): ActiveToast<any> | void {
if (type === 'error' && e) {
- console.error(e)
const errorMessage = e.error
? Array.isArray(e.error.message)
? e.error.message.map((e: string) => this.translateString(e)).join(' & ')
: this.translateString(e.error.message)
- : e.message || 'Unknown error !'
+ : this.translateString(e.message || 'Unknown error !')
if (this.electron.enabled) {
this.electron.sendMessage(this.translateString(title), `${this.translateString(message)} - ${errorMessage}`)
} else {