From beb16f5c92c3929feb288108886d3b706187a67f Mon Sep 17 00:00:00 2001 From: trobonox <57040351+trobonox@users.noreply.github.com> Date: Sat, 12 Aug 2023 09:14:44 +0300 Subject: [PATCH] fix: add extra file for getting workspace files --- src/routes/api/files.ts | 35 ----------------------- src/routes/api/get_files.ts | 38 +++++++++++++++++++++++++ src/routes/dashboard/(overview).tsx | 2 +- supabase/functions/upload-file/index.ts | 35 ----------------------- 4 files changed, 39 insertions(+), 71 deletions(-) create mode 100644 src/routes/api/get_files.ts diff --git a/src/routes/api/files.ts b/src/routes/api/files.ts index c15a9fd..456d763 100644 --- a/src/routes/api/files.ts +++ b/src/routes/api/files.ts @@ -2,41 +2,6 @@ import { type APIEvent, json, redirect } from "solid-start" import { supabase, getUserProfile } from "@/supabase/server"; import type { UserProfile } from "@/types/api"; -export const GET = async ({ request }: APIEvent): Promise => { - let api_token = request.headers.get("authorization"); - - let user_profile: UserProfile | undefined; - if (api_token) (user_profile = await getUserProfile(api_token)); - - if (!user_profile) return json({ - success: false, - message: "API key should be wrong." - }, { status: 403 }); - - const workspace_id = new URL(request.url).searchParams.get("workspace_id"); - const { data: workspace_data } = await supabase - .from("workspaces") - .select() - .eq("id", workspace_id) - .limit(1) - .single(); - - if (!workspace_data || workspace_data.creator !== user_profile.user_id) return json({ - success: false, - message: "Not allowed to get that workspace." - }, { status: 403 }); - - const { data } = await supabase - .from("uploads") - .select() - .eq("workspace_id", workspace_data.id); - - return json({ - success: true, - data - }); -} - export const PUT = () => { const url = new URL(`/functions/v1/upload-file`, import.meta.env.VITE_SUPABASE_PROJECT_URL as string) return redirect(url.href, 308); diff --git a/src/routes/api/get_files.ts b/src/routes/api/get_files.ts new file mode 100644 index 0000000..f3cecda --- /dev/null +++ b/src/routes/api/get_files.ts @@ -0,0 +1,38 @@ +import { type APIEvent, json, redirect } from "solid-start" +import { supabase, getUserProfile } from "@/supabase/server"; +import type { UserProfile } from "@/types/api"; + +export const GET = async ({ request }: APIEvent): Promise => { + let api_token = request.headers.get("authorization"); + + let user_profile: UserProfile | undefined; + if (api_token) (user_profile = await getUserProfile(api_token)); + + if (!user_profile) return json({ + success: false, + message: "API key should be wrong." + }, { status: 403 }); + + const workspace_id = new URL(request.url).searchParams.get("workspace_id"); + const { data: workspace_data } = await supabase + .from("workspaces") + .select() + .eq("id", workspace_id) + .limit(1) + .single(); + + if (!workspace_data || workspace_data.creator !== user_profile.user_id) return json({ + success: false, + message: "Not allowed to get that workspace." + }, { status: 403 }); + + const { data } = await supabase + .from("uploads") + .select() + .eq("workspace_id", workspace_data.id); + + return json({ + success: true, + data + }); +} \ No newline at end of file diff --git a/src/routes/dashboard/(overview).tsx b/src/routes/dashboard/(overview).tsx index 7e37cbb..6d056e0 100644 --- a/src/routes/dashboard/(overview).tsx +++ b/src/routes/dashboard/(overview).tsx @@ -47,7 +47,7 @@ const Page: Component = () => { onMount(async () => { const response = await fetch( - `/api/files?workspace_id=${currentWorkspaceId()}`, + `/api/get_files?workspace_id=${currentWorkspaceId()}`, { method: "GET", headers: { authorization: auth.profile!.api_token }, diff --git a/supabase/functions/upload-file/index.ts b/supabase/functions/upload-file/index.ts index 3d49153..1a4b11d 100644 --- a/supabase/functions/upload-file/index.ts +++ b/supabase/functions/upload-file/index.ts @@ -73,41 +73,6 @@ serve(async (req: Request) => { }); } - if (req.method === "GET") { - let api_token = req.headers.get("authorization"); - - let user_profile: UserProfile | undefined; - if (api_token) (user_profile = await getUserProfile(api_token)); - - if (!user_profile) return json({ - success: false, - message: "API key should be wrong." - }, { status: 403 }); - - const workspace_id = new URL(req.url).searchParams.get("workspace_id"); - const { data: workspace_data } = await supabase - .from("workspaces") - .select() - .eq("id", workspace_id) - .limit(1) - .single(); - - if (!workspace_data || workspace_data.creator !== user_profile.user_id) return json({ - success: false, - message: "Not allowed to get that workspace." - }, { status: 403 }); - - const { data } = await supabase - .from("uploads") - .select() - .eq("workspace_id", workspace_data.id); - - return json({ - success: true, - data - }); - } - return json({ success: false, message: "Unknown method."