protections
This commit is contained in:
@@ -3,41 +3,36 @@
|
||||
* Uploads files to S3 using pre-signed URLs from tRPC
|
||||
*/
|
||||
|
||||
import { api } from "~/lib/api";
|
||||
|
||||
export default async function AddImageToS3(
|
||||
file: Blob | File,
|
||||
title: string,
|
||||
type: string,
|
||||
type: string
|
||||
): Promise<string | undefined> {
|
||||
try {
|
||||
const filename = (file as File).name;
|
||||
|
||||
// Get pre-signed URL from tRPC endpoint
|
||||
const getPreSignedResponse = await fetch("/api/trpc/misc.getPreSignedURL", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
type: type,
|
||||
title: title,
|
||||
filename: (file as File).name,
|
||||
}),
|
||||
const { uploadURL, key } = await api.misc.getPreSignedURL.mutate({
|
||||
type,
|
||||
title,
|
||||
filename
|
||||
});
|
||||
|
||||
if (!getPreSignedResponse.ok) {
|
||||
throw new Error("Failed to get pre-signed URL");
|
||||
}
|
||||
|
||||
const responseData = await getPreSignedResponse.json();
|
||||
const { uploadURL, key } = responseData.result.data as {
|
||||
uploadURL: string;
|
||||
key: string;
|
||||
};
|
||||
|
||||
console.log("url: " + uploadURL, "key: " + key);
|
||||
|
||||
// Extract content type from filename extension
|
||||
const ext = /^.+\.([^.]+)$/.exec(filename);
|
||||
const contentType = ext ? `image/${ext[1]}` : "application/octet-stream";
|
||||
|
||||
// Upload file to S3 using pre-signed URL
|
||||
const uploadResponse = await fetch(uploadURL, {
|
||||
method: "PUT",
|
||||
body: file as File,
|
||||
headers: {
|
||||
"Content-Type": contentType
|
||||
},
|
||||
body: file as File
|
||||
});
|
||||
|
||||
if (!uploadResponse.ok) {
|
||||
|
||||
Reference in New Issue
Block a user