Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ランダム文字列でアップロードする際に拡張子内にドットが含まれるものを正常にアップロードできるように #199

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Client
- Enhance: Unicode絵文字をslugから入力する際に`:ok:`のように最後の`:`を入力したあとにUnicode絵文字に変換できるように
- Fix: フォロー中のユーザーに関する"TLに他の人への返信を含める"の設定が分かりづらい問題を修正
- Fix: ランダム文字列でアップロードする際に拡張子内にドットが含まれるものを正常にアップロードできるように

## 2024.5.0 (merged to 2024.5.0-kinel.1)

Expand Down
28 changes: 27 additions & 1 deletion packages/frontend/src/scripts/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,32 @@ const mimeTypeMap = {
'image/png': 'png',
} as const;

// tar.gzなど、拡張子内にドットを2つまで許容するものはここに追加
const specialExtensions = [
'gz',
'bz2',
'xz',
'zst',
'lz',
'lz4',
'sz',
'z',
'zstd',
] as const;

function getExtension(filename: string): string {
const parts = filename.split('.');

if (parts.length <= 1) return '';

for (const ext of specialExtensions) {
if (parts[parts.length - 1] === ext && parts.length > 2) {
return '.' + parts[parts.length - 2] + '.' + parts[parts.length - 1];
}
}
return '.' + parts.pop();
}

export function uploadFile(
file: File,
folder?: any,
Expand All @@ -45,7 +71,7 @@ export function uploadFile(
const reader = new FileReader();
reader.onload = async (): Promise<void> => {
const filename = name ?? file.name ?? 'untitled';
const extension = filename.split('.').length > 1 ? '.' + filename.split('.').pop() : '';
const extension = getExtension(filename);

const ctx = reactive<Uploading>({
id,
Expand Down
Loading