feat: upload org logos through r2 route

This commit is contained in:
2026-06-22 20:54:54 -07:00
parent 8ff16d643c
commit 2f1a9e4c7d
2 changed files with 21 additions and 13 deletions

View File

@@ -1,3 +0,0 @@
export const getOrgLogoUrl = (orgId: string) => {
return `${import.meta.env.VITE_SUPABASE_URL}/storage/v1/object/public/autumn/logo/${orgId}`;
};

View File

@@ -1,5 +1,4 @@
import { ImageIcon } from "lucide-react";
import axios from "axios";
import type React from "react";
import { useRef, useState } from "react";
import { toast } from "sonner";
@@ -9,7 +8,6 @@ import { useOrg } from "@/hooks/common/useOrg";
import { authClient } from "@/lib/auth-client";
import { useAxiosInstance } from "@/services/useAxiosInstance";
import { getBackendErr } from "@/utils/genUtils";
import { getOrgLogoUrl } from "@/utils/orgUtils";
const MAX_SIZE_MB = 10;
@@ -25,6 +23,7 @@ const OrgLogoUploader: React.FC = () => {
const handleRemove = async () => {
setRemoving(true);
try {
await axiosInstance.delete("/organization/logo");
const { error } = await authClient.organization.update({
data: { logo: "" },
});
@@ -44,12 +43,19 @@ const OrgLogoUploader: React.FC = () => {
inputRef.current?.click();
};
const uploadToSupabase = async (file: File) => {
const { data } = await axiosInstance.get("/organization/upload_url");
const { signedUrl } = data;
await axios.put(signedUrl, file, {
headers: { "Content-Type": file.type },
});
const uploadToR2 = async (file: File) => {
const formData = new FormData();
formData.append("file", file);
const { data } = await axiosInstance.post<{ logoUrl: string }>(
"/organization/logo",
formData,
{
headers: { "Content-Type": "multipart/form-data" },
},
);
return data.logoUrl;
};
const handleUploading = async (e: React.ChangeEvent<HTMLInputElement>) => {
@@ -62,9 +68,14 @@ const OrgLogoUploader: React.FC = () => {
}
setUploading(true);
try {
await uploadToSupabase(file);
if (!org?.id) {
toast.error("Organization is not loaded");
return;
}
const logoUrl = await uploadToR2(file);
const { error } = await authClient.organization.update({
data: { logo: getOrgLogoUrl(org.id) },
data: { logo: logoUrl },
});
if (error) {
toast.error(error.message || "Failed to update logo");