Запрос на слияние 'sber_mobile' (#38) из sber_mobile в main
This commit is contained in:
@@ -18,10 +18,26 @@ router.get('/profile', async (req, res) => {
|
|||||||
|
|
||||||
if (profileError) return res.status(400).json({ error: profileError.message });
|
if (profileError) return res.status(400).json({ error: profileError.message });
|
||||||
|
|
||||||
|
// Получаем аватарку из бакета
|
||||||
|
let avatarUrl = null;
|
||||||
|
const avatarPath = `avatars/${user_id}.jpg`;
|
||||||
|
const { data: avatarData } = await supabase.storage.from('sber.mobile').getPublicUrl(avatarPath);
|
||||||
|
|
||||||
|
if (avatarData) {
|
||||||
|
// Проверяем, существует ли файл
|
||||||
|
const { data: fileData, error: fileError } = await supabase.storage.from('sber.mobile').list('avatars', {
|
||||||
|
search: `${user_id}.jpg`
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!fileError && fileData && fileData.length > 0) {
|
||||||
|
avatarUrl = avatarData.publicUrl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
id: profileData.id,
|
id: profileData.id,
|
||||||
username: profileData.full_name,
|
username: profileData.full_name,
|
||||||
avatar_url: profileData.avatar_url,
|
avatar_url: avatarUrl || profileData.avatar_url,
|
||||||
phone: userData.user.phone,
|
phone: userData.user.phone,
|
||||||
updated_at: profileData.updated_at
|
updated_at: profileData.updated_at
|
||||||
});
|
});
|
||||||
@@ -39,15 +55,51 @@ router.post('/profile', async (req, res) => {
|
|||||||
|
|
||||||
if (userError) return res.status(400).json({ error: userError.message });
|
if (userError) return res.status(400).json({ error: userError.message });
|
||||||
|
|
||||||
|
let avatarUrl = data.avatar_url;
|
||||||
|
|
||||||
|
// Если передана аватарка в base64, сохраняем в бакет
|
||||||
|
if (data.avatarBase64) {
|
||||||
|
try {
|
||||||
|
// Удаляем старую аватарку
|
||||||
|
const oldAvatarPath = `avatars/${user_id}.jpg`;
|
||||||
|
await supabase.storage.from('sber.mobile').remove([oldAvatarPath]);
|
||||||
|
|
||||||
|
// Конвертируем base64 в buffer
|
||||||
|
const base64Data = data.avatarBase64.replace(/^data:image\/[a-z]+;base64,/, '');
|
||||||
|
const buffer = Buffer.from(base64Data, 'base64');
|
||||||
|
|
||||||
|
// Загружаем новую аватарку
|
||||||
|
const avatarPath = `avatars/${user_id}.jpg`;
|
||||||
|
const { error: uploadError } = await supabase.storage
|
||||||
|
.from('sber.mobile')
|
||||||
|
.upload(avatarPath, buffer, {
|
||||||
|
contentType: 'image/jpeg',
|
||||||
|
upsert: true
|
||||||
|
});
|
||||||
|
|
||||||
|
if (uploadError) {
|
||||||
|
console.error('Ошибка загрузки аватарки:', uploadError);
|
||||||
|
} else {
|
||||||
|
// Получаем публичный URL
|
||||||
|
const { data: urlData } = await supabase.storage
|
||||||
|
.from('sber.mobile')
|
||||||
|
.getPublicUrl(avatarPath);
|
||||||
|
avatarUrl = urlData.publicUrl;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Ошибка обработки аватарки:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let { error: profileError } = await supabase.from('user_profiles').update({
|
let { error: profileError } = await supabase.from('user_profiles').update({
|
||||||
full_name: data.username,
|
full_name: data.username,
|
||||||
avatar_url: data.avatar_url,
|
avatar_url: avatarUrl,
|
||||||
// apartment: data.apartment
|
// apartment: data.apartment
|
||||||
}).eq('id', user_id).single();
|
}).eq('id', user_id).single();
|
||||||
|
|
||||||
if (profileError) return res.status(400).json({ error: profileError.message });
|
if (profileError) return res.status(400).json({ error: profileError.message });
|
||||||
|
|
||||||
res.json({ success: true });
|
res.json({ success: true, avatar_url: avatarUrl });
|
||||||
});
|
});
|
||||||
|
|
||||||
// Получить управляющую компанию по квартире
|
// Получить управляющую компанию по квартире
|
||||||
|
|||||||
Reference in New Issue
Block a user