import { APIClient } from "./ApiClient"; export type DiskImageFormat = | { format: "Raw"; is_sparse: boolean } | { format: "QCow2"; virtual_size?: number } | { format: "CompressedQCow2" } | { format: "CompressedRaw" }; export type DiskImage = { file_size: number; file_name: string; name: string; created: number; } & DiskImageFormat; export class DiskImageApi { /** * Upload a new disk image file to the server */ static async Upload( file: File, progress: (progress: number) => void ): Promise { const fd = new FormData(); fd.append("file", file); await APIClient.exec({ method: "POST", uri: "/disk_images/upload", formData: fd, upProgress: progress, }); } /** * Get the list of disk images */ static async GetList(): Promise { return ( await APIClient.exec({ method: "GET", uri: "/disk_images/list", }) ).data; } /** * Download disk image file */ static async Download( file: DiskImage, progress: (p: number) => void ): Promise { return ( await APIClient.exec({ method: "GET", uri: `/disk_images/${file.file_name}`, downProgress(e) { progress(Math.floor(100 * (e.progress / e.total))); }, }) ).data; } /** * Convert disk image file */ static async Convert( file: DiskImage, dest_file_name: string, dest_format: DiskImageFormat ): Promise { await APIClient.exec({ method: "POST", uri: `/disk_images/${file.file_name}/convert`, jsonData: { ...dest_format, dest_file_name }, }); } /** * Delete disk image file */ static async Delete(file: DiskImage): Promise { await APIClient.exec({ method: "DELETE", uri: `/disk_images/${file.file_name}`, }); } }