Skip to content

Commit

Permalink
feat: add DELETE /api/file/id
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexcited committed Aug 12, 2023
1 parent ae7092f commit 9eacd5c
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/routes/api/file/[upload_id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type APIEvent, json } from "solid-start";

import type { UploadedFile, UserProfile } from "@/types/api";
import { supabase, getUserProfile } from "@/supabase/server";
import { file_extension } from "@/utils/files";

export const GET = async ({ request, params }: APIEvent): Promise<Response> => {
try {
Expand Down Expand Up @@ -80,4 +81,49 @@ export const GET = async ({ request, params }: APIEvent): Promise<Response> => {
message: "An error server-side happened."
}, { status: 500 });
}
}
};

export const DELETE = async ({ request, params }: APIEvent): Promise<Response> => {
let { upload_id } = params;

let token = request.headers.get("authorization");
if (!token) return json({
success: false,
message: "You need to provide an API token."
}, { status: 401 });

const { data: file_data } = await supabase
.from("uploads")
.select()
.eq("id", upload_id)
.limit(1)
.single<UploadedFile>();

// Respond only a 404.
if (!file_data) return json({
success: false,
message: "Couldn't find any upload with the given ID."
}, { status: 404 });

const user_profile = await getUserProfile(token)
let isCreatorOfFile = file_data.creator === user_profile.user_id;

// Check if we're the owner of the file.
if (!isCreatorOfFile) return json({
success: false,
message: "You're not allowed to delete this file."
}, { status: 403 });

const extension = file_extension(file_data.name);

await supabase
.from("uploads")
.delete()
.eq("id", upload_id);

await supabase.storage
.from("uploads")
.remove([`${file_data.creator}/${file_data.id}.${extension}`]);

return json({ success: true, data: null });
};

0 comments on commit 9eacd5c

Please sign in to comment.