Daniel Lautzenheiser 421ecf17e6
Feature/website overhaul (#49)
* Reorganize repo
* Overhaul website design and rewrite in NextJS and Typescript
* Delete website-publish.yml
2022-10-25 09:18:18 -04:00

49 lines
1.1 KiB
TypeScript

import type { Field } from '../interface';
interface AssetProxyArgs {
path: string;
url?: string;
file?: File;
field?: Field;
}
export default class AssetProxy {
url: string;
fileObj?: File;
path: string;
field?: Field;
constructor({ url, file, path, field }: AssetProxyArgs) {
this.url = url ? url : window.URL.createObjectURL(file as Blob);
this.fileObj = file;
this.path = path;
this.field = field;
}
toString(): string {
return this.url;
}
async toBase64(): Promise<string> {
const blob = await fetch(this.url).then(response => response.blob());
if (blob.size <= 0) {
return '';
}
const result = await new Promise<string>(resolve => {
const fr = new FileReader();
fr.onload = (readerEvt): void => {
const binaryString = readerEvt.target?.result || '';
resolve(binaryString.toString().split('base64,')[1]);
};
fr.readAsDataURL(blob);
});
return result;
}
}
export function createAssetProxy({ url, file, path, field }: AssetProxyArgs): AssetProxy {
return new AssetProxy({ url, file, path, field });
}