fix: corrigir URL da API de upload de avatar do Supabase Storage

This commit is contained in:
guisilvagomes 2025-10-28 11:23:24 -03:00
parent 227e67fa04
commit 7c1999733e

View File

@ -13,7 +13,8 @@ import type {
class AvatarService {
private readonly SUPABASE_URL = API_CONFIG.SUPABASE_URL;
private readonly STORAGE_URL = `${this.SUPABASE_URL}/storage/v1/object/avatars`;
private readonly STORAGE_URL = `${this.SUPABASE_URL}/storage/v1/object`;
private readonly BUCKET_NAME = "avatars";
/**
* Faz upload de avatar do usuário
@ -22,17 +23,29 @@ class AvatarService {
try {
const token = localStorage.getItem(API_CONFIG.STORAGE_KEYS.ACCESS_TOKEN);
if (!token) {
throw new Error("Token de autenticação não encontrado");
}
// Determina a extensão do arquivo
const ext = data.file.name.split(".").pop()?.toLowerCase() || "jpg";
const path = `${data.userId}/avatar.${ext}`;
const filePath = `${data.userId}/avatar.${ext}`;
// Cria FormData para o upload
const formData = new FormData();
formData.append("file", data.file);
console.log("[AvatarService] Upload:", {
url: `${this.STORAGE_URL}/${this.BUCKET_NAME}/${filePath}`,
userId: data.userId,
fileName: data.file.name,
fileSize: data.file.size,
fileType: data.file.type,
});
// Upload usando Supabase Storage API
await axios.post(
`${this.STORAGE_URL}/${path}`,
const response = await axios.post(
`${this.STORAGE_URL}/${this.BUCKET_NAME}/${filePath}`,
formData,
{
headers: {
@ -42,6 +55,8 @@ class AvatarService {
}
);
console.log("[AvatarService] Upload response:", response.data);
// Retorna a URL pública
const publicUrl = this.getPublicUrl({
userId: data.userId,
@ -53,6 +68,14 @@ class AvatarService {
};
} catch (error) {
console.error("Erro ao fazer upload do avatar:", error);
if (axios.isAxiosError(error)) {
console.error("Detalhes do erro:", {
status: error.response?.status,
statusText: error.response?.statusText,
data: error.response?.data,
url: error.config?.url,
});
}
throw error;
}
}
@ -76,7 +99,7 @@ class AvatarService {
* Não precisa de autenticação pois é endpoint público
*/
getPublicUrl(data: GetAvatarUrlInput): string {
return `${this.STORAGE_URL}/${data.userId}/avatar.${data.ext}`;
return `${this.STORAGE_URL}/${this.BUCKET_NAME}/${data.userId}/avatar.${data.ext}`;
}
}