Add profile uploading + cleanup

This commit is contained in:
ZXY101
2024-03-26 01:21:20 +02:00
parent 8ecc96e92e
commit 6daeb966c5
4 changed files with 163 additions and 68 deletions

32
src/lib/util/cloud.ts Normal file
View File

@@ -0,0 +1,32 @@
type FileInfo = {
accessToken: string;
metadata: any;
fileId?: string;
localStorageId: string;
type: string;
}
const FILES_API_URL = 'https://www.googleapis.com/upload/drive/v3/files';
export async function uploadFile({ accessToken, fileId, localStorageId, metadata, type }: FileInfo) {
const json = localStorage.getItem(localStorageId) || '';
const blob = new Blob([json], { type });
const form = new FormData();
form.append('resource', new Blob([JSON.stringify(metadata)], { type }));
form.append('file', blob);
const res = await fetch(
`${FILES_API_URL}${fileId ? `/${fileId}` : ''}?uploadType=multipart`,
{
method: fileId ? 'PATCH' : 'POST',
headers: new Headers({ Authorization: 'Bearer ' + accessToken }),
body: form
}
);
return await res.json()
}

View File

@@ -2,4 +2,5 @@ export * from './snackbar';
export * from './upload';
export * from './misc';
export * from './modals';
export * from './zip'
export * from './zip'
export * from './cloud'