fix donwload attachment and modal to edit message

This commit is contained in:
Peter Maquiran
2024-08-29 12:13:15 +01:00
parent 97ad62e2b6
commit d8d294b662
38 changed files with 627 additions and 198 deletions
+71 -1
View File
@@ -1,4 +1,6 @@
import { err, ok, Result } from "neverthrow";
import { defer, from } from "rxjs";
import { catchError, timeout } from "rxjs/operators";
/**
* Retrieves a `FileReader` instance, accounting for potential Zone.js modifications.
@@ -56,7 +58,22 @@ export function createDataURL(base64String: string, mimeType: string): string {
export function createBlobFromBase64(base64String: string, mimeType: string): Blob {
// 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,/, '');
// Decode the base64 string
const binaryString = atob(cleanedBase64String);
// Convert binary string to Uint8Array
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
// Create and return the Blob object
return new Blob([bytes], { type: mimeType });
}
@@ -74,7 +91,7 @@ export function createDataURL(base64String: string, mimeType: string): string {
*/
export function convertBlobToDataURL(blob: Blob): Promise<string> {
return new Promise<string>((resolve, reject) => {
const reader = new FileReader();
const reader = getFileReader();
reader.onloadend = () => {
// Resolve the promise with the Data URL
@@ -90,3 +107,56 @@ export function convertBlobToDataURL(blob: Blob): Promise<string> {
reader.readAsDataURL(blob);
});
}
// export function convertBlobToDataURLWithTimeoutNoThrow(blob: Blob): Promise<Result<string, any>> {
// const convert = () => new Promise<Result<string, any>>((resolve, reject) => {
// const reader = new FileReader();
// reader.onloadend = () => resolve(ok(reader.result as string));
// reader.onerror = () => resolve(err(new Error('Failed to convert Blob to Data URL')));
// reader.readAsDataURL(blob);
// });
// return defer(() => from(convert()))
// .pipe(
// timeout(15000),
// catchError(error => {
// console.error('Warning: Operation took too long, but continuing:', error);
// return from(convert()); // Continue with the original operation
// })
// )
// .toPromise();
// }
// export function convertBlobToDataURLWithTimeout(blob: Blob): Promise<Result<string, any>> {
// return from(new Promise<Result<string, any>>((resolve, reject) => {
// const reader = getFileReader();
// reader.onloadend = () => resolve(ok(reader.result as string));
// reader.onerror = () => resolve(err(new Error('Failed to convert Blob to Data URL')));
// reader.readAsDataURL(blob);
// }))
// .pipe(
// timeout(15000),
// catchError(error => {
// console.error('Error: Operation took too long or failed:', error);
// return Promise.resolve(err('timeout'));
// })
// )
// .toPromise();
// }
/**
* 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`.
* @example result.value = 'data:audio/aac;base64,ZGF0YTphdWRpby9hYWM7YmFzZTY0…RnNRQmxmL0FGQUl'
*/
export function createBlobUrl(blob: Blob): Result<string, any> {
try {
return ok(URL.createObjectURL(blob));
} catch (error) {
return err('error '+ error);
}
}