mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-19 12:59:36 +02:00
76 lines
2 KiB
TypeScript
76 lines
2 KiB
TypeScript
import inspirationalQuotes from './json/quotes.json';
|
|
import type { Adventure, Collection } from './types';
|
|
|
|
export function getRandomQuote() {
|
|
const quotes = inspirationalQuotes.quotes;
|
|
const randomIndex = Math.floor(Math.random() * quotes.length);
|
|
let quoteString = quotes[randomIndex].quote;
|
|
let authorString = quotes[randomIndex].author;
|
|
return '"' + quoteString + '" - ' + authorString;
|
|
}
|
|
|
|
export function getFlag(size: number, country: string) {
|
|
return `https://flagcdn.com/h${size}/${country}.png`;
|
|
}
|
|
|
|
export function checkLink(link: string) {
|
|
if (link.startsWith('http://') || (link.startsWith('https://') && link.indexOf('.') !== -1)) {
|
|
return link;
|
|
} else {
|
|
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);
|
|
}
|
|
|
|
export function isValidUrl(url: string) {
|
|
try {
|
|
new URL(url);
|
|
return true;
|
|
} catch (err) {
|
|
return false;
|
|
}
|
|
}
|