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