Просмотр исходного кода

Add config manager argument to content headers

arvid-e 9 месяцев назад
Родитель
Сommit
6eb2027d0d

+ 5 - 3
apps/app/src/server/routes/attachment/get.ts

@@ -9,6 +9,7 @@ import mongoose from 'mongoose';
 
 import type { CrowiProperties, CrowiRequest } from '~/interfaces/crowi-request';
 import { ResponseMode, type ExpressHttpHeader, type RespondOptions } from '~/server/interfaces/attachment';
+import type { ConfigManager } from '~/server/service/config-manager';
 import {
   type FileUploader,
   toExpressHttpHeaders, ContentHeaders, applyHeaders,
@@ -107,10 +108,11 @@ const respondForRedirectMode = async(res: Response, fileUploadService: FileUploa
   }
 };
 
-const respondForRelayMode = async(res: Response, fileUploadService: FileUploader, attachment: IAttachmentDocument, opts?: RespondOptions): Promise<void> => {
+const respondForRelayMode = async(crowi: Crowi, res: Response, fileUploadService: FileUploader,
+    attachment: IAttachmentDocument, opts?: RespondOptions): Promise<void> => {
   // apply content-* headers before response
   const isDownload = opts?.download ?? false;
-  const contentHeaders = new ContentHeaders(attachment, { inline: !isDownload });
+  const contentHeaders = new ContentHeaders(crowi.configManager as ConfigManager, attachment, { inline: !isDownload });
   applyHeaders(res, contentHeaders.toExpressHttpHeaders());
 
   try {
@@ -148,7 +150,7 @@ export const getActionFactory = (crowi: Crowi, attachment: IAttachmentDocument)
         respondForRedirectMode(res, fileUploadService, attachment, opts);
         return;
       case ResponseMode.RELAY:
-        respondForRelayMode(res, fileUploadService, attachment, opts);
+        respondForRelayMode(crowi, res, fileUploadService, attachment, opts);
         return;
     }
   };

+ 10 - 2
apps/app/src/server/service/file-uploader/azure.ts

@@ -24,6 +24,7 @@ import { FilePathOnStoragePrefix, ResponseMode, type RespondOptions } from '~/se
 import type { IAttachmentDocument } from '~/server/models/attachment';
 import loggerFactory from '~/utils/logger';
 
+import type { ConfigManager } from '../config-manager';
 import { configManager } from '../config-manager';
 
 import {
@@ -85,6 +86,13 @@ function getFilePathOnStorage(attachment: IAttachmentDocument) {
 
 class AzureFileUploader extends AbstractFileUploader {
 
+  private readonly configManager: ConfigManager;
+
+  constructor(crowi: Crowi) {
+    super(crowi);
+    this.configManager = crowi.configManager as ConfigManager;
+  }
+
   /**
    * @inheritdoc
    */
@@ -132,7 +140,7 @@ class AzureFileUploader extends AbstractFileUploader {
     const filePath = getFilePathOnStorage(attachment);
     const containerClient = await getContainerClient();
     const blockBlobClient: BlockBlobClient = containerClient.getBlockBlobClient(filePath);
-    const contentHeaders = new ContentHeaders(attachment);
+    const contentHeaders = new ContentHeaders(this.configManager, attachment);
 
     await blockBlobClient.uploadStream(readable, undefined, undefined, {
       blobHTTPHeaders: {
@@ -210,7 +218,7 @@ class AzureFileUploader extends AbstractFileUploader {
       const userDelegationKey = await blobServiceClient.getUserDelegationKey(startsOn, expiresOn);
 
       const isDownload = opts?.download ?? false;
-      const contentHeaders = new ContentHeaders(attachment, { inline: !isDownload });
+      const contentHeaders = new ContentHeaders(this.configManager, attachment, { inline: !isDownload });
 
       // https://github.com/Azure/azure-sdk-for-js/blob/d4d55f73/sdk/storage/storage-blob/src/ContainerSASPermissions.ts#L24
       // r:read, a:add, c:create, w:write, d:delete, l:list

+ 1 - 1
apps/app/src/server/service/file-uploader/gridfs.ts

@@ -65,7 +65,7 @@ class GridfsFileUploader extends AbstractFileUploader {
   override async uploadAttachment(readable: Readable, attachment: IAttachmentDocument): Promise<void> {
     logger.debug(`File uploading: fileName=${attachment.fileName}`);
 
-    const contentHeaders = new ContentHeaders(attachment);
+    const contentHeaders = new ContentHeaders(configManager, attachment);
 
     return AttachmentFile.promisifiedWrite(
       {

+ 1 - 1
apps/app/src/server/service/file-uploader/local.ts

@@ -229,7 +229,7 @@ module.exports = function(crowi: Crowi) {
     const internalPath = urljoin(internalPathRoot, relativePath);
 
     const isDownload = opts?.download ?? false;
-    const contentHeaders = new ContentHeaders(attachment, { inline: !isDownload });
+    const contentHeaders = new ContentHeaders(configManager, attachment, { inline: !isDownload });
     applyHeaders(res, [
       ...contentHeaders.toExpressHttpHeaders(),
       { field: 'X-Accel-Redirect', value: internalPath },

+ 8 - 4
apps/app/src/server/service/file-uploader/utils/headers.ts

@@ -23,10 +23,14 @@ export class ContentHeaders implements IContentHeaders {
 
   private configManager: ConfigManager;
 
-  constructor(attachment: IAttachmentDocument, opts?: {
-    configManager: ConfigManager,
-    inline?: boolean,
-  }) {
+  constructor(
+      configManager: ConfigManager,
+      attachment: IAttachmentDocument,
+      opts?: {
+      inline?: boolean,
+    },
+  ) {
+    this.configManager = configManager;
 
     const attachmentContentType = attachment.fileFormat;
     const filename = attachment.originalName;