未知のファイル形式かつ拡張子がある場合は何もしない

This commit is contained in:
tamaina 2023-08-07 15:25:35 +00:00
parent 2e2a001397
commit b3a6805b5e
4 changed files with 15 additions and 31 deletions

View File

@ -154,7 +154,6 @@
"stringz": "2.1.0", "stringz": "2.1.0",
"summaly": "github:misskey-dev/summaly", "summaly": "github:misskey-dev/summaly",
"systeminformation": "5.18.9", "systeminformation": "5.18.9",
"text-extensions": "^2.4.0",
"tinycolor2": "1.6.0", "tinycolor2": "1.6.0",
"tmp": "0.2.1", "tmp": "0.2.1",
"tsc-alias": "1.8.7", "tsc-alias": "1.8.7",

View File

@ -3,12 +3,9 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import textExts from 'text-extensions';
/** /**
* Array.includes()Set.has() * Array.includes()Set.has()
*/ */
const targetExtsToSkip = new Set([ const targetExtsToSkip = new Set([
'gz', 'gz',
'tar', 'tar',
@ -19,8 +16,6 @@ const targetExtsToSkip = new Set([
'7z', '7z',
]); ]);
const sourceExtsToSkip = new Set(textExts);
const extRegExp = /\.([0-9a-zA-Z]+$)/i; const extRegExp = /\.([0-9a-zA-Z]+$)/i;
/** /**
@ -34,26 +29,28 @@ export function correctFilename(filename: string, ext: string | null) {
const match = extRegExp.exec(filename); const match = extRegExp.exec(filename);
if (!match || !match[1]) { if (!match || !match[1]) {
// filenameが拡張子を持っていない場合は拡張子をつける
return `${filename}${dotExt}`; return `${filename}${dotExt}`;
} }
const filenameExt = match[1].toLowerCase();
if (filenameExt === ext) {
return filename;
}
const filenameExt = match[1].toLowerCase();
if ( if (
// 未知のファイル形式かつ拡張子がある場合は何もしない
ext === null ||
// 拡張子が一致している場合は何もしない
filenameExt === ext ||
// jpeg, tiffを同一視
ext === 'jpg' && filenameExt === 'jpeg' || ext === 'jpg' && filenameExt === 'jpeg' ||
ext === 'tif' && filenameExt === 'tiff' || ext === 'tif' && filenameExt === 'tiff' ||
// 圧縮形式っぽければ下手に拡張子を変えない // 圧縮形式っぽければ下手に拡張子を変えない
// https://github.com/misskey-dev/misskey/issues/11482 // https://github.com/misskey-dev/misskey/issues/11482
ext !== null && targetExtsToSkip.has(ext) || targetExtsToSkip.has(ext)
// テキストファイルext === nullかつ拡張子がテキストファイルっぽい場合は
// 拡張子を変えない
ext === null && sourceExtsToSkip.has(filenameExt)
) { ) {
return filename; return filename;
} }
// 拡張子があるが一致していないなどの場合は拡張子を付け足す
return `${filename}${dotExt}`; return `${filename}${dotExt}`;
} }

View File

@ -36,4 +36,7 @@ describe(correctFilename, () => {
it('skip text file', () => { it('skip text file', () => {
expect(correctFilename('test.txt', null)).toBe('test.txt'); expect(correctFilename('test.txt', null)).toBe('test.txt');
}); });
it('unknown', () => {
expect(correctFilename('test.hoge', null)).toBe('test.hoge');
});
}); });

View File

@ -362,9 +362,6 @@ importers:
systeminformation: systeminformation:
specifier: 5.18.9 specifier: 5.18.9
version: 5.18.9 version: 5.18.9
text-extensions:
specifier: ^2.4.0
version: 2.4.0
tinycolor2: tinycolor2:
specifier: 1.6.0 specifier: 1.6.0
version: 1.6.0 version: 1.6.0
@ -5348,7 +5345,7 @@ packages:
hasBin: true hasBin: true
requiresBuild: true requiresBuild: true
dependencies: dependencies:
detect-libc: 2.0.1 detect-libc: 2.0.2
https-proxy-agent: 5.0.1 https-proxy-agent: 5.0.1
make-dir: 3.1.0 make-dir: 3.1.0
node-fetch: 2.6.11 node-fetch: 2.6.11
@ -11499,13 +11496,6 @@ packages:
engines: {node: '>=8'} engines: {node: '>=8'}
dev: true 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: /detect-libc@2.0.2:
resolution: {integrity: sha512-UX6sGumvvqSaXgdKGUsgZWqcUyIXZ/vZTrlRT/iobiKhGL0zL4d3osHj3uqllWJK+i+sixDS/3COVEOFbupFyw==} resolution: {integrity: sha512-UX6sGumvvqSaXgdKGUsgZWqcUyIXZ/vZTrlRT/iobiKhGL0zL4d3osHj3uqllWJK+i+sixDS/3COVEOFbupFyw==}
engines: {node: '>=8'} engines: {node: '>=8'}
@ -20431,11 +20421,6 @@ packages:
resolution: {integrity: sha512-/0TJD42KDnVwKmDK6jj3xP7E2MG7SHAOG4tyTgyUCRPdHwvkquYNLEQltmdMa3owq3TkddCVcTsoctJI8VQNKA==} resolution: {integrity: sha512-/0TJD42KDnVwKmDK6jj3xP7E2MG7SHAOG4tyTgyUCRPdHwvkquYNLEQltmdMa3owq3TkddCVcTsoctJI8VQNKA==}
dev: false dev: false
/text-extensions@2.4.0:
resolution: {integrity: sha512-te/NtwBwfiNRLf9Ijqx3T0nlqZiQ2XrrtBvu+cLL8ZRrGkO0NHTug8MYFKyoSrv/sHTaSKfilUkizV6XhxMJ3g==}
engines: {node: '>=8'}
dev: false
/text-table@0.2.0: /text-table@0.2.0:
resolution: {integrity: sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw==} resolution: {integrity: sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw==}
dev: true dev: true