|
| 1 | +/* |
| 2 | + * File: storage.ts |
| 3 | + * Project: wardrobe |
| 4 | + * Created Date: 2026-03-23 21:34:56 |
| 5 | + * Author: 3urobeat |
| 6 | + * |
| 7 | + * Last Modified: 2026-03-24 19:19:18 |
| 8 | + * Modified By: 3urobeat |
| 9 | + * |
| 10 | + * Copyright (c) 2026 3urobeat <https://github.com/3urobeat> |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. |
| 13 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. |
| 14 | + * You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +import type { CachedImage } from "~/model/storage"; |
| 19 | + |
| 20 | + |
| 21 | +// Cache |
| 22 | +const cachedImages: Ref<CachedImage[]> = ref([]); |
| 23 | +// TODO: Test Reactivity |
| 24 | +// TODO: Limit size |
| 25 | +// TODO: Establish cache update socket with server |
| 26 | + |
| 27 | + |
| 28 | +/** |
| 29 | + * Helper function to get image from server |
| 30 | + * @param imgPath File path to request |
| 31 | + * @param width Optional: Request scaled down image |
| 32 | + * @returns Returns image blob as string on success |
| 33 | + */ |
| 34 | +export async function getImageFromServer(imgPath: string, width: number | undefined): Promise<CachedImage | null> { |
| 35 | + if (!imgPath) return null; |
| 36 | + |
| 37 | + // Attempt to find image with matching size (or none) in cache |
| 38 | + const cachedImg = cachedImages.value.find((e) => e.imgPath == imgPath && e.imgWidth == width); |
| 39 | + |
| 40 | + if (cachedImg) { |
| 41 | + console.debug(`[DEBUG] getImageFromServer: Found image '${imgPath}' in cache!`); |
| 42 | + return cachedImg; |
| 43 | + } |
| 44 | + |
| 45 | + // Fetch image from server |
| 46 | + const res = await fetch("/api/get-image", { |
| 47 | + method: "POST", |
| 48 | + headers: { |
| 49 | + "Content-Type": "application/json" |
| 50 | + }, |
| 51 | + body: JSON.stringify({ |
| 52 | + filePath: imgPath, |
| 53 | + width: width |
| 54 | + }) |
| 55 | + }); |
| 56 | + |
| 57 | + const imageBlob = await res.text(); |
| 58 | + |
| 59 | + // Add to cache |
| 60 | + const cacheLength = cachedImages.value.push({ imgPath: imgPath, imgBlob: imageBlob, imgWidth: width }); |
| 61 | + console.debug(`[DEBUG] getImageFromServer: Fetched image '${imgPath}' from server. Image cache has ${cacheLength} entries now.`); |
| 62 | + |
| 63 | + return cachedImages.value[cacheLength - 1] as CachedImage; |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +/** |
| 68 | + * Sends new image to server and updates cache |
| 69 | + * @param file File to send |
| 70 | + * @returns API response body |
| 71 | + */ |
| 72 | +export async function sendImageToServer(file: File): Promise<any> { |
| 73 | + |
| 74 | + // Construct form to post |
| 75 | + const formData = new FormData(); |
| 76 | + formData.append("file", file); |
| 77 | + //formData.append("imgType", "clothing"); // TODO: Image type is hardcoded |
| 78 | + |
| 79 | + // Attempt to post file to API |
| 80 | + const res = await fetch("/api/set-clothing-image", { |
| 81 | + method: "POST", |
| 82 | + body: formData |
| 83 | + }); |
| 84 | + |
| 85 | + if (!res.ok) { |
| 86 | + throw("Failed to upload image: " + res.statusText); |
| 87 | + } |
| 88 | + |
| 89 | + // Get file name from response |
| 90 | + const resBody = await res.json(); |
| 91 | + |
| 92 | + // Remove all references of image from cache to fetch next usage from server again |
| 93 | + // TODO: Return imgBlob from API route and replace every matching imgPath using map() |
| 94 | + cachedImages.value = cachedImages.value.filter((e) => { |
| 95 | + const match = e.imgPath == resBody.filePath; |
| 96 | + if (match) console.debug(`[DEBUG] sendImageToServer: Removing '${resBody.filePath}' from image cache...`); |
| 97 | + return !match; |
| 98 | + }); |
| 99 | + |
| 100 | + return resBody; |
| 101 | + |
| 102 | +} |
0 commit comments