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