import { Image } from "@mwater/expressions" import { ImageAcquirer, ImageAcquirerOptions } from "./formContext" import ImageUploaderModalComponent from "./ImageUploaderModalComponent" import uuid from "uuid" export interface UploadImageAcquirerOptions { apiUrl: string client: string | null T: (str: string, ...args: any[]) => string forceCamera?: boolean } /** ImageAcquirer implementation that uses the ImageUploaderModalComponent to upload images. */ export default class UploadImageAcquirer implements ImageAcquirer { options: UploadImageAcquirerOptions constructor(options: UploadImageAcquirerOptions) { this.options = options } acquire(options: ImageAcquirerOptions, success: (image: Image) => void, error: (err: any) => void): void { // Note: maxImageDimension is accepted for interface consistency but not used in web uploader ImageUploaderModalComponent.show({ apiUrl: this.options.apiUrl, client: this.options.client, T: this.options.T, success: (image: Image) => success(image), forceCamera: this.options.forceCamera, allowMultiple: false }) } acquireMultiple(options: ImageAcquirerOptions, success: (images: Image[]) => void, error: (err: any) => void): void { // Note: maxImageDimension is accepted for interface consistency but not used in web uploader ImageUploaderModalComponent.show({ apiUrl: this.options.apiUrl, client: this.options.client, T: this.options.T, success: (images: Image[]) => success(images), forceCamera: this.options.forceCamera, allowMultiple: true }) } addImageFromBase64(base64: string, success: (image: Image) => void, error: (err: any) => void, mimeType?: string): void { const effectiveMimeType = mimeType || "image/jpeg" const extension = effectiveMimeType === "image/png" ? "png" : "jpg" // Convert base64 to blob const base64Data = base64.split(',')[1] // Remove data:image/...;base64, prefix const binaryString = atob(base64Data) const bytes = new Uint8Array(binaryString.length) for (let i = 0; i < binaryString.length; i++) { bytes[i] = binaryString.charCodeAt(i) } const blob = new Blob([bytes], { type: effectiveMimeType }) const file = new File([blob], `image.${extension}`, { type: effectiveMimeType }) // Upload the file this.uploadFile(file, success, error) } private uploadFile(file: File, success: (image: Image) => void, error: (err: any) => void): void { const xhr = new XMLHttpRequest() const fd = new FormData() fd.append("image", file) // Create id - using a simple UID generator const id = uuid().replace(/-/g, "") xhr.addEventListener("load", () => { if (xhr.status === 200) { success({ id }) } else { error(new Error(this.options.T("Upload failed: {0}", xhr.responseText))) } }, false) xhr.addEventListener("error", () => { error(new Error(this.options.T("Error uploading file. You must be connected to the Internet for image upload to work from a web browser."))) }, false) xhr.addEventListener("abort", () => { error(new Error(this.options.T("Upload cancelled"))) }, false) // Generate url let url = this.options.apiUrl + "images/" + id + "?autoOrient=true" if (this.options.client) { url += "&client=" + this.options.client } // Begin upload xhr.open("POST", url, true) xhr.send(fd) } }