finished object endpoints

Added writing (tested) and deleting (untested) endpoints
This commit is contained in:
DecDuck
2024-10-09 15:08:55 +11:00
parent 435551c207
commit 486bce8bc7
7 changed files with 59 additions and 25 deletions

View File

@ -1,10 +1,10 @@
<template> <template>
<Menu as="div" class="relative inline-block"> <Menu v-if="user" as="div" class="relative inline-block">
<MenuButton> <MenuButton>
<HeaderWidget> <HeaderWidget>
<div class="inline-flex items-center text-zinc-300 hover:text-white"> <div class="inline-flex items-center text-zinc-300 hover:text-white">
<img :src="userData.image" class="w-5 h-5 rounded-sm" /> <img :src="useObject(user.profilePicture)" class="w-5 h-5 rounded-sm" />
<span class="ml-2 text-sm font-bold">{{ userData.name }}</span> <span class="ml-2 text-sm font-bold">{{ user.displayName }}</span>
<ChevronDownIcon class="ml-3 h-4" /> <ChevronDownIcon class="ml-3 h-4" />
</div> </div>
</HeaderWidget> </HeaderWidget>
@ -14,13 +14,13 @@
enter-from-class="transform opacity-0 scale-95" enter-to-class="transform opacity-100 scale-100" enter-from-class="transform opacity-0 scale-95" enter-to-class="transform opacity-100 scale-100"
leave-active-class="transition ease-in duration-75" leave-from-class="transform opacity-100 scale-100" leave-active-class="transition ease-in duration-75" leave-from-class="transform opacity-100 scale-100"
leave-to-class="transform opacity-0 scale-95"> leave-to-class="transform opacity-0 scale-95">
<MenuItems class="absolute right-0 top-10 z-10 w-56 origin-top-right focus:outline-none"> <MenuItems class="absolute right-0 top-10 z-10 w-56 origin-top-right focus:outline-none shadow-md">
<PanelWidget class="flex-col gap-y-2"> <PanelWidget class="flex-col gap-y-2">
<NuxtLink to="/id/me" <NuxtLink to="/id/me"
class="transition inline-flex items-center w-full py-3 px-4 hover:bg-zinc-800"> class="transition inline-flex items-center w-full py-3 px-4 hover:bg-zinc-800">
<div class="inline-flex items-center text-zinc-300"> <div class="inline-flex items-center text-zinc-300">
<img :src="userData.image" class="w-5 h-5 rounded-sm" /> <img :src="useObject(user.profilePicture)" class="w-5 h-5 rounded-sm" />
<span class="ml-2 text-sm font-bold">{{ userData.name }}</span> <span class="ml-2 text-sm font-bold">{{ user.displayName }}</span>
</div> </div>
</NuxtLink> </NuxtLink>
<div class="h-0.5 rounded-full w-full bg-zinc-800" /> <div class="h-0.5 rounded-full w-full bg-zinc-800" />
@ -43,11 +43,9 @@ import { Menu, MenuButton, MenuItem, MenuItems } from '@headlessui/vue'
import { ChevronDownIcon } from '@heroicons/vue/16/solid'; import { ChevronDownIcon } from '@heroicons/vue/16/solid';
import type { NavigationItem } from '../composables/types'; import type { NavigationItem } from '../composables/types';
import HeaderWidget from './HeaderWidget.vue'; import HeaderWidget from './HeaderWidget.vue';
import { useObject } from '~/composables/objects';
const userData = { const user = useUser();
image: "https://avatars.githubusercontent.com/u/64579723?v=4",
name: "DecDuck",
}
const navigation: NavigationItem[] = [ const navigation: NavigationItem[] = [
{ {

1
composables/objects.ts Normal file
View File

@ -0,0 +1 @@
export const useObject = (id: string) => `/api/v1/object/${id}`;

View File

@ -12,12 +12,16 @@ const username = ref("");
const password = ref(""); const password = ref("");
async function register() { async function register() {
await $fetch('/api/v1/auth/signup/simple', { await $fetch("/api/v1/auth/signup/simple", {
method: "POST", method: "POST",
body: { body: {
username: username.value, username: username.value,
password: password.value password: password.value,
} },
}) });
} }
definePageMeta({
layout: false,
});
</script> </script>

View File

@ -41,7 +41,7 @@ export default defineEventHandler(async (h3) => {
const user = await prisma.user.create({ const user = await prisma.user.create({
data: { data: {
username, username,
displayName: "", displayName: "DecDuck",
email: "", email: "",
profilePicture: profilePictureObject, profilePicture: profilePictureObject,
}, },

View File

@ -0,0 +1,9 @@
export default defineEventHandler(async (h3) => {
const id = getRouterParam(h3, "id");
if (!id) throw createError({ statusCode: 400, statusMessage: "Invalid ID" });
const userId = await h3.context.session.getUserId(h3);
const result = await h3.context.objects.deleteWithPermission(id, userId);
return { success: result };
});

View File

@ -0,0 +1,21 @@
export default defineEventHandler(async (h3) => {
const id = getRouterParam(h3, "id");
if (!id) throw createError({ statusCode: 400, statusMessage: "Invalid ID" });
const body = await readRawBody(h3, "binary");
if (!body)
throw createError({
statusCode: 400,
statusMessage: "Invalid upload",
});
const userId = await h3.context.session.getUserId(h3);
const buffer = Buffer.from(body);
const result = await h3.context.objects.writeWithPermissions(
id,
async () => buffer,
userId
);
return { success: result };
});

View File

@ -131,11 +131,12 @@ export abstract class ObjectBackend {
userId?: string userId?: string
) { ) {
const metadata = await this.fetchMetadata(id); const metadata = await this.fetchMetadata(id);
if (!metadata) return; if (!metadata) return false;
const myPermissions = metadata.permissions const myPermissions = metadata.permissions
.filter((e) => { .filter((e) => {
if (userId !== undefined && e.startsWith(userId)) return true; if (userId !== undefined && e.startsWith(userId)) return true;
if (userId !== undefined && e.startsWith("internal")) return true;
if (e.startsWith("anonymous")) return true; if (e.startsWith("anonymous")) return true;
return false; return false;
}) })