From aa03c4997982ffab12cad318ffcec67c6d98ea57 Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Wed, 17 Jul 2024 10:35:42 -0400 Subject: [PATCH] export --- frontend/src/lib/index.ts | 46 +++++++++++++++++++++++ frontend/src/routes/settings/+page.svelte | 20 +--------- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/frontend/src/lib/index.ts b/frontend/src/lib/index.ts index da0c4dd..db4f293 100644 --- a/frontend/src/lib/index.ts +++ b/frontend/src/lib/index.ts @@ -1,4 +1,5 @@ import inspirationalQuotes from './json/quotes.json'; +import type { Adventure, Collection } from './types'; export function getRandomQuote() { const quotes = inspirationalQuotes.quotes; @@ -19,3 +20,48 @@ export function checkLink(link: string) { return 'http://' + link + '.com'; } } + +export async function exportData() { + let res = await fetch('/api/adventures/all'); + let adventures = (await res.json()) as Adventure[]; + + res = await fetch('/api/collections/all'); + let collections = (await res.json()) as Collection[]; + + res = await fetch('/api/visitedregion'); + let visitedRegions = await res.json(); + + const data = { + adventures, + collections, + visitedRegions + }; + + async function convertImages() { + const promises = data.adventures.map(async (adventure, i) => { + if (adventure.image) { + const res = await fetch(adventure.image); + const blob = await res.blob(); + const base64 = await blobToBase64(blob); + adventure.image = base64; + data.adventures[i].image = adventure.image; + } + }); + + await Promise.all(promises); + } + + function blobToBase64(blob: Blob): Promise { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.readAsDataURL(blob); + reader.onloadend = () => resolve(reader.result as string); + reader.onerror = (error) => reject(error); + }); + } + + await convertImages(); + + const blob = new Blob([JSON.stringify(data)], { type: 'application/json' }); + return URL.createObjectURL(blob); +} diff --git a/frontend/src/routes/settings/+page.svelte b/frontend/src/routes/settings/+page.svelte index 4c0da6f..7ea4660 100644 --- a/frontend/src/routes/settings/+page.svelte +++ b/frontend/src/routes/settings/+page.svelte @@ -6,6 +6,7 @@ import type { Adventure, Collection, User } from '$lib/types.js'; import { onMount } from 'svelte'; import { browser } from '$app/environment'; + import { exportData } from '$lib'; export let data; let user: User; @@ -35,24 +36,7 @@ } async function exportAdventures() { - let res = await fetch('/api/adventures/all'); - let adventures = (await res.json()) as Adventure[]; - - res = await fetch('/api/collections/all'); - let collections = (await res.json()) as Collection[]; - - res = await fetch('/api/visitedregion'); - let visitedRegions = await res.json(); - - const data = { - adventures, - collections, - visitedRegions - }; - - const blob = new Blob([JSON.stringify(data)], { type: 'application/json' }); - - const url = URL.createObjectURL(blob); + const url = await exportData(); const a = document.createElement('a'); a.href = url;