Wysyłanie zdjęć na backend i ich odbieranie

This commit is contained in:
2025-05-23 15:13:31 +02:00
parent c6fbbe9222
commit 0ee2b8d702
4 changed files with 258 additions and 163 deletions

View File

@@ -6,15 +6,43 @@ export async function listLocations() {
try {
const response = await axios.get(`${API_URL}/locations/all`);
if (!response) {
throw new Error("No locations found");
return "No locations found";
}
return response.data;
// Нормализуем изображения в полученных данных
return response.data.map(location => ({
...location,
imageSource: normalizeImageSource(location.image)
}));
} catch (error) {
console.error("Error fetching locations:", error);
throw error;
}
}
const normalizeImageSource = (image) => {
if (!image) return null;
// Проверка, является ли изображение URL
if (typeof image === 'string') {
// Если строка начинается с http или https, это URL
if (image.startsWith('http://') || image.startsWith('https://')) {
return { uri: image };
}
// Проверка на base64
if (image.startsWith('data:image')) {
return { uri: image };
} else if (image.length > 100) {
// Предполагаем, что это base64 без префикса
return { uri: `data:image/jpeg;base64,${image}` };
}
}
// Возвращаем null для неправильного формата
return null;
}
export async function getLocation(id) {
try {
const location = await axios.get(`${API_URL}/locations/${id}`);
@@ -22,7 +50,7 @@ export async function getLocation(id) {
throw new Error("Location not found");
}
return location.data;
return [...location.data, {image: normalizeImageSource(location.image)}];
} catch (error) {
console.error("Error fetching location:", error);
throw error;