From 82228da8fa24e497a43e4e1e59171d2005e6d691 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Fri, 1 Dec 2023 14:29:41 +0700 Subject: [PATCH] fix: hash tag cannot be used as part of url --- Services/server/src/controllers/fileController.ts | 12 ++++++------ .../src/controllers/subFolderFileController.ts | 12 ++++++------ Services/server/src/utils/minio.ts | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Services/server/src/controllers/fileController.ts b/Services/server/src/controllers/fileController.ts index df3236f..5d7c6d9 100644 --- a/Services/server/src/controllers/fileController.ts +++ b/Services/server/src/controllers/fileController.ts @@ -22,7 +22,7 @@ import HttpStatusCode from "../interfaces/http-status"; import { StorageFile } from "../interfaces/storage-fs"; import HttpError from "../interfaces/http-error"; -import { copyCond, pathExist } from "../utils/minio"; +import { copyCond, pathExist, replaceIllegalChars } from "../utils/minio"; const DEFAULT_BUCKET = process.env.MINIO_BUCKET; const DEFAULT_INDEX = process.env.ELASTICSEARCH_INDEX; @@ -149,7 +149,7 @@ export class FileController extends Controller { } const basePath = `${cabinetName}/${drawerName}/${folderName}/`; - const pathname = `${basePath}${body.file}`; + const pathname = `${basePath}${replaceIllegalChars(body.file)}`; if (!(await pathExist(DEFAULT_BUCKET!, basePath))) { throw new HttpError( @@ -180,7 +180,7 @@ export class FileController extends Controller { const metadata: Partial = { pathname, path: basePath, - fileName: body.file, + fileName: replaceIllegalChars( body.file ), fileSize: 0, fileType: "", title: body.title ?? "", @@ -272,7 +272,7 @@ export class FileController extends Controller { // assume user will probably replace file by re-upload but maybe just rename if (body.file) { - const destination = `${basePath}${body.file}`; + const destination = `${basePath}${replaceIllegalChars(body.file)}`; const source = `/${DEFAULT_BUCKET}/${basePath}${fileName}`; const copy = await minioClient.copyObject(DEFAULT_BUCKET!, destination, source, copyCond); @@ -293,7 +293,7 @@ export class FileController extends Controller { doc: { pathname: destination, path: basePath, - fileName: body.file, + fileName: replaceIllegalChars(body.file), updatedAt: new Date().toISOString(), updatedBy: request.user.preferred_username ?? "n/a", }, @@ -331,7 +331,7 @@ export class FileController extends Controller { ? { upload: await minioClient.presignedPutObject( DEFAULT_BUCKET!, - `${basePath}${body.file ?? fileName}`, + `${basePath}${replaceIllegalChars(body.file) ?? fileName}`, ), } : this.setStatus(HttpStatusCode.NO_CONTENT); diff --git a/Services/server/src/controllers/subFolderFileController.ts b/Services/server/src/controllers/subFolderFileController.ts index 27ea173..2bdc672 100644 --- a/Services/server/src/controllers/subFolderFileController.ts +++ b/Services/server/src/controllers/subFolderFileController.ts @@ -22,7 +22,7 @@ import HttpStatusCode from "../interfaces/http-status"; import { StorageFile } from "../interfaces/storage-fs"; import HttpError from "../interfaces/http-error"; -import { copyCond, pathExist } from "../utils/minio"; +import { copyCond, pathExist, replaceIllegalChars } from "../utils/minio"; const DEFAULT_BUCKET = process.env.MINIO_BUCKET; const DEFAULT_INDEX = process.env.ELASTICSEARCH_INDEX; @@ -155,7 +155,7 @@ export class SubFolderFileController extends Controller { } const basePath = `${cabinetName}/${drawerName}/${folderName}/${subFolderName}/`; - const pathname = `${basePath}${body.file}`; + const pathname = `${basePath}${replaceIllegalChars(body.file)}`; if (!(await pathExist(DEFAULT_BUCKET!, basePath))) { throw new HttpError( @@ -186,7 +186,7 @@ export class SubFolderFileController extends Controller { const metadata: Partial = { pathname, path: basePath, - fileName: body.file, + fileName: replaceIllegalChars(body.file), fileSize: 0, fileType: "", title: body.title ?? "", @@ -279,7 +279,7 @@ export class SubFolderFileController extends Controller { // assume user will probably replace file by re-upload but maybe just rename if (body.file) { - const destination = `${basePath}${body.file}`; + const destination = `${basePath}${replaceIllegalChars(body.file)}`; const source = `/${DEFAULT_BUCKET}/${basePath}${fileName}`; const copy = await minioClient.copyObject(DEFAULT_BUCKET!, destination, source, copyCond); @@ -299,7 +299,7 @@ export class SubFolderFileController extends Controller { id, doc: { pathname: destination, - fileName: body.file, + fileName: replaceIllegalChars(body.file), updatedAt: new Date().toISOString(), updatedBy: request.user.preferred_username ?? "n/a", }, @@ -337,7 +337,7 @@ export class SubFolderFileController extends Controller { ? { upload: await minioClient.presignedPutObject( DEFAULT_BUCKET!, - `${basePath}${body.file ?? fileName}`, + `${basePath}${replaceIllegalChars(body.file) ?? fileName}`, ), } : this.setStatus(HttpStatusCode.NO_CONTENT); diff --git a/Services/server/src/utils/minio.ts b/Services/server/src/utils/minio.ts index d01369d..4dc5b18 100644 --- a/Services/server/src/utils/minio.ts +++ b/Services/server/src/utils/minio.ts @@ -9,7 +9,7 @@ import minioClient from "../minio"; * @returns illegal character replaced path */ export function replaceIllegalChars(path: string, replace = "-") { - return path.replace(/[/\\?%*:|"<>]/g, replace); + return path.replace(/[/\\?%*:|"<>#]/g, replace); } /**