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