92 lines
2.6 KiB
JavaScript
92 lines
2.6 KiB
JavaScript
// [DEF:storageService:Module]
|
|
/**
|
|
* @purpose Frontend API client for file storage management.
|
|
* @layer Service
|
|
* @relation DEPENDS_ON -> backend.api.storage
|
|
*/
|
|
|
|
const API_BASE = '/api/storage';
|
|
|
|
// [DEF:listFiles:Function]
|
|
/**
|
|
* @purpose Fetches the list of files for a given category.
|
|
* @param {string} [category] - Optional category filter.
|
|
* @returns {Promise<Array>}
|
|
*/
|
|
export async function listFiles(category) {
|
|
const params = new URLSearchParams();
|
|
if (category) {
|
|
params.append('category', category);
|
|
}
|
|
const response = await fetch(`${API_BASE}/files?${params.toString()}`);
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to fetch files: ${response.statusText}`);
|
|
}
|
|
return await response.json();
|
|
}
|
|
// [/DEF:listFiles:Function]
|
|
|
|
// [DEF:uploadFile:Function]
|
|
/**
|
|
* @purpose Uploads a file to the storage system.
|
|
* @param {File} file - The file to upload.
|
|
* @param {string} category - Target category.
|
|
* @returns {Promise<Object>}
|
|
*/
|
|
export async function uploadFile(file, category) {
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
formData.append('category', category);
|
|
|
|
const response = await fetch(`${API_BASE}/upload`, {
|
|
method: 'POST',
|
|
body: formData
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => ({}));
|
|
throw new Error(errorData.detail || `Failed to upload file: ${response.statusText}`);
|
|
}
|
|
return await response.json();
|
|
}
|
|
// [/DEF:uploadFile:Function]
|
|
|
|
// [DEF:deleteFile:Function]
|
|
/**
|
|
* @purpose Deletes a file from storage.
|
|
* @param {string} category - File category.
|
|
* @param {string} filename - Name of the file.
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export async function deleteFile(category, filename) {
|
|
const response = await fetch(`${API_BASE}/files/${category}/${filename}`, {
|
|
method: 'DELETE'
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => ({}));
|
|
throw new Error(errorData.detail || `Failed to delete file: ${response.statusText}`);
|
|
}
|
|
}
|
|
// [/DEF:deleteFile:Function]
|
|
|
|
// [DEF:downloadFileUrl:Function]
|
|
/**
|
|
* @purpose Returns the URL for downloading a file.
|
|
* @param {string} category - File category.
|
|
* @param {string} filename - Name of the file.
|
|
* @returns {string}
|
|
*/
|
|
export function downloadFileUrl(category, filename) {
|
|
return `${API_BASE}/download/${category}/${filename}`;
|
|
}
|
|
// [/DEF:downloadFileUrl:Function]
|
|
|
|
export default {
|
|
listFiles,
|
|
uploadFile,
|
|
deleteFile,
|
|
downloadFileUrl
|
|
};
|
|
|
|
// [/DEF:storageService:Module]
|