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