Explorar o código

Add Avif to content type detection

Gaël Métais hai 1 ano
pai
achega
0cde5a3607

+ 22 - 53
lib/tools/redownload/contentTypeChecker.js

@@ -1,20 +1,12 @@
-var debug   = require('debug')('ylt:contentTypeChecker');
-var Q       = require('q');
-var isJpg   = require('is-jpg');
-var isPng   = require('is-png');
-var isSvg   = require('is-svg');
-var isGif   = require('is-gif');
-var isWebp  = require('is-webp');
-var isWoff  = require('is-woff');
-var isWoff2 = require('is-woff2');
-var isOtf   = require('is-otf');
-var isTtf   = require('is-ttf');
-var isEot   = require('is-eot');
-var isJson  = require('is-json');
+var debug       = require('debug')('ylt:contentTypeChecker');
+var Q           = require('q');
+var FileType    = require('file-type');
+var isSvg       = require('is-svg');
+var isJson      = require('is-json');
 
 var ContentTypeChecker = function() {
 
-    function checkContentType(entry) {
+    async function checkContentType(entry) {
         var deferred = Q.defer();
         
         // Setting isSomething values:
@@ -55,7 +47,7 @@ var ContentTypeChecker = function() {
             var foundType;
 
             try {
-                foundType = findContentType(entry.weightCheck.bodyBuffer);
+                foundType = await findContentType(entry.weightCheck.bodyBuffer);
 
                 // If it's an image or a font, then rewrite.
                 if (foundType !== null && (foundType.type === 'image' || foundType.type === 'webfont' || foundType.type === 'json')) {
@@ -76,54 +68,23 @@ var ContentTypeChecker = function() {
         return deferred.promise;
     }
 
-    function findContentType(bodyBuffer) {
+    async function findContentType(bodyBuffer) {
         var bodyStr = bodyBuffer.toString();
 
-        if (isJpg(bodyBuffer)) {
-            return contentTypes.jpeg;
-        }
-
-        if (isPng(bodyBuffer)) {
-            return contentTypes.png;
-        }
-
         // https://github.com/sindresorhus/is-svg/issues/7
         if (/<svg/.test(bodyStr) && isSvg(bodyStr)) {
             return contentTypes.svg;
         }
 
-        if (isGif(bodyBuffer)) {
-            return contentTypes.gif;
-        }
-
-        if (isWebp(bodyBuffer)) {
-            return contentTypes.webp;
-        }
-
-        if (isWoff(bodyBuffer)) {
-            return contentTypes.woff;
-        }
-
-        if (isWoff2(bodyBuffer)) {
-            return contentTypes.woff2;
-        }
-
-        if (isOtf(bodyBuffer)) {
-            return contentTypes.otf;
-        }
-
-        if (isTtf(bodyBuffer)) {
-            return contentTypes.ttf;
-        }
-
-        if (isEot(bodyBuffer)) {
-            return contentTypes.eot;
-        }
-
         if (isJson(bodyStr)) {
             return contentTypes.json;
         }
 
+        const type = await FileType.fromBuffer(bodyBuffer);
+        if (type && type.ext && contentTypes[type.ext]) {
+            return contentTypes[type.ext];
+        }
+
         return null;
     }
 
@@ -146,7 +107,7 @@ var ContentTypeChecker = function() {
     }
 
     var contentTypes = {
-        jpeg: {
+        jpg: {
             type: 'image',
             mimes: ['image/jpeg'],
             updateFn: function(entry) {
@@ -187,6 +148,14 @@ var ContentTypeChecker = function() {
                 entry.isImage = true;
             }
         },
+        avif: {
+            type: 'image',
+            mimes: ['image/avif'],
+            updateFn: function(entry) {
+                entry.type = 'image';
+                entry.isImage = true;
+            }
+        },
         woff: {
             type: 'webfont',
             mimes: ['application/x-font-woff', 'application/font-woff', 'font/woff'],

+ 4 - 0
lib/tools/redownload/imageDimensions.js

@@ -43,6 +43,10 @@ var ImageDimensions = function() {
         return entry.isImage && entry.contentType === 'image/png';
     }
 
+    function isWebP(entry) {
+        return entry.isImage && entry.contentType === 'image/webp';
+    }
+
     return {
         getDimensions: getDimensions
     };

+ 1 - 9
package.json

@@ -26,6 +26,7 @@
     "css-mq-parser": "0.0.3",
     "debug": "4.3.4",
     "easyxml": "2.0.1",
+    "file-type": "16.5.3",
     "fontkit": "2.0.2",
     "html-minifier": "4.0.0",
     "image-size": "1.0.2",
@@ -34,17 +35,8 @@
     "imagemin-jpegtran": "7.0.0",
     "imagemin-optipng": "8.0.0",
     "imagemin-svgo": "9.0.0",
-    "is-eot": "1.0.0",
-    "is-gif": "3.0.0",
-    "is-jpg": "2.0.0",
     "is-json": "2.0.1",
-    "is-otf": "0.1.2",
-    "is-png": "1.1.0",
     "is-svg": "3.0.0",
-    "is-ttf": "0.2.2",
-    "is-webp": "1.0.1",
-    "is-woff": "1.0.3",
-    "is-woff2": "1.0.0",
     "md5": "2.3.0",
     "meow": "5.0.0",
     "parse-color": "1.0.0",

+ 5 - 5
test/core/contentTypeCheckerTest.js

@@ -10,11 +10,11 @@ describe('contentTypeChecker', function() {
     var svgImageContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
     var cssFileContent = fs.readFileSync(path.resolve(__dirname, '../www/unminified-stylesheet.css'));
     
-    it('detect the right content type', function() {
-        contentTypeChecker.findContentType(jpgImageContent).mimes.should.deep.equal(['image/jpeg']);
-        contentTypeChecker.findContentType(pngImageContent).mimes.should.deep.equal(['image/png']);
-        contentTypeChecker.findContentType(svgImageContent).mimes.should.deep.equal(['image/svg+xml']);
-        should.equal(contentTypeChecker.findContentType(cssFileContent), null);
+    it('detect the right content type', async function() {
+        (await contentTypeChecker.findContentType(jpgImageContent)).mimes.should.deep.equal(['image/jpeg']);
+        (await contentTypeChecker.findContentType(pngImageContent)).mimes.should.deep.equal(['image/png']);
+        (await contentTypeChecker.findContentType(svgImageContent)).mimes.should.deep.equal(['image/svg+xml']);
+        should.equal(await contentTypeChecker.findContentType(cssFileContent), null);
     });
 
 });