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