Explorar o código

Move strict and lax logic to frontend and let backend only set recieved mimeType settings

arvid-e hai 6 meses
pai
achega
583b32a102

+ 28 - 71
apps/app/src/server/routes/apiv3/content-disposition-settings.ts

@@ -20,43 +20,6 @@ module.exports = (crowi) => {
   /**
  * @swagger
  *
- * /content-disposition-settings:
- *   get:
- *     tags: [Content-Disposition Settings]
- *     summary: Get content disposition settings for configurable MIME types
- *     security:
- *       - cookieAuth: []
- *     responses:
- *       200:
- *         description: Successfully retrieved content disposition settings.
- *         content:
- *           application/json:
- *             schema:
- *               type: object
- *               properties:
- *                 contentDispositionSettings:
- *                   type: object
- *                   additionalProperties:
- *                     type: string
- *                     description: inline or attachment
- *
- */
-  router.get('/', loginRequiredStrictly, adminRequired, async(req, res) => {
-    try {
-      const currentDispositionSettings = configManager.getConfig('attachments:contentDisposition:mimeTypeOverrides');
-      const contentDispositionSettings: Record<string, 'inline' | 'attachment'> = currentDispositionSettings;
-
-      return res.apiv3({ contentDispositionSettings });
-    }
-    catch (err) {
-      logger.error('Error retrieving content disposition settings:', err);
-      return res.apiv3Err(new ErrorV3('Failed to retrieve content disposition settings', 'get-content-disposition-failed'));
-    }
-  });
-
-  /**
- * @swagger
- *
  * /content-disposition-settings/strict:
  *   put:
  *     tags: [Content-Disposition Settings]
@@ -79,14 +42,24 @@ module.exports = (crowi) => {
  *
  */
   router.put(
-    '/strict',
+    '/update',
     loginRequiredStrictly,
     adminRequired,
     addActivity,
     async(req, res) => {
 
       try {
-        await configManager.updateConfigs({ 'attachments:contentDisposition:mimeTypeOverrides': strictMimeTypeSettings });
+        const { newInlineMimeTypes } = req.body;
+
+        const currentSettings = await configManager.getConfig('attachments:contentDisposition:mimeTypeOverrides');
+        const currentInlineMimeTypes = currentSettings.inlineMimeTypes || [];
+
+        const updatedInlineMimeTypes = Array.from(new Set([
+          ...currentInlineMimeTypes,
+          ...newInlineMimeTypes,
+        ]));
+
+        await configManager.updateConfigs({ 'attachments:contentDisposition:mimeTypeOverrides': { inlineMimeTypes: updatedInlineMimeTypes } });
 
         const parameters = {
           action: SupportedAction.ACTION_ADMIN_ATTACHMENT_DISPOSITION_UPDATE,
@@ -95,7 +68,7 @@ module.exports = (crowi) => {
         };
         activityEvent.emit('update', res.locals.activity._id, parameters);
 
-        return res.apiv3({ currentMode: 'strict', contentDispositionSettings: strictMimeTypeSettings });
+        return res.apiv3({ currentMode: 'custom', contentDispositionSettings: strictMimeTypeSettings });
       }
       catch (err) {
         const msg = 'Error occurred in updating content disposition for MIME types';
@@ -110,15 +83,15 @@ module.exports = (crowi) => {
   /**
  * @swagger
  *
- * /content-disposition-settings/lax:
- *   put:
+ * /content-disposition-settings:
+ *   get:
  *     tags: [Content-Disposition Settings]
- *     summary: Set content disposition settings for configurable MIME types to lax.
+ *     summary: Get content disposition settings for configurable MIME types
  *     security:
  *       - cookieAuth: []
  *     responses:
  *       200:
- *         description: Successfully set lax content disposition settings.
+ *         description: Successfully retrieved content disposition settings.
  *         content:
  *           application/json:
  *             schema:
@@ -131,34 +104,18 @@ module.exports = (crowi) => {
  *                     description: inline or attachment
  *
  */
-  router.put(
-    '/lax',
-    loginRequiredStrictly,
-    adminRequired,
-    addActivity,
-    async(req, res) => {
-
-      try {
-        await configManager.updateConfigs({ 'attachments:contentDisposition:mimeTypeOverrides': laxMimeTypeSettings });
-
-        const parameters = {
-          action: SupportedAction.ACTION_ADMIN_ATTACHMENT_DISPOSITION_UPDATE,
-          contentDispositionSettings: laxMimeTypeSettings,
-          currentMode: 'lax',
-        };
-        activityEvent.emit('update', res.locals.activity._id, parameters);
+  router.get('/', loginRequiredStrictly, adminRequired, async(req, res) => {
+    try {
+      const currentDispositionSettings = configManager.getConfig('attachments:contentDisposition:mimeTypeOverrides');
+      const contentDispositionSettings: Record<string, 'inline' | 'attachment'> = currentDispositionSettings;
 
-        return res.apiv3({ currentMode: 'lax', contentDispositionSettings: laxMimeTypeSettings });
-      }
-      catch (err) {
-        const msg = 'Error occurred in updating content disposition for MIME types';
-        logger.error(msg, err);
-        return res.apiv3Err(
-          new ErrorV3(msg, 'update-content-disposition-failed'),
-        );
-      }
-    },
-  );
+      return res.apiv3({ contentDispositionSettings });
+    }
+    catch (err) {
+      logger.error('Error retrieving content disposition settings:', err);
+      return res.apiv3Err(new ErrorV3('Failed to retrieve content disposition settings', 'get-content-disposition-failed'));
+    }
+  });
 
   return router;
 };

+ 2 - 25
apps/app/src/server/service/config-manager/config-definition.ts

@@ -542,32 +542,9 @@ export const CONFIG_DEFINITIONS = {
   }),
 
   // Attachment Content-Disposition settings
-  'attachments:contentDisposition:mimeTypeOverrides': defineConfig<Record<string, 'inline' | 'attachment'>>({
+  'attachments:contentDisposition:mimeTypeOverrides': defineConfig<{ inlineMimeTypes: string[]; }>({
     defaultValue: {
-      // Documents
-      'application/pdf': 'attachment',
-      'application/json': 'attachment',
-      'text/plain': 'attachment',
-      'text/csv': 'attachment',
-      'text/html': 'attachment',
-
-      // Images
-      'image/jpeg': 'attachment',
-      'image/png': 'attachment',
-      'image/gif': 'attachment',
-      'image/webp': 'attachment',
-      'image/svg+xml': 'attachment',
-
-      // Audio and Video
-      'audio/mpeg': 'attachment',
-      'video/mp4': 'attachment',
-      'video/webm': 'attachment',
-
-      // Fonts
-      'font/woff2': 'attachment',
-      'font/woff': 'attachment',
-      'font/ttf': 'attachment',
-      'font/otf': 'attachment',
+      inlineMimeTypes: [],
     },
   }),