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

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

@@ -13,11 +13,16 @@
// [SECTION: IMPORTS]
import { createEventDispatcher } from 'svelte';
import { downloadFileUrl } from '../../services/storageService';
import { t } from '../../lib/i18n';
// [/SECTION: IMPORTS]
export let files = [];
const dispatch = createEventDispatcher();
function isDirectory(file) {
return file.mime_type === 'directory';
}
function formatSize(bytes) {
if (bytes === 0) return '0 B';
const k = 1024;
@@ -36,40 +41,63 @@
<table class="min-w-full bg-white border border-gray-200">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Name</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Category</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Size</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Created At</th>
<th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">{$t.storage.table.name}</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">{$t.storage.table.category}</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">{$t.storage.table.size}</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">{$t.storage.table.created_at}</th>
<th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">{$t.storage.table.actions}</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200">
{#each files as file}
<tr class="hover:bg-gray-50">
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">{file.name}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
{#if isDirectory(file)}
<button
on:click={() => dispatch('navigate', file.path)}
class="flex items-center text-indigo-600 hover:text-indigo-900"
>
<svg class="h-5 w-5 mr-2 text-yellow-400" fill="currentColor" viewBox="0 0 20 20">
<path d="M2 6a2 2 0 012-2h5l2 2h5a2 2 0 012 2v6a2 2 0 01-2 2H4a2 2 0 01-2-2V6z" />
</svg>
{file.name}
</button>
{:else}
<div class="flex items-center">
<svg class="h-5 w-5 mr-2 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z" />
</svg>
{file.name}
</div>
{/if}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 capitalize">{file.category}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{formatSize(file.size)}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{isDirectory(file) ? '--' : formatSize(file.size)}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{formatDate(file.created_at)}</td>
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
<a
href={downloadFileUrl(file.category, file.name)}
download={file.name}
class="text-indigo-600 hover:text-indigo-900 mr-4"
>
Download
</a>
<button
on:click={() => dispatch('delete', { category: file.category, filename: file.name })}
{#if !isDirectory(file)}
<a
href={downloadFileUrl(file.category, file.path)}
download={file.name}
class="text-indigo-600 hover:text-indigo-900 mr-4"
>
{$t.storage.table.download}
</a>
{/if}
<button
on:click={() => dispatch('delete', { category: file.category, path: file.path, name: file.name })}
class="text-red-600 hover:text-red-900"
>
Delete
{$t.storage.table.delete}
</button>
</td>
</tr>
{:else}
<tr>
<td colspan="5" class="px-6 py-10 text-center text-sm text-gray-500">
No files found.
{$t.storage.no_files}
</td>
</tr>
{/each}

View File

@@ -14,6 +14,7 @@
import { createEventDispatcher } from 'svelte';
import { uploadFile } from '../../services/storageService';
import { addToast } from '../../lib/toasts';
import { t } from '../../lib/i18n';
// [/SECTION: IMPORTS]
// [DEF:handleUpload:Function]
@@ -24,7 +25,8 @@
*/
const dispatch = createEventDispatcher();
let fileInput;
let category = 'backup';
export let category = 'backups';
export let path = '';
let isUploading = false;
let dragOver = false;
@@ -34,12 +36,18 @@
isUploading = true;
try {
await uploadFile(file, category);
addToast(`File ${file.name} uploaded successfully.`, 'success');
// path is relative to root, but upload endpoint expects path within category
// FileList.path is like "backup/folder", we need just "folder"
const subpath = path.startsWith(category)
? path.substring(category.length).replace(/^\/+/, '')
: path;
await uploadFile(file, category, subpath);
addToast($t.storage.messages.upload_success.replace('{name}', file.name), 'success');
fileInput.value = '';
dispatch('uploaded');
} catch (error) {
addToast(`Upload failed: ${error.message}`, 'error');
addToast($t.storage.messages.upload_failed.replace('{error}', error.message), 'error');
} finally {
isUploading = false;
}
@@ -65,17 +73,17 @@
<!-- [SECTION: TEMPLATE] -->
<div class="bg-white p-6 rounded-lg border border-gray-200 shadow-sm">
<h2 class="text-lg font-semibold mb-4">Upload File</h2>
<h2 class="text-lg font-semibold mb-4">{$t.storage.upload_title}</h2>
<div class="space-y-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Target Category</label>
<select
<label class="block text-sm font-medium text-gray-700 mb-1">{$t.storage.target_category}</label>
<select
bind:value={category}
class="block w-full border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
>
<option value="backup">Backup</option>
<option value="repository">Repository</option>
<option value="backups">{$t.storage.backups}</option>
<option value="repositorys">{$t.storage.repositories}</option>
</select>
</div>
@@ -92,8 +100,8 @@
</svg>
<div class="flex text-sm text-gray-600">
<label for="file-upload" class="relative cursor-pointer bg-white rounded-md font-medium text-indigo-600 hover:text-indigo-500 focus-within:outline-none focus-within:ring-2 focus-within:ring-offset-2 focus-within:ring-indigo-500">
<span>Upload a file</span>
<input
<span>{$t.storage.upload_button}</span>
<input
id="file-upload"
name="file-upload"
type="file"
@@ -103,16 +111,16 @@
disabled={isUploading}
>
</label>
<p class="pl-1">or drag and drop</p>
<p class="pl-1">{$t.storage.drag_drop}</p>
</div>
<p class="text-xs text-gray-500">ZIP, YAML, JSON up to 50MB</p>
<p class="text-xs text-gray-500">{$t.storage.supported_formats}</p>
</div>
</div>
{#if isUploading}
<div class="flex items-center justify-center space-x-2 text-indigo-600">
<div class="animate-spin rounded-full h-4 w-4 border-b-2 border-indigo-600"></div>
<span class="text-sm font-medium">Uploading...</span>
<span class="text-sm font-medium">{$t.storage.uploading}</span>
</div>
{/if}
</div>

View File

@@ -20,7 +20,6 @@
let settings = {
environments: [],
settings: {
backup_path: '',
default_environment_id: null,
logging: {
level: 'INFO',
@@ -204,12 +203,6 @@
<section class="mb-8 bg-white p-6 rounded shadow">
<h2 class="text-xl font-semibold mb-4">Global Settings</h2>
<div class="grid grid-cols-1 gap-4">
<div>
<label for="backup_path" class="block text-sm font-medium text-gray-700">Backup Storage Path</label>
<input type="text" id="backup_path" bind:value={settings.settings.backup_path} class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2" />
</div>
</div>
<h3 class="text-lg font-medium mb-4 mt-6">Logging Configuration</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">

View File

@@ -170,19 +170,6 @@
</div>
{/if}
<div class="mb-8">
<Card title={$t.settings?.global_title || "Global Settings"}>
<div class="grid grid-cols-1 gap-6">
<Input
label={$t.settings?.backup_path || "Backup Storage Path"}
bind:value={settings.settings.backup_path}
/>
<Button on:click={handleSaveGlobal}>
{$t.common.save}
</Button>
</div>
</Card>
</div>
<div class="mb-8">
<Card title={$t.settings?.storage_title || "File Storage Configuration"}>

View File

@@ -18,7 +18,6 @@ export async function load() {
settings: {
environments: [],
settings: {
backup_path: '',
default_environment_id: null
}
},

View File

@@ -15,6 +15,7 @@
import { onMount } from 'svelte';
import { listFiles, deleteFile } from '../../../services/storageService';
import { addToast } from '../../../lib/toasts';
import { t } from '../../../lib/i18n';
import FileList from '../../../components/storage/FileList.svelte';
import FileUpload from '../../../components/storage/FileUpload.svelte';
// [/SECTION: IMPORTS]
@@ -26,15 +27,30 @@
*/
let files = [];
let isLoading = false;
let activeTab = 'all';
let activeTab = 'backups';
let currentPath = 'backups'; // Relative to storage root
async function loadFiles() {
isLoading = true;
try {
const category = activeTab === 'all' ? null : activeTab;
files = await listFiles(category);
const category = activeTab;
// If we have a currentPath, we use it.
// But if user switched tabs, we should reset currentPath to category root
let effectivePath = currentPath;
if (category && !currentPath.startsWith(category)) {
effectivePath = category;
currentPath = category;
}
// API expects path relative to category root if category is provided
const subpath = (category && effectivePath.startsWith(category))
? effectivePath.substring(category.length).replace(/^\/+/, '')
: effectivePath;
files = await listFiles(category, subpath);
} catch (error) {
addToast(`Failed to load files: ${error.message}`, 'error');
addToast($t.storage.messages.load_failed.replace('{error}', error.message), 'error');
} finally {
isLoading = false;
}
@@ -44,39 +60,73 @@
// [DEF:handleDelete:Function]
/**
* @purpose Handles the file deletion process.
* @param {CustomEvent} event - The delete event containing category and filename.
* @param {CustomEvent} event - The delete event containing category and path.
*/
async function handleDelete(event) {
const { category, filename } = event.detail;
if (!confirm(`Are you sure you want to delete ${filename}?`)) return;
const { category, path, name } = event.detail;
if (!confirm($t.storage.messages.delete_confirm.replace('{name}', name))) return;
try {
await deleteFile(category, filename);
addToast(`File ${filename} deleted.`, 'success');
await deleteFile(category, path);
addToast($t.storage.messages.delete_success.replace('{name}', name), 'success');
await loadFiles();
} catch (error) {
addToast(`Delete failed: ${error.message}`, 'error');
addToast($t.storage.messages.delete_failed.replace('{error}', error.message), 'error');
}
}
// [/DEF:handleDelete:Function]
function handleNavigate(event) {
currentPath = event.detail;
loadFiles();
}
function navigateUp() {
if (!currentPath || currentPath === activeTab) return;
const parts = currentPath.split('/');
parts.pop();
currentPath = parts.join('/') || '';
loadFiles();
}
onMount(loadFiles);
$: if (activeTab) {
// Reset path when switching tabs
if (!currentPath.startsWith(activeTab)) {
currentPath = activeTab;
}
loadFiles();
}
</script>
<!-- [SECTION: TEMPLATE] -->
<div class="container mx-auto p-4 max-w-6xl">
<div class="flex justify-between items-center mb-6">
<h1 class="text-2xl font-bold text-gray-900">File Storage Management</h1>
<button
<div class="mb-6">
<h1 class="text-2xl font-bold text-gray-900">{$t.storage.management}</h1>
{#if currentPath}
<div class="flex items-center mt-2 text-sm text-gray-500">
<button on:click={() => { currentPath = activeTab; loadFiles(); }} class="hover:text-indigo-600">{$t.storage.root}</button>
{#each currentPath.split('/').slice(1) as part, i}
<span class="mx-2">/</span>
<button
on:click={() => { currentPath = currentPath.split('/').slice(0, i + 1).join('/'); loadFiles(); }}
class="hover:text-indigo-600 capitalize"
>
{part}
</button>
{/each}
</div>
{/if}
</div>
<div class="flex justify-end mb-4">
<button
on:click={loadFiles}
class="inline-flex items-center px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50"
disabled={isLoading}
>
{isLoading ? 'Refreshing...' : 'Refresh'}
{isLoading ? $t.storage.refreshing : $t.storage.refresh}
</button>
</div>
@@ -86,33 +136,45 @@
<!-- Tabs -->
<div class="border-b border-gray-200">
<nav class="-mb-px flex space-x-8">
<button
on:click={() => activeTab = 'all'}
class="py-4 px-1 border-b-2 font-medium text-sm {activeTab === 'all' ? 'border-indigo-500 text-indigo-600' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'}"
<button
on:click={() => activeTab = 'backups'}
class="py-4 px-1 border-b-2 font-medium text-sm {activeTab === 'backups' ? 'border-indigo-500 text-indigo-600' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'}"
>
All Files
{$t.storage.backups}
</button>
<button
on:click={() => activeTab = 'backup'}
class="py-4 px-1 border-b-2 font-medium text-sm {activeTab === 'backup' ? 'border-indigo-500 text-indigo-600' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'}"
<button
on:click={() => activeTab = 'repositorys'}
class="py-4 px-1 border-b-2 font-medium text-sm {activeTab === 'repositorys' ? 'border-indigo-500 text-indigo-600' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'}"
>
Backups
</button>
<button
on:click={() => activeTab = 'repository'}
class="py-4 px-1 border-b-2 font-medium text-sm {activeTab === 'repository' ? 'border-indigo-500 text-indigo-600' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'}"
>
Repositories
{$t.storage.repositories}
</button>
</nav>
</div>
<FileList {files} on:delete={handleDelete} />
<div class="flex items-center mb-2">
{#if currentPath && currentPath !== activeTab}
<button
on:click={navigateUp}
class="mr-4 inline-flex items-center px-3 py-1 border border-gray-300 shadow-sm text-xs font-medium rounded text-gray-700 bg-white hover:bg-gray-50"
>
<svg class="h-4 w-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18" />
</svg>
Back
</button>
{/if}
</div>
<FileList {files} on:delete={handleDelete} on:navigate={handleNavigate} />
</div>
<!-- Sidebar: Upload -->
<div class="lg:col-span-1">
<FileUpload on:uploaded={loadFiles} />
<FileUpload
category={activeTab}
path={currentPath}
on:uploaded={loadFiles}
/>
</div>
</div>
</div>

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]