This commit is contained in:
parent
b3a6805b5e
commit
2400e23826
|
|
@ -7,16 +7,16 @@
|
||||||
* Array.includes()よりSet.has()の方が高速
|
* Array.includes()よりSet.has()の方が高速
|
||||||
*/
|
*/
|
||||||
const targetExtsToSkip = new Set([
|
const targetExtsToSkip = new Set([
|
||||||
'gz',
|
'.gz',
|
||||||
'tar',
|
'.tar',
|
||||||
'tgz',
|
'.tgz',
|
||||||
'bz2',
|
'.bz2',
|
||||||
'xz',
|
'.xz',
|
||||||
'zip',
|
'.zip',
|
||||||
'7z',
|
'.7z',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const extRegExp = /\.([0-9a-zA-Z]+$)/i;
|
const extRegExp = /\.[0-9a-zA-Z]+$/i;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 与えられた拡張子とファイル名が一致しているかどうかを確認し、
|
* 与えられた拡張子とファイル名が一致しているかどうかを確認し、
|
||||||
|
|
@ -25,28 +25,28 @@ const extRegExp = /\.([0-9a-zA-Z]+$)/i;
|
||||||
* extはfile-typeのextを想定
|
* extはfile-typeのextを想定
|
||||||
*/
|
*/
|
||||||
export function correctFilename(filename: string, ext: string | null) {
|
export function correctFilename(filename: string, ext: string | null) {
|
||||||
const dotExt = ext ? ext.startsWith('.') ? ext : `.${ext}` : '.unknown';
|
const dotExt = ext ? ext[0] === '.' ? ext : `.${ext}` : '.unknown';
|
||||||
|
|
||||||
const match = extRegExp.exec(filename);
|
const match = extRegExp.exec(filename);
|
||||||
if (!match || !match[1]) {
|
if (!match || !match[0]) {
|
||||||
// filenameが拡張子を持っていない場合は拡張子をつける
|
// filenameが拡張子を持っていない場合は拡張子をつける
|
||||||
return `${filename}${dotExt}`;
|
return `${filename}${dotExt}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const filenameExt = match[1].toLowerCase();
|
const filenameExt = match[0].toLowerCase();
|
||||||
if (
|
if (
|
||||||
// 未知のファイル形式かつ拡張子がある場合は何もしない
|
// 未知のファイル形式かつ拡張子がある場合は何もしない
|
||||||
ext === null ||
|
ext === null ||
|
||||||
// 拡張子が一致している場合は何もしない
|
// 拡張子が一致している場合は何もしない
|
||||||
filenameExt === ext ||
|
filenameExt === dotExt ||
|
||||||
|
|
||||||
// jpeg, tiffを同一視
|
// jpeg, tiffを同一視
|
||||||
ext === 'jpg' && filenameExt === 'jpeg' ||
|
dotExt === '.jpg' && filenameExt === '.jpeg' ||
|
||||||
ext === 'tif' && filenameExt === 'tiff' ||
|
dotExt === '.tif' && filenameExt === '.tiff' ||
|
||||||
|
|
||||||
// 圧縮形式っぽければ下手に拡張子を変えない
|
// 圧縮形式っぽければ下手に拡張子を変えない
|
||||||
// https://github.com/misskey-dev/misskey/issues/11482
|
// https://github.com/misskey-dev/misskey/issues/11482
|
||||||
targetExtsToSkip.has(ext)
|
targetExtsToSkip.has(dotExt)
|
||||||
) {
|
) {
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,4 +39,7 @@ describe(correctFilename, () => {
|
||||||
it('unknown', () => {
|
it('unknown', () => {
|
||||||
expect(correctFilename('test.hoge', null)).toBe('test.hoge');
|
expect(correctFilename('test.hoge', null)).toBe('test.hoge');
|
||||||
});
|
});
|
||||||
|
test('non ascii with space', () => {
|
||||||
|
expect(correctFilename('ファイル 名前', 'jpg')).toBe('ファイル 名前.jpg');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@
|
||||||
|
|
||||||
import { describe, test, expect } from '@jest/globals';
|
import { describe, test, expect } from '@jest/globals';
|
||||||
import { contentDisposition } from '@/misc/content-disposition.js';
|
import { contentDisposition } from '@/misc/content-disposition.js';
|
||||||
import { correctFilename } from '@/misc/correct-filename.js';
|
|
||||||
|
|
||||||
describe('misc:content-disposition', () => {
|
describe('misc:content-disposition', () => {
|
||||||
test('inline', () => {
|
test('inline', () => {
|
||||||
|
|
@ -18,30 +17,3 @@ describe('misc:content-disposition', () => {
|
||||||
expect(contentDisposition('attachment', 'ファイル名')).toBe('attachment; filename=\"_____\"; filename*=UTF-8\'\'%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E5%90%8D');
|
expect(contentDisposition('attachment', 'ファイル名')).toBe('attachment; filename=\"_____\"; filename*=UTF-8\'\'%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E5%90%8D');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('misc:correct-filename', () => {
|
|
||||||
test('simple', () => {
|
|
||||||
expect(correctFilename('filename', 'jpg')).toBe('filename.jpg');
|
|
||||||
});
|
|
||||||
test('with same ext', () => {
|
|
||||||
expect(correctFilename('filename.jpg', 'jpg')).toBe('filename.jpg');
|
|
||||||
});
|
|
||||||
test('.ext', () => {
|
|
||||||
expect(correctFilename('filename.jpg', '.jpg')).toBe('filename.jpg');
|
|
||||||
});
|
|
||||||
test('with different ext', () => {
|
|
||||||
expect(correctFilename('filename.webp', 'jpg')).toBe('filename.webp.jpg');
|
|
||||||
});
|
|
||||||
test('non ascii with space', () => {
|
|
||||||
expect(correctFilename('ファイル 名前', 'jpg')).toBe('ファイル 名前.jpg');
|
|
||||||
});
|
|
||||||
test('jpeg', () => {
|
|
||||||
expect(correctFilename('filename.jpeg', 'jpg')).toBe('filename.jpeg');
|
|
||||||
});
|
|
||||||
test('tiff', () => {
|
|
||||||
expect(correctFilename('filename.tiff', 'tif')).toBe('filename.tiff');
|
|
||||||
});
|
|
||||||
test('null ext', () => {
|
|
||||||
expect(correctFilename('filename', null)).toBe('filename.unknown');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue