1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-24 15:29:36 +02:00

Image upload for adventures

This commit is contained in:
Sean Morley 2024-06-14 16:04:02 +00:00
parent 4c1bdf5faf
commit 2acfc1defb
5 changed files with 185 additions and 15 deletions

View file

@ -5,13 +5,14 @@
"private": true,
"scripts": {
"dev": "vite dev",
"generate": "drizzle-kit generate:pg --config drizzle.config.ts",
"migrate": "drizzle-kit push:pg --config drizzle.config.ts",
"minio": "cd minio && docker-compose up",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"studio": "drizzle-kit studio --config drizzle.config.ts",
"generate": "drizzle-kit generate:pg --config drizzle.config.ts",
"migrate": "drizzle-kit push:pg --config drizzle.config.ts",
"seed": "node seed.js"
},
"devDependencies": {

View file

@ -42,12 +42,20 @@
>
<figure>
<!-- svelte-ignore a11y-img-redundant-alt -->
<img
src={adventure.imageUrl ||
"https://placehold.co/300?text=No%20Image%20Found&font=roboto"}
alt="No image available"
class="w-full h-48 object-cover"
/>
{#if adventure.imageUrl && adventure.imageUrl !== ""}
<img
src="/cdn/adventures/{adventure.imageUrl}"
alt="No image available"
class="w-full h-48 object-cover"
crossorigin="anonymous"
/>
{:else}
<img
src={"https://placehold.co/300?text=No%20Image%20Found&font=roboto"}
alt="No image available"
class="w-full h-48 object-cover"
/>
{/if}
</figure>
<div class="card-body">

View file

@ -18,8 +18,11 @@
import { onMount } from "svelte";
import { addActivityType, generateDescription, getImage } from "$lib";
import AutoComplete from "./AutoComplete.svelte";
import ImageModal from "./ImageModal.svelte";
let modal: HTMLDialogElement;
let isImageModalOpen: boolean = false;
let activityTypes: string[] = [];
$: selected = "";
@ -96,8 +99,18 @@
newAdventure = addActivityType(activityInput, newAdventure);
activityInput = "";
}
function upload(e: CustomEvent<any>) {
let key = e.detail;
console.log("EE" + key);
newAdventure.imageUrl = key;
}
</script>
{#if isImageModalOpen}
<ImageModal on:submit={upload} on:close={() => (isImageModalOpen = false)} />
{/if}
<dialog id="my_modal_1" class="modal">
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
@ -176,12 +189,14 @@
</div>
<div>
<label for="rating">Image URL</label>
<input
type="url"
id="iamgeUrl"
bind:value={newAdventure.imageUrl}
class="input input-bordered w-full max-w-xs"
/>
<button
type="button"
class="btn btn-secondary"
on:click={() => {
isImageModalOpen = true;
}}
>
</button>
</div>
<button

View file

@ -0,0 +1,146 @@
<script lang="ts">
import { createEventDispatcher } from "svelte";
import { onMount } from "svelte";
let modal: HTMLDialogElement;
let viewType: string = "upload";
let imageUrl: string = "";
let imageFile: File | null = null;
let key: string;
const dispatch = createEventDispatcher();
onMount(() => {
modal = document.getElementById("my_modal_1") as HTMLDialogElement;
if (modal) {
modal.showModal();
}
});
function close() {
dispatch("close");
}
async function submit() {
try {
let key: string;
if (viewType === "url") {
// Get image from URL
const response = await fetch(imageUrl);
const blob = await response.blob();
const uploadResponse = await fetch("/api/upload", {
method: "POST",
body: blob,
headers: {
bucket: "adventures",
type: "adventure",
"Content-Type": blob.type,
},
});
const result = await uploadResponse.json();
key = result.key;
console.log(result);
} else if (imageFile) {
// Get the image from the file input
const uploadResponse = await fetch("/api/upload", {
method: "POST",
body: imageFile,
headers: {
bucket: "adventures",
type: "adventure",
"Content-Type": imageFile.type,
},
});
const result = await uploadResponse.json();
key = result.key;
console.log(result);
} else {
console.error("No file selected for upload");
return;
}
dispatch("submit", key);
close();
} catch (error) {
console.error("Error during submission", error);
}
}
function handleKeydown(event: KeyboardEvent) {
if (event.key === "Escape") {
close();
}
}
function setViewType(type: string) {
viewType = type;
}
function handleFileChange(event: Event) {
const target = event.target as HTMLInputElement;
if (target.files && target.files.length > 0) {
imageFile = target.files[0];
}
}
</script>
<dialog id="my_modal_1" class="modal">
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
<div class="modal-box" role="dialog" on:keydown={handleKeydown} tabindex="0">
<h3 class="font-bold text-lg">Image Upload</h3>
<div class="join items-center justify-center flex">
<input
class="join-item btn btn-neutral"
type="radio"
name="upload"
checked
aria-label="Upload File"
on:click={() => setViewType("upload")}
/>
<input
class="join-item btn btn-neutral"
type="radio"
name="upload"
aria-label="Url"
on:click={() => setViewType("url")}
/>
</div>
{#if viewType == "url"}
<form method="dialog" style="width: 100%;">
<div>
<label for="rating">Image URL</label>
<input
type="text"
id="imageUrl"
bind:value={imageUrl}
placeholder="Enter the URL of the image"
/>
</div>
</form>
{/if}
{#if viewType == "upload"}
<form method="dialog" style="width: 100%;">
<div>
<label for="rating">Image Upload</label>
<input
type="file"
id="imageFile"
on:change={handleFileChange}
placeholder="Upload an image file"
/>
</div>
</form>
{/if}
<button class="btn btn-neutral" on:click={close}>Close</button>
<button class="btn btn-primary" on:click={submit}>Submit</button>
</div>
</dialog>

View file

@ -64,7 +64,7 @@ export async function POST(event: RequestEvent): Promise<Response> {
"Content-Type": contentType,
};
const allowedBuckets = ["backgrounds", "profile-pics"];
const allowedBuckets = ["backgrounds", "profile-pics", "adventures"];
if (!allowedBuckets.includes(bucket)) {
return new Response(JSON.stringify({ error: "Invalid bucket name" }), {