2020-10-17 04:12:00 -07:00
|
|
|
import { faUpload, faCloud, faLink } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import * as os from '@/os';
|
|
|
|
import { i18n } from '@/i18n';
|
2020-01-29 14:13:36 -08:00
|
|
|
|
2020-10-17 04:12:00 -07:00
|
|
|
export function selectFile(src: any, label: string | null, multiple = false) {
|
2020-01-29 14:13:36 -08:00
|
|
|
return new Promise((res, rej) => {
|
|
|
|
const chooseFileFromPc = () => {
|
|
|
|
const input = document.createElement('input');
|
|
|
|
input.type = 'file';
|
|
|
|
input.multiple = multiple;
|
|
|
|
input.onchange = () => {
|
2020-10-17 04:12:00 -07:00
|
|
|
const promises = Array.from(input.files).map(file => os.upload(file));
|
2020-01-29 14:13:36 -08:00
|
|
|
|
|
|
|
Promise.all(promises).then(driveFiles => {
|
|
|
|
res(multiple ? driveFiles : driveFiles[0]);
|
|
|
|
}).catch(e => {
|
2020-10-17 04:12:00 -07:00
|
|
|
os.dialog({
|
2020-01-29 14:13:36 -08:00
|
|
|
type: 'error',
|
|
|
|
text: e
|
|
|
|
});
|
|
|
|
});
|
2020-05-31 06:19:28 -07:00
|
|
|
|
|
|
|
// 一応廃棄
|
|
|
|
(window as any).__misskey_input_ref__ = null;
|
2020-01-29 14:13:36 -08:00
|
|
|
};
|
2020-05-31 06:19:28 -07:00
|
|
|
|
|
|
|
// https://qiita.com/fukasawah/items/b9dc732d95d99551013d
|
|
|
|
// iOS Safari で正常に動かす為のおまじない
|
|
|
|
(window as any).__misskey_input_ref__ = input;
|
|
|
|
|
2020-01-29 14:13:36 -08:00
|
|
|
input.click();
|
|
|
|
};
|
|
|
|
|
|
|
|
const chooseFileFromDrive = () => {
|
2020-10-17 04:12:00 -07:00
|
|
|
os.selectDriveFile(multiple).then(files => {
|
2020-01-29 14:13:36 -08:00
|
|
|
res(files);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const chooseFileFromUrl = () => {
|
2020-10-17 04:12:00 -07:00
|
|
|
os.dialog({
|
|
|
|
title: i18n.global.t('uploadFromUrl'),
|
|
|
|
input: {
|
|
|
|
placeholder: i18n.global.t('uploadFromUrlDescription')
|
|
|
|
}
|
|
|
|
}).then(({ canceled, result: url }) => {
|
|
|
|
if (canceled) return;
|
|
|
|
|
|
|
|
const marker = Math.random().toString(); // TODO: UUIDとか使う
|
|
|
|
|
|
|
|
const connection = os.stream.useSharedConnection('main');
|
|
|
|
connection.on('urlUploadFinished', data => {
|
|
|
|
if (data.marker === marker) {
|
|
|
|
res(multiple ? [data.file] : data.file);
|
|
|
|
connection.dispose();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
os.api('drive/files/upload_from_url', {
|
|
|
|
url: url,
|
|
|
|
marker
|
|
|
|
});
|
2020-01-29 14:13:36 -08:00
|
|
|
|
2020-10-17 04:12:00 -07:00
|
|
|
os.dialog({
|
|
|
|
title: i18n.global.t('uploadFromUrlRequested'),
|
|
|
|
text: i18n.global.t('uploadFromUrlMayTakeTime')
|
|
|
|
});
|
|
|
|
});
|
2020-01-29 14:13:36 -08:00
|
|
|
};
|
|
|
|
|
2020-10-17 04:12:00 -07:00
|
|
|
os.modalMenu([label ? {
|
|
|
|
text: label,
|
|
|
|
type: 'label'
|
|
|
|
} : undefined, {
|
|
|
|
text: i18n.global.t('upload'),
|
|
|
|
icon: faUpload,
|
|
|
|
action: chooseFileFromPc
|
|
|
|
}, {
|
|
|
|
text: i18n.global.t('fromDrive'),
|
|
|
|
icon: faCloud,
|
|
|
|
action: chooseFileFromDrive
|
|
|
|
}, {
|
|
|
|
text: i18n.global.t('fromUrl'),
|
|
|
|
icon: faLink,
|
|
|
|
action: chooseFileFromUrl
|
|
|
|
}], src);
|
2020-01-29 14:13:36 -08:00
|
|
|
});
|
|
|
|
}
|