Файловое хранилище готово

This commit is contained in:
2026-01-26 11:08:18 +03:00
parent a542e7d2df
commit edf9286071
35 changed files with 377 additions and 497 deletions

View File

@@ -9,15 +9,19 @@ const API_BASE = '/api/storage';
// [DEF:listFiles:Function]
/**
* @purpose Fetches the list of files for a given category.
* @purpose Fetches the list of files for a given category and subpath.
* @param {string} [category] - Optional category filter.
* @param {string} [path] - Optional subpath filter.
* @returns {Promise<Array>}
*/
export async function listFiles(category) {
export async function listFiles(category, path) {
const params = new URLSearchParams();
if (category) {
params.append('category', category);
}
if (path) {
params.append('path', path);
}
const response = await fetch(`${API_BASE}/files?${params.toString()}`);
if (!response.ok) {
throw new Error(`Failed to fetch files: ${response.statusText}`);
@@ -31,12 +35,16 @@ export async function listFiles(category) {
* @purpose Uploads a file to the storage system.
* @param {File} file - The file to upload.
* @param {string} category - Target category.
* @param {string} [path] - Target subpath.
* @returns {Promise<Object>}
*/
export async function uploadFile(file, category) {
export async function uploadFile(file, category, path) {
const formData = new FormData();
formData.append('file', file);
formData.append('category', category);
if (path) {
formData.append('path', path);
}
const response = await fetch(`${API_BASE}/upload`, {
method: 'POST',
@@ -53,19 +61,19 @@ export async function uploadFile(file, category) {
// [DEF:deleteFile:Function]
/**
* @purpose Deletes a file from storage.
* @purpose Deletes a file or directory from storage.
* @param {string} category - File category.
* @param {string} filename - Name of the file.
* @param {string} path - Relative path of the item.
* @returns {Promise<void>}
*/
export async function deleteFile(category, filename) {
const response = await fetch(`${API_BASE}/files/${category}/${filename}`, {
export async function deleteFile(category, path) {
const response = await fetch(`${API_BASE}/files/${category}/${path}`, {
method: 'DELETE'
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.detail || `Failed to delete file: ${response.statusText}`);
throw new Error(errorData.detail || `Failed to delete: ${response.statusText}`);
}
}
// [/DEF:deleteFile:Function]
@@ -74,11 +82,11 @@ export async function deleteFile(category, filename) {
/**
* @purpose Returns the URL for downloading a file.
* @param {string} category - File category.
* @param {string} filename - Name of the file.
* @param {string} path - Relative path of the file.
* @returns {string}
*/
export function downloadFileUrl(category, filename) {
return `${API_BASE}/download/${category}/${filename}`;
export function downloadFileUrl(category, path) {
return `${API_BASE}/download/${category}/${path}`;
}
// [/DEF:downloadFileUrl:Function]