69 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			69 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| export type MemoriumFile = {
 | |
|   type: "file";
 | |
|   name: string;
 | |
|   path: string;
 | |
|   modTime: string;
 | |
|   mime: string;
 | |
|   size: string;
 | |
|   content: any;
 | |
| };
 | |
| 
 | |
| export type MemoriumDir = {
 | |
|   type: "dir";
 | |
|   name: string;
 | |
|   path: string;
 | |
|   modTime: string;
 | |
|   mime: string;
 | |
|   size: string;
 | |
|   content: MemoriumEntry[];
 | |
| };
 | |
| 
 | |
| export type MemoriumEntry = MemoriumFile | MemoriumDir;
 | |
| 
 | |
| const SERVER_URL = "https://marka.max-richter.dev";
 | |
| //const SERVER_URL = "http://localhost:8080";
 | |
| 
 | |
| const cache = {};
 | |
| 
 | |
| export async function listResource(
 | |
|   id: string,
 | |
| ): Promise<MemoriumEntry | undefined> {
 | |
|   const url = `${SERVER_URL}/resources/${id}`;
 | |
|   if (cache[url]) return cache[url];
 | |
|   try {
 | |
|     const response = await fetch(url, { signal: AbortSignal.timeout(5000) });
 | |
|     if (response.ok) {
 | |
|       const json = await response.json();
 | |
|       if (json.type == "dir") {
 | |
|         const res = {
 | |
|           ...json,
 | |
|           content: json.content.filter((res: MemoriumEntry) =>
 | |
|             res.mime === "application/markdown"
 | |
|           ),
 | |
|         };
 | |
|         cache[url] = res;
 | |
|         return res;
 | |
|       }
 | |
|       cache[url] = json;
 | |
|       return json;
 | |
|     }
 | |
|   } catch (_e) {
 | |
|     console.log("Failed to get: ", url);
 | |
|     cache[url] = undefined;
 | |
|     return;
 | |
|   }
 | |
| }
 | |
| 
 | |
| export function getImageUrl(input: string): string {
 | |
|   if (!input) {
 | |
|     return;
 | |
|   }
 | |
|   if (input.startsWith("https://") || input.startsWith("http://")) {
 | |
|     return input;
 | |
|   }
 | |
|   if (input.startsWith("/")) {
 | |
|     return `${SERVER_URL}${input}`;
 | |
|   }
 | |
|   return `${SERVER_URL}/${input}`;
 | |
| }
 |