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