2020-11-26 10:41:13 +00:00
|
|
|
import { IMedia } from "@/types/media.model";
|
2019-09-02 12:35:50 +00:00
|
|
|
|
2020-11-30 09:24:11 +00:00
|
|
|
export async function buildFileFromIMedia(
|
|
|
|
obj: IMedia | null | undefined
|
|
|
|
): Promise<File | null> {
|
2020-10-25 11:01:50 +00:00
|
|
|
if (!obj) return Promise.resolve(null);
|
2019-09-02 12:35:50 +00:00
|
|
|
|
|
|
|
const response = await fetch(obj.url);
|
|
|
|
const blob = await response.blob();
|
|
|
|
|
|
|
|
return new File([blob], obj.name);
|
|
|
|
}
|
|
|
|
|
2020-11-27 18:27:44 +00:00
|
|
|
export function buildFileVariable(
|
|
|
|
file: File | null,
|
|
|
|
name: string,
|
|
|
|
alt?: string
|
|
|
|
): Record<string, unknown> {
|
2019-09-02 12:35:50 +00:00
|
|
|
if (!file) return {};
|
|
|
|
|
|
|
|
return {
|
|
|
|
[name]: {
|
2020-11-26 10:41:13 +00:00
|
|
|
media: {
|
2019-09-02 12:35:50 +00:00
|
|
|
name: file.name,
|
|
|
|
alt: alt || file.name,
|
|
|
|
file,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2019-10-08 17:32:48 +00:00
|
|
|
|
2020-11-30 09:24:11 +00:00
|
|
|
export function readFileAsync(
|
|
|
|
file: File
|
|
|
|
): Promise<string | ArrayBuffer | null> {
|
2019-10-08 17:32:48 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
|
|
|
reader.onload = () => {
|
|
|
|
resolve(reader.result);
|
|
|
|
};
|
|
|
|
|
|
|
|
reader.onerror = reject;
|
|
|
|
|
|
|
|
reader.readAsBinaryString(file);
|
|
|
|
});
|
|
|
|
}
|