2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-12 03:37:45 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-01-30 06:11:52 +01:00
|
|
|
import { ref } from 'vue';
|
2023-09-04 06:33:38 +02:00
|
|
|
import * as Misskey from 'misskey-js';
|
2023-09-19 09:37:43 +02:00
|
|
|
import * as os from '@/os.js';
|
2024-01-04 10:32:46 +01:00
|
|
|
import { misskeyApi } from '@/scripts/misskey-api.js';
|
2023-09-19 09:37:43 +02:00
|
|
|
import { useStream } from '@/stream.js';
|
|
|
|
import { i18n } from '@/i18n.js';
|
|
|
|
import { defaultStore } from '@/store.js';
|
|
|
|
import { uploadFile } from '@/scripts/upload.js';
|
2020-01-29 23:13:36 +01:00
|
|
|
|
2023-09-04 06:33:38 +02:00
|
|
|
export function chooseFileFromPc(multiple: boolean, keepOriginal = false): Promise<Misskey.entities.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 = () => {
|
2023-07-25 12:46:52 +02:00
|
|
|
if (!input.files) return res([]);
|
|
|
|
const promises = Array.from(input.files, 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-09-04 06:33:38 +02:00
|
|
|
export function chooseFileFromDrive(multiple: boolean): Promise<Misskey.entities.DriveFile[]> {
|
2023-05-08 01:52:01 +02:00
|
|
|
return new Promise((res, rej) => {
|
|
|
|
os.selectDriveFile(multiple).then(files => {
|
|
|
|
res(files);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-01-29 23:13:36 +01:00
|
|
|
|
2023-09-04 06:33:38 +02:00
|
|
|
export function chooseFileFromUrl(): Promise<Misskey.entities.DriveFile> {
|
2023-05-08 01:52:01 +02: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 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-15 12:08:46 +02:00
|
|
|
const connection = useStream().useChannel('main');
|
2023-05-08 01:52:01 +02:00
|
|
|
connection.on('urlUploadFinished', urlResponse => {
|
|
|
|
if (urlResponse.marker === marker) {
|
|
|
|
res(urlResponse.file);
|
|
|
|
connection.dispose();
|
|
|
|
}
|
|
|
|
});
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2024-01-04 10:32:46 +01:00
|
|
|
misskeyApi('drive/files/upload-from-url', {
|
2023-05-08 01:52:01 +02:00
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-04 06:33:38 +02:00
|
|
|
function select(src: any, label: string | null, multiple: boolean): Promise<Misskey.entities.DriveFile[]> {
|
2023-05-08 01:52:01 +02:00
|
|
|
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
|
|
|
|
2023-09-04 06:33:38 +02:00
|
|
|
export function selectFile(src: any, label: string | null = null): Promise<Misskey.entities.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
|
|
|
}
|
|
|
|
|
2023-09-04 06:33:38 +02:00
|
|
|
export function selectFiles(src: any, label: string | null = null): Promise<Misskey.entities.DriveFile[]> {
|
2023-05-08 01:52:01 +02:00
|
|
|
return select(src, label, true);
|
2021-12-09 17:22:22 +01:00
|
|
|
}
|