2022-01-30 07:11:52 +02:00
|
|
|
import { ref } from 'vue';
|
2021-11-11 19:02:25 +02:00
|
|
|
import * as os from '@/os';
|
2021-12-29 15:13:09 +02:00
|
|
|
import { stream } from '@/stream';
|
2021-11-11 19:02:25 +02:00
|
|
|
import { i18n } from '@/i18n';
|
|
|
|
import { defaultStore } from '@/store';
|
2021-12-09 18:22:22 +02:00
|
|
|
import { DriveFile } from 'misskey-js/built/entities';
|
2020-01-30 00:13:36 +02:00
|
|
|
|
2021-12-09 18:22:22 +02:00
|
|
|
function select(src: any, label: string | null, multiple: boolean): Promise<DriveFile | DriveFile[]> {
|
2020-01-30 00:13:36 +02:00
|
|
|
return new Promise((res, rej) => {
|
2022-01-30 07:11:52 +02:00
|
|
|
const keepOriginal = ref(defaultStore.state.keepOriginalUploading);
|
|
|
|
|
2020-01-30 00:13:36 +02:00
|
|
|
const chooseFileFromPc = () => {
|
|
|
|
const input = document.createElement('input');
|
|
|
|
input.type = 'file';
|
|
|
|
input.multiple = multiple;
|
|
|
|
input.onchange = () => {
|
2022-01-30 07:11:52 +02:00
|
|
|
const promises = Array.from(input.files).map(file => os.upload(file, defaultStore.state.uploadFolder, undefined, keepOriginal.value));
|
2020-01-30 00:13:36 +02:00
|
|
|
|
|
|
|
Promise.all(promises).then(driveFiles => {
|
|
|
|
res(multiple ? driveFiles : driveFiles[0]);
|
|
|
|
}).catch(e => {
|
2021-11-18 11:45:58 +02:00
|
|
|
os.alert({
|
2020-01-30 00:13:36 +02:00
|
|
|
type: 'error',
|
|
|
|
text: e
|
|
|
|
});
|
|
|
|
});
|
2020-05-31 16:19:28 +03:00
|
|
|
|
|
|
|
// 一応廃棄
|
|
|
|
(window as any).__misskey_input_ref__ = null;
|
2020-01-30 00:13:36 +02:00
|
|
|
};
|
2020-05-31 16:19:28 +03:00
|
|
|
|
|
|
|
// https://qiita.com/fukasawah/items/b9dc732d95d99551013d
|
|
|
|
// iOS Safari で正常に動かす為のおまじない
|
|
|
|
(window as any).__misskey_input_ref__ = input;
|
|
|
|
|
2020-01-30 00:13:36 +02:00
|
|
|
input.click();
|
|
|
|
};
|
|
|
|
|
|
|
|
const chooseFileFromDrive = () => {
|
2020-10-17 14:12:00 +03:00
|
|
|
os.selectDriveFile(multiple).then(files => {
|
2020-01-30 00:13:36 +02:00
|
|
|
res(files);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const chooseFileFromUrl = () => {
|
2021-11-18 11:45:58 +02:00
|
|
|
os.inputText({
|
2022-01-28 04:39:49 +02:00
|
|
|
title: i18n.ts.uploadFromUrl,
|
2021-11-18 11:45:58 +02:00
|
|
|
type: 'url',
|
2022-01-28 04:39:49 +02:00
|
|
|
placeholder: i18n.ts.uploadFromUrlDescription
|
2020-10-17 14:12:00 +03:00
|
|
|
}).then(({ canceled, result: url }) => {
|
|
|
|
if (canceled) return;
|
|
|
|
|
|
|
|
const marker = Math.random().toString(); // TODO: UUIDとか使う
|
|
|
|
|
2021-12-29 15:13:09 +02:00
|
|
|
const connection = stream.useChannel('main');
|
2020-10-17 14:12:00 +03:00
|
|
|
connection.on('urlUploadFinished', data => {
|
|
|
|
if (data.marker === marker) {
|
|
|
|
res(multiple ? [data.file] : data.file);
|
|
|
|
connection.dispose();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-27 11:15:08 +03:00
|
|
|
os.api('drive/files/upload-from-url', {
|
2020-10-17 14:12:00 +03:00
|
|
|
url: url,
|
2021-07-13 17:30:12 +03:00
|
|
|
folderId: defaultStore.state.uploadFolder,
|
2020-10-17 14:12:00 +03:00
|
|
|
marker
|
|
|
|
});
|
2020-01-30 00:13:36 +02:00
|
|
|
|
2021-11-18 11:45:58 +02:00
|
|
|
os.alert({
|
2022-01-28 04:39:49 +02:00
|
|
|
title: i18n.ts.uploadFromUrlRequested,
|
|
|
|
text: i18n.ts.uploadFromUrlMayTakeTime
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
|
|
|
});
|
2020-01-30 00:13:36 +02:00
|
|
|
};
|
|
|
|
|
2021-08-08 06:19:10 +03:00
|
|
|
os.popupMenu([label ? {
|
2020-10-17 14:12:00 +03:00
|
|
|
text: label,
|
|
|
|
type: 'label'
|
|
|
|
} : undefined, {
|
2022-01-30 07:11:52 +02:00
|
|
|
type: 'switch',
|
|
|
|
text: i18n.ts.keepOriginalUploading,
|
|
|
|
ref: keepOriginal
|
|
|
|
}, {
|
2022-01-28 04:39:49 +02:00
|
|
|
text: i18n.ts.upload,
|
2021-04-20 17:22:59 +03:00
|
|
|
icon: 'fas fa-upload',
|
2020-10-17 14:12:00 +03:00
|
|
|
action: chooseFileFromPc
|
|
|
|
}, {
|
2022-01-28 04:39:49 +02:00
|
|
|
text: i18n.ts.fromDrive,
|
2021-04-20 17:22:59 +03:00
|
|
|
icon: 'fas fa-cloud',
|
2020-10-17 14:12:00 +03:00
|
|
|
action: chooseFileFromDrive
|
|
|
|
}, {
|
2022-01-28 04:39:49 +02:00
|
|
|
text: i18n.ts.fromUrl,
|
2021-04-20 17:22:59 +03:00
|
|
|
icon: 'fas fa-link',
|
2020-10-17 14:12:00 +03:00
|
|
|
action: chooseFileFromUrl
|
|
|
|
}], src);
|
2020-01-30 00:13:36 +02:00
|
|
|
});
|
|
|
|
}
|
2021-12-09 18:22:22 +02:00
|
|
|
|
|
|
|
export function selectFile(src: any, label: string | null = null): Promise<DriveFile> {
|
|
|
|
return select(src, label, false) as Promise<DriveFile>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function selectFiles(src: any, label: string | null = null): Promise<DriveFile[]> {
|
|
|
|
return select(src, label, true) as Promise<DriveFile[]>;
|
|
|
|
}
|