2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-28 04:14:03 +02:00
|
|
|
import { reactive, ref } from 'vue';
|
2022-07-02 08:12:11 +02:00
|
|
|
import * as Misskey from 'misskey-js';
|
|
|
|
import { readAndCompressImage } from 'browser-image-resizer';
|
2022-12-18 07:40:38 +01:00
|
|
|
import { getCompressionConfig } from './upload/compress-config';
|
2022-04-28 04:14:03 +02:00
|
|
|
import { defaultStore } from '@/store';
|
|
|
|
import { apiUrl } from '@/config';
|
|
|
|
import { $i } from '@/account';
|
|
|
|
import { alert } from '@/os';
|
2022-07-07 14:06:37 +02:00
|
|
|
import { i18n } from '@/i18n';
|
2022-04-28 04:14:03 +02:00
|
|
|
|
|
|
|
type Uploading = {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
progressMax: number | undefined;
|
|
|
|
progressValue: number | undefined;
|
|
|
|
img: string;
|
|
|
|
};
|
|
|
|
export const uploads = ref<Uploading[]>([]);
|
|
|
|
|
|
|
|
const mimeTypeMap = {
|
|
|
|
'image/webp': 'webp',
|
|
|
|
'image/jpeg': 'jpg',
|
|
|
|
'image/png': 'png',
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
export function uploadFile(
|
|
|
|
file: File,
|
|
|
|
folder?: any,
|
|
|
|
name?: string,
|
2022-07-02 08:12:11 +02:00
|
|
|
keepOriginal: boolean = defaultStore.state.keepOriginalUploading,
|
2022-04-28 04:14:03 +02:00
|
|
|
): Promise<Misskey.entities.DriveFile> {
|
2022-12-18 07:40:38 +01:00
|
|
|
if ($i == null) throw new Error('Not logged in');
|
|
|
|
|
2022-05-07 07:19:15 +02:00
|
|
|
if (folder && typeof folder === 'object') folder = folder.id;
|
2022-04-28 04:14:03 +02:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const id = Math.random().toString();
|
|
|
|
|
|
|
|
const reader = new FileReader();
|
2022-12-18 07:40:38 +01:00
|
|
|
reader.onload = async (): Promise<void> => {
|
2022-04-28 04:14:03 +02:00
|
|
|
const ctx = reactive<Uploading>({
|
|
|
|
id: id,
|
2022-12-18 07:40:38 +01:00
|
|
|
name: name ?? file.name ?? 'untitled',
|
2022-04-28 04:14:03 +02:00
|
|
|
progressMax: undefined,
|
|
|
|
progressValue: undefined,
|
2022-07-02 08:12:11 +02:00
|
|
|
img: window.URL.createObjectURL(file),
|
2022-04-28 04:14:03 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
uploads.value.push(ctx);
|
|
|
|
|
2022-12-18 07:40:38 +01:00
|
|
|
const config = !keepOriginal ? await getCompressionConfig(file) : undefined;
|
|
|
|
let resizedImage: Blob | undefined;
|
|
|
|
if (config) {
|
2022-04-28 04:14:03 +02:00
|
|
|
try {
|
2022-12-18 07:40:38 +01:00
|
|
|
const resized = await readAndCompressImage(file, config);
|
|
|
|
if (resized.size < file.size || file.type === 'image/webp') {
|
|
|
|
// The compression may not always reduce the file size
|
|
|
|
// (and WebP is not browser safe yet)
|
|
|
|
resizedImage = resized;
|
|
|
|
}
|
|
|
|
if (_DEV_) {
|
|
|
|
const saved = ((1 - resized.size / file.size) * 100).toFixed(2);
|
|
|
|
console.log(`Image compression: before ${file.size} bytes, after ${resized.size} bytes, saved ${saved}%`);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.name = file.type !== config.mimeType ? `${ctx.name}.${mimeTypeMap[config.mimeType]}` : ctx.name;
|
2022-05-07 07:19:15 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error('Failed to resize image', err);
|
2022-04-28 04:14:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-07 07:19:15 +02:00
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('i', $i.token);
|
|
|
|
formData.append('force', 'true');
|
2022-12-18 07:40:38 +01:00
|
|
|
formData.append('file', resizedImage ?? file);
|
2022-05-07 07:19:15 +02:00
|
|
|
formData.append('name', ctx.name);
|
|
|
|
if (folder) formData.append('folderId', folder);
|
2022-04-28 04:14:03 +02:00
|
|
|
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('POST', apiUrl + '/drive/files/create', true);
|
2022-12-18 07:40:38 +01:00
|
|
|
xhr.onload = ((ev: ProgressEvent<XMLHttpRequest>) => {
|
2022-04-28 04:14:03 +02:00
|
|
|
if (xhr.status !== 200 || ev.target == null || ev.target.response == null) {
|
2022-07-07 14:06:37 +02:00
|
|
|
// TODO: 消すのではなくて(ネットワーク的なエラーなら)再送できるようにしたい
|
2022-05-07 07:19:15 +02:00
|
|
|
uploads.value = uploads.value.filter(x => x.id !== id);
|
2022-04-28 04:14:03 +02:00
|
|
|
|
2023-04-22 10:18:57 +02:00
|
|
|
if (xhr.status === 413) {
|
|
|
|
alert({
|
|
|
|
type: 'error',
|
|
|
|
title: i18n.ts.failedToUpload,
|
|
|
|
text: i18n.ts.cannotUploadBecauseExceedsFileSizeLimit,
|
|
|
|
});
|
|
|
|
} else if (ev.target?.response) {
|
2022-07-07 14:06:37 +02:00
|
|
|
const res = JSON.parse(ev.target.response);
|
|
|
|
if (res.error?.id === 'bec5bd69-fba3-43c9-b4fb-2894b66ad5d2') {
|
|
|
|
alert({
|
|
|
|
type: 'error',
|
|
|
|
title: i18n.ts.failedToUpload,
|
|
|
|
text: i18n.ts.cannotUploadBecauseInappropriate,
|
|
|
|
});
|
|
|
|
} else if (res.error?.id === 'd08dbc37-a6a9-463a-8c47-96c32ab5f064') {
|
|
|
|
alert({
|
|
|
|
type: 'error',
|
|
|
|
title: i18n.ts.failedToUpload,
|
|
|
|
text: i18n.ts.cannotUploadBecauseNoFreeSpace,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
alert({
|
|
|
|
type: 'error',
|
|
|
|
title: i18n.ts.failedToUpload,
|
|
|
|
text: `${res.error?.message}\n${res.error?.code}\n${res.error?.id}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
alert({
|
|
|
|
type: 'error',
|
|
|
|
title: 'Failed to upload',
|
|
|
|
text: `${JSON.stringify(ev.target?.response)}, ${JSON.stringify(xhr.response)}`,
|
|
|
|
});
|
|
|
|
}
|
2022-04-28 04:14:03 +02:00
|
|
|
|
|
|
|
reject();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const driveFile = JSON.parse(ev.target.response);
|
|
|
|
|
|
|
|
resolve(driveFile);
|
|
|
|
|
2022-05-07 07:19:15 +02:00
|
|
|
uploads.value = uploads.value.filter(x => x.id !== id);
|
2022-12-18 07:40:38 +01:00
|
|
|
}) as (ev: ProgressEvent<EventTarget>) => any;
|
2022-04-28 04:14:03 +02:00
|
|
|
|
2022-05-07 07:19:15 +02:00
|
|
|
xhr.upload.onprogress = ev => {
|
|
|
|
if (ev.lengthComputable) {
|
|
|
|
ctx.progressMax = ev.total;
|
|
|
|
ctx.progressValue = ev.loaded;
|
2022-04-28 04:14:03 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-07 07:19:15 +02:00
|
|
|
xhr.send(formData);
|
2022-04-28 04:14:03 +02:00
|
|
|
};
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
});
|
|
|
|
}
|