|
|
@ -1,27 +1,27 @@
|
|
|
|
import { invoke } from '@tauri-apps/api/tauri';
|
|
|
|
import { invoke } from "@tauri-apps/api/tauri";
|
|
|
|
import { appWindow } from '@tauri-apps/api/window';
|
|
|
|
import { appWindow } from "@tauri-apps/api/window";
|
|
|
|
|
|
|
|
|
|
|
|
interface BaseHandlerData {
|
|
|
|
interface BaseHandlerData {
|
|
|
|
id: number;
|
|
|
|
id: number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface ProgressHandlerData extends BaseHandlerData {
|
|
|
|
interface ProgressHandlerData extends BaseHandlerData {
|
|
|
|
progress: number;
|
|
|
|
progress: number;
|
|
|
|
total: number;
|
|
|
|
total: number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface SizeHandlerData extends BaseHandlerData {
|
|
|
|
interface SizeHandlerData extends BaseHandlerData {
|
|
|
|
size: number;
|
|
|
|
size: number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface ResponseData {
|
|
|
|
interface ResponseData {
|
|
|
|
text: string;
|
|
|
|
text: string;
|
|
|
|
status: number;
|
|
|
|
status: number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enum UploadEvent {
|
|
|
|
enum UploadEvent {
|
|
|
|
progress = 'upload://progress',
|
|
|
|
progress = "upload://progress",
|
|
|
|
fileSize = 'upload://file-size',
|
|
|
|
fileSize = "upload://file-size",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type EventId = `${UploadEvent}-${number}`;
|
|
|
|
type EventId = `${UploadEvent}-${number}`;
|
|
|
@ -31,43 +31,53 @@ type SizeHandler = (data: SizeHandlerData) => unknown;
|
|
|
|
const handlers: Map<EventId, ProgressHandler | SizeHandler> = new Map();
|
|
|
|
const handlers: Map<EventId, ProgressHandler | SizeHandler> = new Map();
|
|
|
|
const listeningMap: Map<UploadEvent, boolean> = new Map();
|
|
|
|
const listeningMap: Map<UploadEvent, boolean> = new Map();
|
|
|
|
|
|
|
|
|
|
|
|
const getIdForEvent = (event: UploadEvent, id: number): EventId => `${event}-${id}`;
|
|
|
|
const getIdForEvent = (event: UploadEvent, id: number): EventId =>
|
|
|
|
|
|
|
|
`${event}-${id}`;
|
|
|
|
|
|
|
|
|
|
|
|
async function listenToEventIfNeeded(event: UploadEvent) {
|
|
|
|
async function listenToEventIfNeeded(event: UploadEvent) {
|
|
|
|
if (listeningMap.get(event)) {
|
|
|
|
if (listeningMap.get(event)) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return appWindow.listen<SizeHandlerData & ProgressHandlerData>(
|
|
|
|
|
|
|
|
event,
|
|
|
|
|
|
|
|
({ payload }) => {
|
|
|
|
|
|
|
|
const eventId = getIdForEvent(event, payload.id);
|
|
|
|
|
|
|
|
const handler = handlers.get(eventId);
|
|
|
|
|
|
|
|
if (typeof handler === "function") {
|
|
|
|
|
|
|
|
handler(payload);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return appWindow.listen<SizeHandlerData & ProgressHandlerData>(event, ({ payload }) => {
|
|
|
|
);
|
|
|
|
const eventId = getIdForEvent(event, payload.id);
|
|
|
|
|
|
|
|
const handler = handlers.get(eventId);
|
|
|
|
|
|
|
|
if (typeof handler === 'function') {
|
|
|
|
|
|
|
|
handler(payload);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default async function upload(url: string, filePath: string, progressHandler?: ProgressHandler, fileSizeHandler?: SizeHandler, headers?: Record<string, string>) {
|
|
|
|
export default async function upload(
|
|
|
|
const ids = new Uint32Array(1);
|
|
|
|
url: string,
|
|
|
|
window.crypto.getRandomValues(ids);
|
|
|
|
filePath: string,
|
|
|
|
const id = ids[0];
|
|
|
|
progressHandler?: ProgressHandler,
|
|
|
|
|
|
|
|
fileSizeHandler?: SizeHandler,
|
|
|
|
|
|
|
|
headers?: Record<string, string>
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
const ids = new Uint32Array(1);
|
|
|
|
|
|
|
|
window.crypto.getRandomValues(ids);
|
|
|
|
|
|
|
|
const id = ids[0];
|
|
|
|
|
|
|
|
|
|
|
|
if (progressHandler) {
|
|
|
|
if (progressHandler) {
|
|
|
|
const eventId = getIdForEvent(UploadEvent.progress, id);
|
|
|
|
const eventId = getIdForEvent(UploadEvent.progress, id);
|
|
|
|
handlers.set(eventId, progressHandler);
|
|
|
|
handlers.set(eventId, progressHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (fileSizeHandler) {
|
|
|
|
if (fileSizeHandler) {
|
|
|
|
const eventId = getIdForEvent(UploadEvent.fileSize, id);
|
|
|
|
const eventId = getIdForEvent(UploadEvent.fileSize, id);
|
|
|
|
handlers.set(eventId, fileSizeHandler);
|
|
|
|
handlers.set(eventId, fileSizeHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await listenToEventIfNeeded(UploadEvent.progress);
|
|
|
|
await listenToEventIfNeeded(UploadEvent.progress);
|
|
|
|
await listenToEventIfNeeded(UploadEvent.fileSize);
|
|
|
|
await listenToEventIfNeeded(UploadEvent.fileSize);
|
|
|
|
|
|
|
|
|
|
|
|
return await invoke<ResponseData>('plugin:upload|upload', {
|
|
|
|
return await invoke<ResponseData>("plugin:upload|upload", {
|
|
|
|
id,
|
|
|
|
id,
|
|
|
|
url,
|
|
|
|
url,
|
|
|
|
filePath,
|
|
|
|
filePath,
|
|
|
|
headers: headers ?? {},
|
|
|
|
headers: headers ?? {},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|