2020-02-18 08:57:00 +01:00
|
|
|
import { IPicture } from "@/types/picture.model";
|
2019-09-02 14:35:50 +02:00
|
|
|
|
2020-10-25 12:01:50 +01:00
|
|
|
export async function buildFileFromIPicture(obj: IPicture | null | undefined): Promise<File | null> {
|
|
|
|
if (!obj) return Promise.resolve(null);
|
2019-09-02 14:35:50 +02:00
|
|
|
|
|
|
|
const response = await fetch(obj.url);
|
|
|
|
const blob = await response.blob();
|
|
|
|
|
|
|
|
return new File([blob], obj.name);
|
|
|
|
}
|
|
|
|
|
2020-10-25 12:01:50 +01:00
|
|
|
export function buildFileVariable<T>(file: File | null, name: string, alt?: string): Record<string, unknown> {
|
2019-09-02 14:35:50 +02:00
|
|
|
if (!file) return {};
|
|
|
|
|
|
|
|
return {
|
|
|
|
[name]: {
|
|
|
|
picture: {
|
|
|
|
name: file.name,
|
|
|
|
alt: alt || file.name,
|
|
|
|
file,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2019-10-08 19:32:48 +02:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
export function readFileAsync(file: File): Promise<string | ArrayBuffer | null> {
|
2019-10-08 19:32:48 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
|
|
|
reader.onload = () => {
|
|
|
|
resolve(reader.result);
|
|
|
|
};
|
|
|
|
|
|
|
|
reader.onerror = reject;
|
|
|
|
|
|
|
|
reader.readAsBinaryString(file);
|
|
|
|
});
|
|
|
|
}
|