mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
upload attachment
This commit is contained in:
@@ -11,11 +11,11 @@ function getFileReader(): FileReader {
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a `File` object to a Base64 encoded string.
|
||||
* Converts a `File` object to a data url encoded string.
|
||||
* @param {File} file - The file to be converted.
|
||||
* @returns {Promise<Result<string, any>>} A promise that resolves with a `Result` object containing either the Base64 encoded string or an error.
|
||||
*/
|
||||
export function JSFileToBase64(file: File): Promise<Result<string, any>> {
|
||||
export function JSFileToDataUrl(file: File): Promise<Result<string, any>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
var reader = getFileReader();
|
||||
reader.readAsDataURL(file);
|
||||
@@ -28,3 +28,64 @@ export function JSFileToBase64(file: File): Promise<Result<string, any>> {
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Creates a Data URL from a base64-encoded string and MIME type.
|
||||
*
|
||||
* @param base64String - The base64-encoded data as a string. This should not include the `data:[mime-type];base64,` prefix.
|
||||
* @param mimeType - The MIME type of the data (e.g., `image/png`, `application/pdf`).
|
||||
*
|
||||
* @returns A Data URL formatted string.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const base64String = 'iVBORw0KGgoAAAANSUhEUgAAAAU...'; // Your base64 string
|
||||
* const mimeType = 'image/png'; // Your MIME type
|
||||
*
|
||||
* const dataURL = createDataURL(base64String, mimeType);
|
||||
* console.log(dataURL);
|
||||
* ```
|
||||
*/
|
||||
export function createDataURL(base64String: string, mimeType: string): string {
|
||||
// Make sure the base64 string doesn't have the data URL scheme or extra padding
|
||||
const cleanedBase64String = base64String.replace(/^data:[a-z]+\/[a-z]+;base64,/, '');
|
||||
return `data:${mimeType};base64,${cleanedBase64String}`;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Converts a `Blob` to a Data URL.
|
||||
* @param {Blob} blob - The `Blob` to be converted.
|
||||
* @returns {Promise<string>} A promise that resolves with the Data URL representation of the `Blob`.
|
||||
*/
|
||||
export function convertBlobToDataURL(blob: Blob): Promise<string> {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onloadend = () => {
|
||||
// Resolve the promise with the Data URL
|
||||
resolve(reader.result as string);
|
||||
};
|
||||
|
||||
reader.onerror = () => {
|
||||
// Reject the promise on error
|
||||
reject(new Error('Failed to convert Blob to Data URL'));
|
||||
};
|
||||
|
||||
// Read the Blob as a Data URL
|
||||
reader.readAsDataURL(blob);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* Regular expression to validate a data URL.
|
||||
* The pattern matches data URLs that start with `data:`, optionally followed by a media type,
|
||||
* optionally followed by an encoding (e.g., `base64`), and ending with base64 encoded data.
|
||||
*/
|
||||
export const zodDataUrlRegex = /^data:(?:[\w+\/-]+)?(?:[\w+\/-]+)?;base64,[\s\S]*$/;
|
||||
|
||||
/**
|
||||
* Zod schema for validating data URLs.
|
||||
* This schema ensures that the input string matches the format of a data URL.
|
||||
*
|
||||
* @example
|
||||
* const result = dataUrlSchema.safeParse('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDCAAAAC0BEMAAW9R3AAAAAElFTkSuQmCC');
|
||||
* if (result.success) {
|
||||
* console.log('Valid data URL:', result.data);
|
||||
* } else {
|
||||
* console.error('Validation error:', result.error.errors);
|
||||
* }
|
||||
*/
|
||||
export const zodDataUrlSchema = z.string().refine(value => zodDataUrlRegex.test(value), {
|
||||
message: 'Invalid data URL',
|
||||
});
|
||||
|
||||
/**
|
||||
* Validates if a string is a valid data URL.
|
||||
*
|
||||
* @param url - The string to validate as a data URL.
|
||||
* @returns {void} - Logs the result of the validation.
|
||||
*
|
||||
* @example
|
||||
* validateDataUrl('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDCAAAAC0BEMAAW9R3AAAAAElFTkSuQmCC');
|
||||
* // Output: Valid data URL: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDCAAAAC0BEMAAW9R3AAAAAElFTkSuQmCC
|
||||
*
|
||||
* validateDataUrl('invalid-url');
|
||||
* // Output: Validation error: [ ... ]
|
||||
*/
|
||||
const validateDataUrl = (url: string) => {
|
||||
const result: any = zodDataUrlSchema.safeParse(url);
|
||||
if (result.success) {
|
||||
console.log('Valid data URL:', url);
|
||||
} else {
|
||||
console.error('Validation error:', result.error.errors);
|
||||
}
|
||||
};
|
||||
|
||||
// Test the schema
|
||||
validateDataUrl('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDCAAAAC0BEMAAW9R3AAAAAElFTkSuQmCC');
|
||||
validateDataUrl('invalid-url');
|
||||
Reference in New Issue
Block a user