forked from appwrite/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path+page.ts
58 lines (48 loc) · 1.61 KB
/
+page.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { VARS } from '$lib/system.js';
import { redirect } from '@sveltejs/kit';
function getCardImgUrls(userId: string, endpoint: string) {
const resolved = endpoint;
const frontImg = `${resolved}/cards/cloud?userId=${userId}`;
const backImg = `${resolved}/cards/cloud-back?userId=${userId}`;
const ogImg = `${resolved}/cards/cloud-og?userId=${userId}`;
return { frontImg, backImg, ogImg };
}
async function urlContentToDataUri(url: string): Promise<string> {
const res = await fetch(url);
const blob = await res.blob();
return await new Promise((callback) => {
const reader = new FileReader();
reader.onload = function () {
if (typeof this.result !== 'string') return '';
callback(this.result);
};
reader.readAsDataURL(blob);
});
}
export async function load({ params, url, parent }) {
const { account } = await parent();
const userId = params.uid;
const endpoint = VARS.APPWRITE_ENDPOINT ?? `${url.origin}/v1`;
const { frontImg, backImg } = getCardImgUrls(userId, endpoint);
try {
const res = await fetch(frontImg);
if (!res.ok) {
redirect(303, '/');
}
return {
userId: params.uid,
base64: {
front: await urlContentToDataUri(frontImg),
back: await urlContentToDataUri(backImg)
},
isOwner: account && account.$id === userId,
imgUrls: {
frontImg,
backImg
}
};
} catch (e) {
console.error(e);
redirect(303, '/');
}
}