From b3a6805b5ee051aa76ddbdcfa93f296dabae760c Mon Sep 17 00:00:00 2001 From: tamaina Date: Mon, 7 Aug 2023 15:25:35 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9C=AA=E7=9F=A5=E3=81=AE=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=82=A4=E3=83=AB=E5=BD=A2=E5=BC=8F=E3=81=8B=E3=81=A4=E6=8B=A1?= =?UTF-8?q?=E5=BC=B5=E5=AD=90=E3=81=8C=E3=81=82=E3=82=8B=E5=A0=B4=E5=90=88?= =?UTF-8?q?=E3=81=AF=E4=BD=95=E3=82=82=E3=81=97=E3=81=AA=E3=81=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/backend/package.json | 1 - packages/backend/src/misc/correct-filename.ts | 25 ++++++++----------- .../test/unit/misc/correct-filename.ts | 3 +++ pnpm-lock.yaml | 17 +------------ 4 files changed, 15 insertions(+), 31 deletions(-) diff --git a/packages/backend/package.json b/packages/backend/package.json index a8ec49d196..8c5b67ced6 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -154,7 +154,6 @@ "stringz": "2.1.0", "summaly": "github:misskey-dev/summaly", "systeminformation": "5.18.9", - "text-extensions": "^2.4.0", "tinycolor2": "1.6.0", "tmp": "0.2.1", "tsc-alias": "1.8.7", diff --git a/packages/backend/src/misc/correct-filename.ts b/packages/backend/src/misc/correct-filename.ts index b4d2611062..9a1298b957 100644 --- a/packages/backend/src/misc/correct-filename.ts +++ b/packages/backend/src/misc/correct-filename.ts @@ -3,12 +3,9 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import textExts from 'text-extensions'; - /** * Array.includes()よりSet.has()の方が高速 */ - const targetExtsToSkip = new Set([ 'gz', 'tar', @@ -19,8 +16,6 @@ const targetExtsToSkip = new Set([ '7z', ]); -const sourceExtsToSkip = new Set(textExts); - const extRegExp = /\.([0-9a-zA-Z]+$)/i; /** @@ -34,26 +29,28 @@ export function correctFilename(filename: string, ext: string | null) { const match = extRegExp.exec(filename); if (!match || !match[1]) { + // filenameが拡張子を持っていない場合は拡張子をつける return `${filename}${dotExt}`; } - const filenameExt = match[1].toLowerCase(); - if (filenameExt === ext) { - return filename; - } + const filenameExt = match[1].toLowerCase(); if ( + // 未知のファイル形式かつ拡張子がある場合は何もしない + ext === null || + // 拡張子が一致している場合は何もしない + filenameExt === ext || + + // jpeg, tiffを同一視 ext === 'jpg' && filenameExt === 'jpeg' || ext === 'tif' && filenameExt === 'tiff' || // 圧縮形式っぽければ下手に拡張子を変えない // https://github.com/misskey-dev/misskey/issues/11482 - ext !== null && targetExtsToSkip.has(ext) || - - // テキストファイル?(ext === null)かつ拡張子がテキストファイルっぽい場合は - // 拡張子を変えない - ext === null && sourceExtsToSkip.has(filenameExt) + targetExtsToSkip.has(ext) ) { return filename; } + + // 拡張子があるが一致していないなどの場合は拡張子を付け足す return `${filename}${dotExt}`; } diff --git a/packages/backend/test/unit/misc/correct-filename.ts b/packages/backend/test/unit/misc/correct-filename.ts index 42d3f3e85f..77498ec3dc 100644 --- a/packages/backend/test/unit/misc/correct-filename.ts +++ b/packages/backend/test/unit/misc/correct-filename.ts @@ -36,4 +36,7 @@ describe(correctFilename, () => { it('skip text file', () => { expect(correctFilename('test.txt', null)).toBe('test.txt'); }); + it('unknown', () => { + expect(correctFilename('test.hoge', null)).toBe('test.hoge'); + }); }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 441367fa01..1a559e867e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -362,9 +362,6 @@ importers: systeminformation: specifier: 5.18.9 version: 5.18.9 - text-extensions: - specifier: ^2.4.0 - version: 2.4.0 tinycolor2: specifier: 1.6.0 version: 1.6.0 @@ -5348,7 +5345,7 @@ packages: hasBin: true requiresBuild: true dependencies: - detect-libc: 2.0.1 + detect-libc: 2.0.2 https-proxy-agent: 5.0.1 make-dir: 3.1.0 node-fetch: 2.6.11 @@ -11499,13 +11496,6 @@ packages: engines: {node: '>=8'} dev: true - /detect-libc@2.0.1: - resolution: {integrity: sha512-463v3ZeIrcWtdgIg6vI6XUncguvr2TnGl4SzDXinkt9mSLpBJKXT3mW6xT3VQdDN11+WVs29pgvivTc4Lp8v+w==} - engines: {node: '>=8'} - requiresBuild: true - dev: false - optional: true - /detect-libc@2.0.2: resolution: {integrity: sha512-UX6sGumvvqSaXgdKGUsgZWqcUyIXZ/vZTrlRT/iobiKhGL0zL4d3osHj3uqllWJK+i+sixDS/3COVEOFbupFyw==} engines: {node: '>=8'} @@ -20431,11 +20421,6 @@ packages: resolution: {integrity: sha512-/0TJD42KDnVwKmDK6jj3xP7E2MG7SHAOG4tyTgyUCRPdHwvkquYNLEQltmdMa3owq3TkddCVcTsoctJI8VQNKA==} dev: false - /text-extensions@2.4.0: - resolution: {integrity: sha512-te/NtwBwfiNRLf9Ijqx3T0nlqZiQ2XrrtBvu+cLL8ZRrGkO0NHTug8MYFKyoSrv/sHTaSKfilUkizV6XhxMJ3g==} - engines: {node: '>=8'} - dev: false - /text-table@0.2.0: resolution: {integrity: sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw==} dev: true