Skip to content

Commit

Permalink
fix: add extra file for getting workspace files
Browse files Browse the repository at this point in the history
  • Loading branch information
trobonox committed Aug 12, 2023
1 parent 4541ff8 commit beb16f5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 71 deletions.
35 changes: 0 additions & 35 deletions src/routes/api/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> => {
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);
Expand Down
38 changes: 38 additions & 0 deletions src/routes/api/get_files.ts
Original file line number Diff line number Diff line change
@@ -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<Response> => {
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
});
}
2 changes: 1 addition & 1 deletion src/routes/dashboard/(overview).tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
35 changes: 0 additions & 35 deletions supabase/functions/upload-file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down

0 comments on commit beb16f5

Please sign in to comment.