浏览代码

strip metadata from thumbnails

isundil 1 年之前
父节点
当前提交
6cd545175a
共有 4 个文件被更改,包括 17 次插入13 次删除
  1. 0 1
      router/api.js
  2. 1 1
      src/fileTypeManager.js
  3. 6 4
      src/filetype/imagemagick.js
  4. 10 7
      src/filetype/sharp.js

+ 0 - 1
router/api.js

@@ -127,7 +127,6 @@ module.exports = { register: app => {
             }
             if (!thumbnail)
                 return app.routerUtils.onPageNotFound(res);
-            //if (data.accessType === ACCESS_GRANT.readNoMeta) -> trim metadata
             res.setHeader("Content-Type", "image/jpeg");
             res.setHeader("Content-Length", fs.statSync(thumbnail.name)?.size || undefined);
             res.setHeader("Cache-Control", "private, max-age=2630000"); // 1 month cache

+ 1 - 1
src/fileTypeManager.js

@@ -8,7 +8,7 @@ const parsers = [
 module.exports.createThumbnail = async function(fileObj, w, h, quality) {
     for (let i of parsers)
     {
-        let result = await i.createThumbnail(fileObj, w, h, quality);
+        let result = await i.createThumbnail(fileObj, w, h, quality, false);
         if (result)
             return result;
     }

+ 6 - 4
src/filetype/imagemagick.js

@@ -119,7 +119,7 @@ module.exports.parse = async (fileObj, data) => {
     return { value: result };
 }
 
-module.exports.createThumbnail = async (fileObj, width, height, quality) => {
+module.exports.createThumbnail = async (fileObj, width, height, quality, keepMeta) => {
     if (!fileObj?.meta?.width || !fileObj?.meta?.height)
         return null;
     if (!width && !height)
@@ -127,10 +127,12 @@ module.exports.createThumbnail = async (fileObj, width, height, quality) => {
     let ratio = Math.min((width / fileObj.meta.width) || 1, (height / fileObj.meta.height) || 1, 1);
     const output = tmp.fileSync({ discardDescriptor: true });
     try {
-        await im.convert([
-            fileObj.path,
-            '-strip',
+        let args = [ fileObj.path ];
+        if (keepMeta !== true)
+            args.push('-strip');
+        await im.convert([...args,
             '-interlace', 'Plane',
+            '-auto-orient',
             '-resize', (Math.floor(fileObj.meta.width * ratio)),
             '-quality', '' + (Math.floor(Math.max(0, Math.min(100, (quality *10))))) + '%',
             '-sampling-factor', '4:2:0',

+ 10 - 7
src/filetype/sharp.js

@@ -15,18 +15,21 @@ class SharpWrapper {
         );
     }
 
-    async convert(fileObj, width, height, quality) {
+    async convert(fileObj, width, height, quality, keepMeta) {
         const output = tmp.fileSync({ discardDescriptor: true });
 
         let result = null;
         try {
-            result = await this.#threadPool.pushTask(() =>
-                new Sharp(fileObj.path)
-                    .resize(width, height, { fit: "inside", withoutEnlargement: true })
+            result = await this.#threadPool.pushTask(async () => {
+                let sharpInst = new Sharp(fileObj.path);
+                if (keepMeta)
+                    sharpInst = await sharpInst.keepMetadata();
+                return sharpInst
                     .rotate()
+                    .resize(width, height, { fit: "inside", withoutEnlargement: true })
                     .jpeg({ quality: quality *10 })
                     .toFile(output.name)
-            );
+            });
         } catch (err) {
             console.error("Sharp::createThumbnail: ", err);
             output.removeCallback();
@@ -84,7 +87,7 @@ module.exports.parse = async (fileObj) => {
     }
 }
 
-module.exports.createThumbnail = (fileObj, width, height, quality) => {
-    return sh.convert(fileObj, width, height, quality);
+module.exports.createThumbnail = (fileObj, width, height, quality, keepMeta) => {
+    return sh.convert(fileObj, width, height, quality, keepMeta);
 };