Explorar el Código

Update file-type dependency and move it to its correct package.json

Refs:
- https://github.com/sindresorhus/file-type/releases/tag/v17.0.0

Went through the release notes since v16 to check that none of the MIME types we
care about have changed.
Manav Rathi hace 1 año
padre
commit
38e18f29bd

+ 3 - 18
web/apps/cast/src/services/detect-type.ts

@@ -1,7 +1,6 @@
 import { KnownFileTypeInfos } from "@/media/file-type";
 import { nameAndExtension } from "@/next/file";
-import log from "@/next/log";
-import FileType from "file-type";
+import { fileTypeFromBlob } from "file-type";
 
 /**
  * Try to deduce the MIME type for the given {@link file}. Return the MIME type
@@ -11,22 +10,8 @@ import FileType from "file-type";
  * It first peeks into the file's initial contents to detect the MIME type. If
  * that doesn't give any results, it tries to deduce it from the file's name.
  */
-export const tryDetectMediaMIMEType = async (file: File): Promise<string> => {
-    const chunkSizeForTypeDetection = 4100;
-
-    let mime: string | undefined;
-    try {
-        const fileChunk = file.slice(0, chunkSizeForTypeDetection);
-        const chunkData = new Uint8Array(await fileChunk.arrayBuffer());
-        const result = await FileType.fromBuffer(chunkData);
-        mime = result?.mime;
-    } catch (e) {
-        log.error(
-            `Failed to deduce MIME type from file contents for ${file}, will try with the file name instead`,
-            e,
-        );
-    }
-
+export const detectMediaMIMEType = async (file: File): Promise<string> => {
+    const mime = (await fileTypeFromBlob(file))?.mime;
     if (mime) {
         if (mime.startsWith("image/") || mime.startsWith("video/")) return mime;
         else throw new Error(`Detected MIME type ${mime} is not a media file`);

+ 2 - 2
web/apps/cast/src/utils/file.ts

@@ -4,7 +4,7 @@ import log from "@/next/log";
 import ComlinkCryptoWorker from "@ente/shared/crypto";
 import { RAW_FORMATS } from "constants/upload";
 import CastDownloadManager from "services/castDownloadManager";
-import { tryDetectMediaMIMEType } from "services/detect-type";
+import { detectMediaMIMEType } from "services/detect-type";
 import {
     EncryptedEnteFile,
     EnteFile,
@@ -132,7 +132,7 @@ export const getPreviewableImage = async (
             );
             fileBlob = new Blob([imageData]);
         }
-        const mimeType = await tryDetectMediaMIMEType(
+        const mimeType = await detectMediaMIMEType(
             new File([fileBlob], file.metadata.title),
         );
         if (!mimeType) return undefined;

+ 0 - 1
web/apps/photos/package.json

@@ -21,7 +21,6 @@
         "exifr": "^7.1.3",
         "fast-srp-hap": "^2.0.4",
         "ffmpeg-wasm": "file:./thirdparty/ffmpeg-wasm",
-        "file-type": "^16.5.4",
         "formik": "^2.1.5",
         "hdbscan": "0.0.1-alpha.5",
         "heic-convert": "^2.0.0",

+ 2 - 2
web/apps/photos/src/services/typeDetectionService.ts

@@ -7,7 +7,7 @@ import {
 import log from "@/next/log";
 import { ElectronFile } from "@/next/types/file";
 import { CustomError } from "@ente/shared/error";
-import FileType, { FileTypeResult } from "file-type";
+import { fileTypeFromBuffer, type FileTypeResult } from "file-type";
 import { getFileExtension } from "utils/file";
 import { getUint8ArrayView } from "./readerService";
 
@@ -83,7 +83,7 @@ async function extractElectronFileType(file: ElectronFile) {
 }
 
 async function getFileTypeFromBuffer(buffer: Uint8Array) {
-    const result = await FileType.fromBuffer(buffer);
+    const result = await fileTypeFromBuffer(buffer);
     if (!result?.mime) {
         let logableInfo = "";
         try {

+ 5 - 2
web/docs/dependencies.md

@@ -133,8 +133,11 @@ some cases.
 
 ## Media
 
--   "jszip" is used for reading zip files in JavaScript. Live photos are zip
-    files under the hood.
+-   "[jszip](https://github.com/Stuk/jszip)" is used for reading zip files in
+    JavaScript (Live photos are zip files under the hood).
+
+-   "[file-type](https://github.com/sindresorhus/file-type)" is used for MIME
+   type detection.
 
 ## Photos app specific
 

+ 1 - 0
web/packages/media/package.json

@@ -4,6 +4,7 @@
     "private": true,
     "dependencies": {
         "@/next": "*",
+        "file-type": "^19",
         "jszip": "^3.10"
     }
 }

+ 21 - 21
web/yarn.lock

@@ -2505,14 +2505,14 @@ file-selector@^0.4.0:
   dependencies:
     tslib "^2.0.3"
 
-file-type@^16.5.4:
-  version "16.5.4"
-  resolved "https://registry.yarnpkg.com/file-type/-/file-type-16.5.4.tgz#474fb4f704bee427681f98dd390058a172a6c2fd"
-  integrity sha512-/yFHK0aGjFEgDJjEKP0pWCplsPFPhwyfwevf/pVxiN0tmE4L9LmwWxWukdJSHdoCli4VgQLehjJtwQBnqmsKcw==
+file-type@^19:
+  version "19.0.0"
+  resolved "https://registry.yarnpkg.com/file-type/-/file-type-19.0.0.tgz#62a6cadc43f73ba38c53e1a174943a75fdafafa9"
+  integrity sha512-s7cxa7/leUWLiXO78DVVfBVse+milos9FitauDLG1pI7lNaJ2+5lzPnr2N24ym+84HVwJL6hVuGfgVE+ALvU8Q==
   dependencies:
-    readable-web-to-node-stream "^3.0.0"
-    strtok3 "^6.2.4"
-    token-types "^4.1.1"
+    readable-web-to-node-stream "^3.0.2"
+    strtok3 "^7.0.0"
+    token-types "^5.0.1"
 
 fill-range@^7.0.1:
   version "7.0.1"
@@ -3748,10 +3748,10 @@ path-type@^4.0.0:
   resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b"
   integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==
 
-peek-readable@^4.1.0:
-  version "4.1.0"
-  resolved "https://registry.yarnpkg.com/peek-readable/-/peek-readable-4.1.0.tgz#4ece1111bf5c2ad8867c314c81356847e8a62e72"
-  integrity sha512-ZI3LnwUv5nOGbQzD9c2iDG6toheuXSZP5esSHBjopsXH4dg19soufvpUGA3uohi5anFtGb2lhAVdHzH6R/Evvg==
+peek-readable@^5.0.0:
+  version "5.0.0"
+  resolved "https://registry.yarnpkg.com/peek-readable/-/peek-readable-5.0.0.tgz#7ead2aff25dc40458c60347ea76cfdfd63efdfec"
+  integrity sha512-YtCKvLUOvwtMGmrniQPdO7MwPjgkFBtFIrmfSbYmYuq3tKDV/mcfAhBth1+C3ru7uXIZasc/pHnb+YDYNkkj4A==
 
 "photoswipe@file:./apps/photos/thirdparty/photoswipe":
   version "4.1.6"
@@ -3994,7 +3994,7 @@ readable-stream@~2.3.6:
     string_decoder "~1.1.1"
     util-deprecate "~1.0.1"
 
-readable-web-to-node-stream@^3.0.0:
+readable-web-to-node-stream@^3.0.2:
   version "3.0.2"
   resolved "https://registry.yarnpkg.com/readable-web-to-node-stream/-/readable-web-to-node-stream-3.0.2.tgz#5d52bb5df7b54861fd48d015e93a2cb87b3ee0bb"
   integrity sha512-ePeK6cc1EcKLEhJFt/AebMCLL+GgSKhuygrZ/GLaKZYEecIgIECf4UaUuaByiGtzckwR4ain9VzUh95T1exYGw==
@@ -4451,13 +4451,13 @@ strip-json-comments@^3.1.1:
   resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006"
   integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==
 
-strtok3@^6.2.4:
-  version "6.3.0"
-  resolved "https://registry.yarnpkg.com/strtok3/-/strtok3-6.3.0.tgz#358b80ffe6d5d5620e19a073aa78ce947a90f9a0"
-  integrity sha512-fZtbhtvI9I48xDSywd/somNqgUHl2L2cstmXCCif0itOf96jeW18MBSyrLuNicYQVkvpOxkZtkzujiTJ9LW5Jw==
+strtok3@^7.0.0:
+  version "7.0.0"
+  resolved "https://registry.yarnpkg.com/strtok3/-/strtok3-7.0.0.tgz#868c428b4ade64a8fd8fee7364256001c1a4cbe5"
+  integrity sha512-pQ+V+nYQdC5H3Q7qBZAz/MO6lwGhoC2gOAjuouGf/VO0m7vQRh8QNMl2Uf6SwAtzZ9bOw3UIeBukEGNJl5dtXQ==
   dependencies:
     "@tokenizer/token" "^0.3.0"
-    peek-readable "^4.1.0"
+    peek-readable "^5.0.0"
 
 styled-jsx@5.1.1:
   version "5.1.1"
@@ -4530,10 +4530,10 @@ to-regex-range@^5.0.1:
   dependencies:
     is-number "^7.0.0"
 
-token-types@^4.1.1:
-  version "4.2.1"
-  resolved "https://registry.yarnpkg.com/token-types/-/token-types-4.2.1.tgz#0f897f03665846982806e138977dbe72d44df753"
-  integrity sha512-6udB24Q737UD/SDsKAHI9FCRP7Bqc9D/MQUV02ORQg5iskjtLJlZJNdN4kKtcdtwCeWIwIHDGaUsTsCCAa8sFQ==
+token-types@^5.0.1:
+  version "5.0.1"
+  resolved "https://registry.yarnpkg.com/token-types/-/token-types-5.0.1.tgz#aa9d9e6b23c420a675e55413b180635b86a093b4"
+  integrity sha512-Y2fmSnZjQdDb9W4w4r1tswlMHylzWIeOKpx0aZH9BgGtACHhrk3OkT52AzwcuqTRBZtvvnTjDBh8eynMulu8Vg==
   dependencies:
     "@tokenizer/token" "^0.3.0"
     ieee754 "^1.2.1"