file-util.ts


Overview

file-util.ts is a utility module designed to facilitate common file operations in a web application context, especially around image and document handling. It provides functions for:

The module integrates tightly with frontend file input components (notably from the Ant Design library) and backend services (via fileManagerService) to fetch document blobs. It is aimed at simplifying file manipulation for upload, preview, download, and display purposes within the application.


Detailed Description of Functions

1. transformFile2Base64(val: any): Promise<any>

Converts a given File or Blob object (typically an image) into a compressed base64-encoded string.


2. transformBase64ToFile(dataUrl: string, filename: string = 'file'): File

Converts a base64-encoded Data URL back into a File object.


3. normFile(e: any): any[]

Normalizes the file input event from upload components.


4. getUploadFileListFromBase64(avatar: string): UploadFile[]

Generates an Ant Design UploadFile list from a single base64 string.


5. getBase64FromUploadFileList(fileList?: UploadFile[]): Promise<string>

Extracts the base64 string from an Ant Design UploadFile list.


6. fetchDocumentBlob(id: string, mimeType?: FileMimeType): Promise<Blob>

Fetches a document's binary data blob from the backend service.


7. previewHtmlFile(id: string): Promise<void>

Previews an HTML file by opening it in a new browser tab/window.


8. downloadFileFromBlob(blob: Blob, name?: string): void

Triggers a browser download for a given blob with optional filename.


9. downloadDocument({ id, filename }: { id: string; filename?: string }): Promise<void>

Downloads a document file by its ID.


10. formatBytes(x: string | number): string

Formats a file size in bytes into a human-readable string with units.


11. downloadJsonFile(data: Record<string, any>, fileName: string): Promise<void>

Downloads a JSON file from a JavaScript object.


12. transformBase64ToFileWithPreview(dataUrl: string, filename: string = 'file'): File

Converts a base64 string to a File object and attaches a preview property.


13. getBase64FromFileList(fileList?: File[]): Promise<string>

Gets a base64 string from the first file in a list of raw File objects.


Important Implementation Details


Interaction with Other System Parts


Visual Diagram

Below is a flowchart representing the main functions and their relationships within file-util.ts:

flowchart TD
    A[transformFile2Base64] --> B[Image Compression & Canvas]
    B --> C[Return base64 string]
    
    D[transformBase64ToFile] --> E[Parse base64 & create File]
    
    F[normFile] --> G[Normalize Upload Event]
    
    H[getUploadFileListFromBase64] --> I[Create UploadFile array with thumbUrl]
    
    J[getBase64FromUploadFileList] -->|If originFileObj| A
    J -->|Else| K[Return thumbUrl]
    
    L[fetchDocumentBlob] --> M[Call fileManagerService.getDocumentFile]
    M --> N[Return Blob]
    
    O[previewHtmlFile] --> L
    O --> P[Create ObjectURL & Open Link]
    
    Q[downloadFileFromBlob] --> R[Create ObjectURL & Trigger Download]
    
    S[downloadDocument] --> L
    S --> Q
    
    T[formatBytes] --> U[Convert bytes to human-readable string]
    
    V[downloadJsonFile] --> W[Create Blob from JSON]
    W --> Q
    
    X[transformBase64ToFileWithPreview] --> D
    X --> Y[Attach preview property]
    
    Z[getBase64FromFileList] --> A

Summary

The file-util.ts module is a comprehensive utility library for file operations focused on web applications that handle images and documents. It provides robust facilities for:

Its design leverages both browser APIs and backend services integration, making it a critical part of the file handling infrastructure in the application.


End of Documentation for file-util.ts