2022-04-28 05:14:03 +03:00
|
|
|
import { reactive, ref } from 'vue';
|
2022-07-02 09:12:11 +03:00
|
|
|
import * as Misskey from 'misskey-js';
|
|
|
|
import { readAndCompressImage } from 'browser-image-resizer';
|
2022-04-28 05:14:03 +03:00
|
|
|
import { defaultStore } from '@/store';
|
|
|
|
import { apiUrl } from '@/config';
|
|
|
|
import { $i } from '@/account';
|
|
|
|
import { alert } from '@/os';
|
|
|
|
|
|
|
|
type Uploading = {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
progressMax: number | undefined;
|
|
|
|
progressValue: number | undefined;
|
|
|
|
img: string;
|
|
|
|
};
|
|
|
|
export const uploads = ref<Uploading[]>([]);
|
|
|
|
|
|
|
|
const compressTypeMap = {
|
|
|
|
'image/jpeg': { quality: 0.85, mimeType: 'image/jpeg' },
|
|
|
|
'image/webp': { quality: 0.85, mimeType: 'image/jpeg' },
|
|
|
|
'image/svg+xml': { quality: 1, mimeType: 'image/png' },
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
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 09:12:11 +03:00
|
|
|
keepOriginal: boolean = defaultStore.state.keepOriginalUploading,
|
2022-04-28 05:14:03 +03:00
|
|
|
): Promise<Misskey.entities.DriveFile> {
|
2022-05-07 08:19:15 +03:00
|
|
|
if (folder && typeof folder === 'object') folder = folder.id;
|
2022-04-28 05:14:03 +03:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const id = Math.random().toString();
|
|
|
|
|
|
|
|
const reader = new FileReader();
|
2022-05-07 08:19:15 +03:00
|
|
|
reader.onload = async (ev) => {
|
2022-04-28 05:14:03 +03:00
|
|
|
const ctx = reactive<Uploading>({
|
|
|
|
id: id,
|
|
|
|
name: name || file.name || 'untitled',
|
|
|
|
progressMax: undefined,
|
|
|
|
progressValue: undefined,
|
2022-07-02 09:12:11 +03:00
|
|
|
img: window.URL.createObjectURL(file),
|
2022-04-28 05:14:03 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
uploads.value.push(ctx);
|
|
|
|
|
|
|
|
let resizedImage: any;
|
|
|
|
if (!keepOriginal && file.type in compressTypeMap) {
|
|
|
|
const imgConfig = compressTypeMap[file.type];
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
maxWidth: 2048,
|
|
|
|
maxHeight: 2048,
|
|
|
|
debug: true,
|
|
|
|
...imgConfig,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
resizedImage = await readAndCompressImage(file, config);
|
|
|
|
ctx.name = file.type !== imgConfig.mimeType ? `${ctx.name}.${mimeTypeMap[compressTypeMap[file.type].mimeType]}` : ctx.name;
|
2022-05-07 08:19:15 +03:00
|
|
|
} catch (err) {
|
|
|
|
console.error('Failed to resize image', err);
|
2022-04-28 05:14:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-07 08:19:15 +03:00
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('i', $i.token);
|
|
|
|
formData.append('force', 'true');
|
|
|
|
formData.append('file', resizedImage || file);
|
|
|
|
formData.append('name', ctx.name);
|
|
|
|
if (folder) formData.append('folderId', folder);
|
2022-04-28 05:14:03 +03:00
|
|
|
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('POST', apiUrl + '/drive/files/create', true);
|
|
|
|
xhr.onload = (ev) => {
|
|
|
|
if (xhr.status !== 200 || ev.target == null || ev.target.response == null) {
|
|
|
|
// TODO: 消すのではなくて再送できるようにしたい
|
2022-05-07 08:19:15 +03:00
|
|
|
uploads.value = uploads.value.filter(x => x.id !== id);
|
2022-04-28 05:14:03 +03:00
|
|
|
|
|
|
|
alert({
|
|
|
|
type: 'error',
|
|
|
|
title: 'Failed to upload',
|
2022-07-02 09:12:11 +03:00
|
|
|
text: `${JSON.stringify(ev.target?.response)}, ${JSON.stringify(xhr.response)}`,
|
2022-04-28 05:14:03 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
reject();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const driveFile = JSON.parse(ev.target.response);
|
|
|
|
|
|
|
|
resolve(driveFile);
|
|
|
|
|
2022-05-07 08:19:15 +03:00
|
|
|
uploads.value = uploads.value.filter(x => x.id !== id);
|
2022-04-28 05:14:03 +03:00
|
|
|
};
|
|
|
|
|
2022-05-07 08:19:15 +03:00
|
|
|
xhr.upload.onprogress = ev => {
|
|
|
|
if (ev.lengthComputable) {
|
|
|
|
ctx.progressMax = ev.total;
|
|
|
|
ctx.progressValue = ev.loaded;
|
2022-04-28 05:14:03 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-07 08:19:15 +03:00
|
|
|
xhr.send(formData);
|
2022-04-28 05:14:03 +03:00
|
|
|
};
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
});
|
|
|
|
}
|