починили скачивание

This commit is contained in:
2026-03-06 15:22:14 +03:00
parent c5a3001e32
commit b452335370
5 changed files with 7679 additions and 2452 deletions

View File

@@ -28,6 +28,21 @@ function getStorageAuthHeaders() {
}
// [/DEF:getStorageAuthHeaders:Function]
// [DEF:encodeStoragePath:Function]
/**
* @purpose Encodes a storage-relative path preserving slash separators.
* @param {string} path - Relative storage path.
* @returns {string} Encoded path safe for URL segments.
*/
function encodeStoragePath(path) {
return String(path || '')
.split('/')
.filter((part) => part.length > 0)
.map((part) => encodeURIComponent(part))
.join('/');
}
// [/DEF:encodeStoragePath:Function]
// [DEF:listFiles:Function]
/**
* @purpose Fetches the list of files for a given category and subpath.
@@ -121,15 +136,50 @@ export async function deleteFile(category, path) {
* or the backend must allow unauthenticated downloads for valid paths.
*/
export function downloadFileUrl(category, path) {
return `${API_BASE}/download/${category}/${path}`;
const safeCategory = encodeURIComponent(String(category || ''));
const safePath = encodeStoragePath(path);
return `${API_BASE}/download/${safeCategory}/${safePath}`;
}
// [/DEF:downloadFileUrl:Function]
// [DEF:downloadFile:Function]
/**
* @purpose Downloads a file using authenticated fetch and saves it in browser.
* @param {string} category - File category.
* @param {string} path - Relative path of the file.
* @param {string} [filename] - Optional preferred filename.
* @returns {Promise<void>}
* @PRE category/path identify an existing file and user has READ permission.
* @POST Browser download is triggered or an Error is thrown.
*/
export async function downloadFile(category, path, filename) {
const response = await fetch(downloadFileUrl(category, path), {
headers: getStorageAuthHeaders(),
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.detail || `Failed to download file: ${response.statusText}`);
}
const blob = await response.blob();
const objectUrl = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = objectUrl;
link.download = filename || String(path || '').split('/').pop() || 'download';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(objectUrl);
}
// [/DEF:downloadFile:Function]
export default {
listFiles,
uploadFile,
deleteFile,
downloadFileUrl
downloadFileUrl,
downloadFile
};
// [/DEF:storageService:Module]
// [/DEF:storageService:Module]