85 lines
No EOL
2.5 KiB
JavaScript
85 lines
No EOL
2.5 KiB
JavaScript
import apiClient from './api';
|
|
|
|
export const imageService = {
|
|
/**
|
|
* Upload a single product image (admin only)
|
|
* @param {File} imageFile - The image file to upload
|
|
* @returns {Promise} Promise with the API response
|
|
*/
|
|
uploadProductImage: async (imageFile) => {
|
|
const formData = new FormData();
|
|
formData.append('image', imageFile);
|
|
|
|
try {
|
|
const response = await apiClient.post('/images/admin/products', formData, {
|
|
headers: {
|
|
'Content-Type': 'multipart/form-data'
|
|
}
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error.response?.data || { message: 'Failed to upload image' };
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Upload multiple product images (admin only)
|
|
* @param {Array<File>} imageFiles - The image files to upload
|
|
* @returns {Promise} Promise with the API response
|
|
*/
|
|
uploadMultipleProductImages: async (imageFiles) => {
|
|
const formData = new FormData();
|
|
|
|
imageFiles.forEach(file => {
|
|
formData.append('images', file);
|
|
});
|
|
|
|
try {
|
|
const response = await apiClient.post('/images/admin/products/multiple', formData, {
|
|
headers: {
|
|
'Content-Type': 'multipart/form-data'
|
|
}
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error.response?.data || { message: 'Failed to upload images' };
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Delete a product image (admin only)
|
|
* @param {string} filename - The filename to delete
|
|
* @returns {Promise} Promise with the API response
|
|
*/
|
|
deleteProductImage: async (filename) => {
|
|
try {
|
|
const response = await apiClient.delete(`/images/admin/products/${filename}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error.response?.data || { message: 'Failed to delete image' };
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Get the full path to an image
|
|
* @param {string} imagePath - The relative image path
|
|
* @returns {string} The full image URL
|
|
*/
|
|
getImageUrl: (imagePath) => {
|
|
if (!imagePath) return '/images/placeholder.jpg';
|
|
|
|
// If it's already a full URL, return it
|
|
if (imagePath.startsWith('http')) return imagePath;
|
|
|
|
// If it's a relative path, add the API base URL
|
|
// The API base URL is either from the environment or '/api'
|
|
const baseUrl = import.meta.env.VITE_API_URL || '/api';
|
|
|
|
// If the path already starts with a slash, remove it to avoid double slashes
|
|
const cleanPath = imagePath.startsWith('/') ? imagePath.substring(1) : imagePath;
|
|
|
|
return `${baseUrl}/${cleanPath}`;
|
|
}
|
|
};
|
|
|
|
export default imageService; |