1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-21 22:09:36 +02:00
This commit is contained in:
Sean Morley 2024-07-17 10:35:42 -04:00
parent 824f5a6e2a
commit aa03c49979
2 changed files with 48 additions and 18 deletions

View file

@ -1,4 +1,5 @@
import inspirationalQuotes from './json/quotes.json'; import inspirationalQuotes from './json/quotes.json';
import type { Adventure, Collection } from './types';
export function getRandomQuote() { export function getRandomQuote() {
const quotes = inspirationalQuotes.quotes; const quotes = inspirationalQuotes.quotes;
@ -19,3 +20,48 @@ export function checkLink(link: string) {
return 'http://' + link + '.com'; 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<string> {
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);
}

View file

@ -6,6 +6,7 @@
import type { Adventure, Collection, User } from '$lib/types.js'; import type { Adventure, Collection, User } from '$lib/types.js';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { browser } from '$app/environment'; import { browser } from '$app/environment';
import { exportData } from '$lib';
export let data; export let data;
let user: User; let user: User;
@ -35,24 +36,7 @@
} }
async function exportAdventures() { async function exportAdventures() {
let res = await fetch('/api/adventures/all'); const url = await exportData();
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 a = document.createElement('a'); const a = document.createElement('a');
a.href = url; a.href = url;