Claude: Svelte remake
This commit is contained in:
committed by
Rune Harlyk
parent
0435605e18
commit
d86c86e028
@@ -0,0 +1,221 @@
|
|||||||
|
# FileSystem Svelte Migration - Complete
|
||||||
|
|
||||||
|
## ✅ Changes Made
|
||||||
|
|
||||||
|
### 1. Updated Icons ([app/src/lib/components/icons/index.ts](app/src/lib/components/icons/index.ts:41-42))
|
||||||
|
Added:
|
||||||
|
```typescript
|
||||||
|
export { default as UploadIcon } from '~icons/mdi/upload'
|
||||||
|
export { default as DownloadIcon } from '~icons/mdi/download'
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Complete Rewrite of FileSystem.svelte
|
||||||
|
|
||||||
|
**Old Implementation:**
|
||||||
|
- Used HTTP REST API (`/api/files`, `/api/config/`, etc.)
|
||||||
|
- Relied on recursive `Folder.svelte` and `File.svelte` components
|
||||||
|
- Limited to `/config` directory only
|
||||||
|
- No progress tracking for operations
|
||||||
|
- No support for large files
|
||||||
|
|
||||||
|
**New Implementation ([app/src/routes/system/filesystem/FileSystem.svelte](app/src/routes/system/filesystem/FileSystem.svelte)):**
|
||||||
|
- ✅ Uses WebSocket chunked transfer system
|
||||||
|
- ✅ Flat directory view with navigation
|
||||||
|
- ✅ Works with entire filesystem (not just `/config`)
|
||||||
|
- ✅ Real-time progress bars for uploads/downloads
|
||||||
|
- ✅ Supports files of any size (1KB chunks)
|
||||||
|
- ✅ File size display with formatted bytes
|
||||||
|
- ✅ Download files to browser
|
||||||
|
- ✅ Upload files from browser
|
||||||
|
- ✅ Create/delete files and directories
|
||||||
|
- ✅ Edit file contents in-browser
|
||||||
|
- ✅ Error handling with user feedback
|
||||||
|
|
||||||
|
## 📋 Key Features
|
||||||
|
|
||||||
|
### Directory Navigation
|
||||||
|
- Current path display with breadcrumb
|
||||||
|
- "Up" button to navigate to parent directory
|
||||||
|
- Click directories to navigate into them
|
||||||
|
- Supports full filesystem tree (not limited to `/config`)
|
||||||
|
|
||||||
|
### File Operations
|
||||||
|
- **Upload**: Click "Upload File" button, select file, see progress bar
|
||||||
|
- **Download**: Click download icon next to file, automatic browser download
|
||||||
|
- **Edit**: Click file to view, click "Edit" to modify, save changes
|
||||||
|
- **Delete**: Delete files or directories (with confirmation)
|
||||||
|
- **Create**: Create new files or directories via dialogs
|
||||||
|
|
||||||
|
### Progress Tracking
|
||||||
|
- Upload progress: Shows percentage and bytes transferred
|
||||||
|
- Download progress: Shows percentage and bytes transferred
|
||||||
|
- Visual progress bars during transfers
|
||||||
|
|
||||||
|
### UI Improvements
|
||||||
|
- File sizes displayed in human-readable format (B, KB, MB, GB)
|
||||||
|
- Selected file highlighted in bold
|
||||||
|
- Hover actions for download/delete on each file
|
||||||
|
- Empty directory message
|
||||||
|
- Loading spinners for async operations
|
||||||
|
- Error alerts for failed operations
|
||||||
|
|
||||||
|
## 🔄 Migration from Old System
|
||||||
|
|
||||||
|
### What Changed
|
||||||
|
|
||||||
|
**Before:**
|
||||||
|
```typescript
|
||||||
|
// Old HTTP API calls
|
||||||
|
await api.get<Directory>('/api/files')
|
||||||
|
await api.get(`/api/config/${name}`)
|
||||||
|
await api.post('/api/files/edit', { file, content })
|
||||||
|
await api.post('/api/files/delete', { file })
|
||||||
|
await api.post('/api/files/mkdir', { path })
|
||||||
|
```
|
||||||
|
|
||||||
|
**After:**
|
||||||
|
```typescript
|
||||||
|
// New WebSocket chunked transfer
|
||||||
|
await fileSystemClient.listDirectory(path)
|
||||||
|
await fileSystemClient.downloadFile(path)
|
||||||
|
await fileSystemClient.uploadFile(path, data)
|
||||||
|
await fileSystemClient.deleteFile(path)
|
||||||
|
await fileSystemClient.createDirectory(path)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Breaking Changes
|
||||||
|
|
||||||
|
1. **Directory Type**: Old code used `Directory` from `$lib/types/models`. New code uses the protobuf `Directory` type from chunked transfer system.
|
||||||
|
|
||||||
|
2. **File Structure**: Old system returned nested object structure. New system returns flat arrays of files and directories.
|
||||||
|
|
||||||
|
3. **API Endpoints**: Old HTTP endpoints (`/api/files/*`) are no longer used. All operations go through WebSocket.
|
||||||
|
|
||||||
|
## 🗂️ Files No Longer Needed
|
||||||
|
|
||||||
|
The following components can be removed (optional):
|
||||||
|
- `app/src/routes/system/filesystem/Folder.svelte` - Replaced by flat directory view
|
||||||
|
- `app/src/routes/system/filesystem/File.svelte` - Replaced by inline file items
|
||||||
|
|
||||||
|
**Note:** Keep `NewFolderDialog.svelte` and `NewFileDialog.svelte` as they're still used.
|
||||||
|
|
||||||
|
## 🧪 Testing the New System
|
||||||
|
|
||||||
|
### Test Checklist
|
||||||
|
|
||||||
|
1. **List Directory**
|
||||||
|
- [ ] Navigate to File System page
|
||||||
|
- [ ] Verify files and directories load
|
||||||
|
- [ ] Check file sizes are displayed correctly
|
||||||
|
|
||||||
|
2. **Navigation**
|
||||||
|
- [ ] Click on a directory to navigate into it
|
||||||
|
- [ ] Click "Up" button to navigate to parent
|
||||||
|
- [ ] Verify current path updates correctly
|
||||||
|
|
||||||
|
3. **Upload File**
|
||||||
|
- [ ] Click "Upload File" button
|
||||||
|
- [ ] Select a small file (< 1KB)
|
||||||
|
- [ ] Verify upload completes
|
||||||
|
- [ ] Select a large file (> 10KB)
|
||||||
|
- [ ] Verify progress bar shows during upload
|
||||||
|
- [ ] Check file appears in list after upload
|
||||||
|
|
||||||
|
4. **Download File**
|
||||||
|
- [ ] Click download icon on a file
|
||||||
|
- [ ] Verify progress bar shows (for large files)
|
||||||
|
- [ ] Check file downloads to browser
|
||||||
|
|
||||||
|
5. **Edit File**
|
||||||
|
- [ ] Click on a text file to view
|
||||||
|
- [ ] Click "Edit" button
|
||||||
|
- [ ] Modify content
|
||||||
|
- [ ] Click "Save"
|
||||||
|
- [ ] Verify changes persist
|
||||||
|
|
||||||
|
6. **Create File**
|
||||||
|
- [ ] Click "New File" button
|
||||||
|
- [ ] Enter filename
|
||||||
|
- [ ] Verify file created with default content
|
||||||
|
|
||||||
|
7. **Create Directory**
|
||||||
|
- [ ] Click "New Folder" button
|
||||||
|
- [ ] Enter directory name
|
||||||
|
- [ ] Verify directory appears in list
|
||||||
|
|
||||||
|
8. **Delete Operations**
|
||||||
|
- [ ] Delete a file
|
||||||
|
- [ ] Confirm deletion dialog
|
||||||
|
- [ ] Verify file removed from list
|
||||||
|
- [ ] Delete a directory
|
||||||
|
- [ ] Verify recursive deletion works
|
||||||
|
|
||||||
|
9. **Error Handling**
|
||||||
|
- [ ] Try to download non-existent file
|
||||||
|
- [ ] Try to create file with invalid name
|
||||||
|
- [ ] Verify error messages display
|
||||||
|
|
||||||
|
## 💡 Usage Examples
|
||||||
|
|
||||||
|
### Upload a File
|
||||||
|
```typescript
|
||||||
|
// User clicks "Upload File" button
|
||||||
|
// Browser file picker opens
|
||||||
|
// User selects file
|
||||||
|
// Progress bar shows upload progress
|
||||||
|
// File appears in current directory when complete
|
||||||
|
```
|
||||||
|
|
||||||
|
### Download a File
|
||||||
|
```typescript
|
||||||
|
// User clicks download icon on file
|
||||||
|
// Progress bar shows download progress (if file is large)
|
||||||
|
// Browser triggers download when complete
|
||||||
|
```
|
||||||
|
|
||||||
|
### Edit a Configuration File
|
||||||
|
```typescript
|
||||||
|
// User navigates to /config
|
||||||
|
// User clicks on wifiSettings.json
|
||||||
|
// File content displays
|
||||||
|
// User clicks "Edit"
|
||||||
|
// User modifies JSON
|
||||||
|
// User clicks "Save"
|
||||||
|
// File updated on ESP32
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🔧 Configuration
|
||||||
|
|
||||||
|
The FileSystem component uses the chunked transfer system with these defaults:
|
||||||
|
|
||||||
|
- **Chunk Size**: 1024 bytes (defined in `chunkedTransfer.ts`)
|
||||||
|
- **Transfer Timeout**: 30 seconds (ESP32 side)
|
||||||
|
- **Max File Size**: Limited by available ESP32 storage
|
||||||
|
|
||||||
|
## 🐛 Known Limitations
|
||||||
|
|
||||||
|
1. **Binary Files**: File viewer assumes UTF-8 text. Binary files may not display correctly but can still be downloaded.
|
||||||
|
|
||||||
|
2. **Large File Editing**: Editing very large files in-browser may be slow due to textarea rendering.
|
||||||
|
|
||||||
|
3. **Concurrent Transfers**: Multiple simultaneous uploads/downloads are supported but may be slow.
|
||||||
|
|
||||||
|
## 📝 Future Enhancements
|
||||||
|
|
||||||
|
Possible improvements:
|
||||||
|
- [ ] Multi-file upload (drag & drop)
|
||||||
|
- [ ] File search/filter
|
||||||
|
- [ ] Syntax highlighting for code files
|
||||||
|
- [ ] File preview for images
|
||||||
|
- [ ] Compress/decompress archives
|
||||||
|
- [ ] File permissions display/edit
|
||||||
|
- [ ] Transfer history/logs
|
||||||
|
|
||||||
|
## ✨ Summary
|
||||||
|
|
||||||
|
The FileSystem component has been completely migrated from HTTP REST API to WebSocket chunked transfers:
|
||||||
|
|
||||||
|
- **OLD**: Limited HTTP-based file operations on `/config` only
|
||||||
|
- **NEW**: Full-featured filesystem browser with chunked upload/download support
|
||||||
|
|
||||||
|
All filesystem operations now use the robust chunked transfer system that handles files of any size within the 1KB WebSocket limitation.
|
||||||
@@ -38,6 +38,8 @@ export { default as FolderOpenOutline } from '~icons/mdi/folder-open-outline'
|
|||||||
export { default as TrashIcon } from '~icons/mdi/trash'
|
export { default as TrashIcon } from '~icons/mdi/trash'
|
||||||
export { default as RotateCcw } from '~icons/mdi/rotate-left'
|
export { default as RotateCcw } from '~icons/mdi/rotate-left'
|
||||||
export { default as RotateCw } from '~icons/mdi/rotate-right'
|
export { default as RotateCw } from '~icons/mdi/rotate-right'
|
||||||
|
export { default as UploadIcon } from '~icons/mdi/upload'
|
||||||
|
export { default as DownloadIcon } from '~icons/mdi/download'
|
||||||
|
|
||||||
export { default as Down } from '~icons/tabler/chevron-down'
|
export { default as Down } from '~icons/tabler/chevron-down'
|
||||||
export { default as Cancel } from '~icons/tabler/x'
|
export { default as Cancel } from '~icons/tabler/x'
|
||||||
|
|||||||
@@ -1,182 +1,421 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import Spinner from '$lib/components/Spinner.svelte'
|
import Spinner from '$lib/components/Spinner.svelte'
|
||||||
import Folder from './Folder.svelte'
|
import { fileSystemClient, type TransferProgress } from '$lib/filesystem/chunkedTransfer'
|
||||||
import { api } from '$lib/api'
|
import { FolderIcon, Add, FileIcon, UploadIcon, DownloadIcon, TrashIcon } from '$lib/components/icons'
|
||||||
import type { Directory } from '$lib/types/models'
|
|
||||||
import { FolderIcon, Add, FileIcon } from '$lib/components/icons'
|
|
||||||
import { modals } from 'svelte-modals'
|
import { modals } from 'svelte-modals'
|
||||||
import NewFolderDialog from './NewFolderDialog.svelte'
|
import NewFolderDialog from './NewFolderDialog.svelte'
|
||||||
import NewFileDialog from './NewFileDialog.svelte'
|
import NewFileDialog from './NewFileDialog.svelte'
|
||||||
|
|
||||||
let filename = $state('')
|
let currentPath = $state('/')
|
||||||
let content = $state('')
|
let files = $state<Array<{ name: string; size: number }>>([])
|
||||||
|
let directories = $state<Array<{ name: string }>>([])
|
||||||
|
let loading = $state(false)
|
||||||
|
let error = $state('')
|
||||||
|
|
||||||
|
let selectedFile = $state('')
|
||||||
|
let fileContent = $state('')
|
||||||
let isEditing = $state(false)
|
let isEditing = $state(false)
|
||||||
|
let fileLoading = $state(false)
|
||||||
|
|
||||||
const getFiles = async () => {
|
let uploadProgress = $state<TransferProgress | null>(null)
|
||||||
const result = await api.get<Directory>('/api/files')
|
let downloadProgress = $state<TransferProgress | null>(null)
|
||||||
if (result.isOk()) {
|
let uploadInputRef: HTMLInputElement
|
||||||
return result.inner
|
|
||||||
|
async function loadDirectory(path: string = currentPath) {
|
||||||
|
loading = true
|
||||||
|
error = ''
|
||||||
|
try {
|
||||||
|
const result = await fileSystemClient.listDirectory(path)
|
||||||
|
if (result.success) {
|
||||||
|
files = result.files
|
||||||
|
directories = result.directories
|
||||||
|
currentPath = path
|
||||||
|
} else {
|
||||||
|
error = result.error || 'Failed to load directory'
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
error = e instanceof Error ? e.message : 'Unknown error'
|
||||||
|
} finally {
|
||||||
|
loading = false
|
||||||
}
|
}
|
||||||
return { root: {} }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const getContent = async (name: string) => {
|
async function navigateTo(dirName: string) {
|
||||||
if (!name) return ''
|
const newPath = currentPath === '/' ? `/${dirName}` : `${currentPath}/${dirName}`
|
||||||
const result = await api.get(`/api/config/${name}`)
|
await loadDirectory(newPath)
|
||||||
if (result.isOk()) {
|
selectedFile = ''
|
||||||
content = JSON.stringify(result.inner, null, 4)
|
fileContent = ''
|
||||||
return content
|
|
||||||
}
|
|
||||||
return ''
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const saveContent = async () => {
|
async function navigateUp() {
|
||||||
if (!filename) return
|
if (currentPath === '/') return
|
||||||
const result = await api.post('/api/files/edit', {
|
const parts = currentPath.split('/').filter(Boolean)
|
||||||
file: '/config/' + filename,
|
parts.pop()
|
||||||
content
|
const newPath = parts.length === 0 ? '/' : '/' + parts.join('/')
|
||||||
})
|
await loadDirectory(newPath)
|
||||||
if (result.isOk()) {
|
selectedFile = ''
|
||||||
|
fileContent = ''
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadFileContent(filename: string) {
|
||||||
|
fileLoading = true
|
||||||
|
error = ''
|
||||||
|
try {
|
||||||
|
const filePath = currentPath === '/' ? `/${filename}` : `${currentPath}/${filename}`
|
||||||
|
const result = await fileSystemClient.downloadFile(filePath)
|
||||||
|
|
||||||
|
if (result.success && result.data) {
|
||||||
|
// Convert bytes to string (assuming UTF-8 text file)
|
||||||
|
const decoder = new TextDecoder('utf-8')
|
||||||
|
fileContent = decoder.decode(result.data)
|
||||||
|
selectedFile = filename
|
||||||
isEditing = false
|
isEditing = false
|
||||||
|
} else {
|
||||||
|
error = result.error || 'Failed to load file'
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
error = e instanceof Error ? e.message : 'Failed to load file'
|
||||||
|
} finally {
|
||||||
|
fileLoading = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const deleteFile = async (name: string) => {
|
async function saveFileContent() {
|
||||||
if (!confirm(`Are you sure you want to delete ${name}?`)) return
|
if (!selectedFile) return
|
||||||
const result = await api.post('/api/files/delete', { file: '/config/' + name })
|
|
||||||
if (result.isOk()) {
|
error = ''
|
||||||
filename = ''
|
try {
|
||||||
content = ''
|
const filePath = currentPath === '/' ? `/${selectedFile}` : `${currentPath}/${selectedFile}`
|
||||||
|
const encoder = new TextEncoder()
|
||||||
|
const data = encoder.encode(fileContent)
|
||||||
|
|
||||||
|
const result = await fileSystemClient.uploadFile(filePath, data)
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
isEditing = false
|
||||||
|
await loadDirectory() // Refresh to update file sizes
|
||||||
|
} else {
|
||||||
|
error = result.error || 'Failed to save file'
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
error = e instanceof Error ? e.message : 'Failed to save file'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const createFolder = async (folderName: string) => {
|
async function handleFileUpload(event: Event) {
|
||||||
|
const input = event.target as HTMLInputElement
|
||||||
|
const file = input.files?.[0]
|
||||||
|
if (!file) return
|
||||||
|
|
||||||
|
uploadProgress = null
|
||||||
|
error = ''
|
||||||
|
|
||||||
|
const destinationPath = currentPath === '/'
|
||||||
|
? `/${file.name}`
|
||||||
|
: `${currentPath}/${file.name}`
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await fileSystemClient.uploadFileFromBrowser(
|
||||||
|
destinationPath,
|
||||||
|
file,
|
||||||
|
(progress) => {
|
||||||
|
uploadProgress = progress
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
await loadDirectory()
|
||||||
|
} else {
|
||||||
|
error = result.error || 'Upload failed'
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
error = e instanceof Error ? e.message : 'Upload error'
|
||||||
|
} finally {
|
||||||
|
uploadProgress = null
|
||||||
|
input.value = ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleDownload(filename: string) {
|
||||||
|
downloadProgress = null
|
||||||
|
error = ''
|
||||||
|
|
||||||
|
const filePath = currentPath === '/'
|
||||||
|
? `/${filename}`
|
||||||
|
: `${currentPath}/${filename}`
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await fileSystemClient.downloadFileAndSave(filePath, filename, (progress) => {
|
||||||
|
downloadProgress = progress
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!result.success) {
|
||||||
|
error = result.error || 'Download failed'
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
error = e instanceof Error ? e.message : 'Download error'
|
||||||
|
} finally {
|
||||||
|
downloadProgress = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleDelete(name: string, isDirectory: boolean) {
|
||||||
|
if (!confirm(`Delete ${isDirectory ? 'directory' : 'file'} "${name}"?`)) return
|
||||||
|
|
||||||
|
error = ''
|
||||||
|
const path = currentPath === '/' ? `/${name}` : `${currentPath}/${name}`
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await fileSystemClient.deleteFile(path)
|
||||||
|
if (result.success) {
|
||||||
|
if (selectedFile === name) {
|
||||||
|
selectedFile = ''
|
||||||
|
fileContent = ''
|
||||||
|
}
|
||||||
|
await loadDirectory()
|
||||||
|
} else {
|
||||||
|
error = result.error || 'Delete failed'
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
error = e instanceof Error ? e.message : 'Delete error'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createFolder(folderName: string) {
|
||||||
if (!folderName) return
|
if (!folderName) return
|
||||||
const result = await api.post('/api/files/mkdir', {
|
|
||||||
path: '/config/' + folderName
|
error = ''
|
||||||
})
|
const path = currentPath === '/' ? `/${folderName}` : `${currentPath}/${folderName}`
|
||||||
if (result.isOk()) {
|
|
||||||
// Refresh the file list
|
try {
|
||||||
await getFiles()
|
const result = await fileSystemClient.createDirectory(path)
|
||||||
|
if (result.success) {
|
||||||
|
await loadDirectory()
|
||||||
|
} else {
|
||||||
|
error = result.error || 'Failed to create directory'
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
error = e instanceof Error ? e.message : 'Error creating directory'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateSelected = async (name: string) => {
|
async function createFile(fileName: string) {
|
||||||
filename = name
|
if (!fileName) return
|
||||||
isEditing = false
|
|
||||||
await getContent(name)
|
error = ''
|
||||||
|
const path = currentPath === '/' ? `/${fileName}` : `${currentPath}/${fileName}`
|
||||||
|
|
||||||
|
try {
|
||||||
|
const encoder = new TextEncoder()
|
||||||
|
const data = encoder.encode('{}') // Default empty JSON
|
||||||
|
|
||||||
|
const result = await fileSystemClient.uploadFile(path, data)
|
||||||
|
if (result.success) {
|
||||||
|
await loadDirectory()
|
||||||
|
await loadFileContent(fileName)
|
||||||
|
} else {
|
||||||
|
error = result.error || 'Failed to create file'
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
error = e instanceof Error ? e.message : 'Error creating file'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const openNewFolderDialog = () => {
|
function openNewFolderDialog() {
|
||||||
modals.open(NewFolderDialog, {
|
modals.open(NewFolderDialog, {
|
||||||
onConfirm: createFolder
|
onConfirm: createFolder
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const createFile = async (fileName: string) => {
|
function openNewFileDialog() {
|
||||||
if (!fileName) return
|
|
||||||
const result = await api.post('/api/files/edit', {
|
|
||||||
file: '/config/' + fileName,
|
|
||||||
content: '{}' // Default empty JSON object
|
|
||||||
})
|
|
||||||
if (result.isOk()) {
|
|
||||||
// Refresh the file list and select the new file
|
|
||||||
await getFiles()
|
|
||||||
await updateSelected(fileName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const openNewFileDialog = () => {
|
|
||||||
modals.open(NewFileDialog, {
|
modals.open(NewFileDialog, {
|
||||||
onConfirm: createFile
|
onConfirm: createFile
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatBytes(bytes: number): string {
|
||||||
|
if (bytes === 0) return '0 B'
|
||||||
|
const k = 1024
|
||||||
|
const sizes = ['B', 'KB', 'MB', 'GB']
|
||||||
|
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
||||||
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load initial directory
|
||||||
|
$effect(() => {
|
||||||
|
loadDirectory('/')
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- <SettingsCard collapsible={false}> -->
|
|
||||||
<!-- {#snippet icon()} -->
|
|
||||||
<FolderIcon class="flex-shrink-0 mr-2 h-6 w-6 self-end" />
|
<FolderIcon class="flex-shrink-0 mr-2 h-6 w-6 self-end" />
|
||||||
<!-- {/snippet}
|
|
||||||
{#snippet title()} -->
|
<div class="flex justify-between items-center w-full gap-2 mb-4">
|
||||||
<div class="flex justify-between items-center w-full gap-2">
|
<span class="text-xl font-bold">File System</span>
|
||||||
<span>File System</span>
|
|
||||||
<div class="flex gap-2">
|
<div class="flex gap-2">
|
||||||
|
<button class="btn btn-sm btn-primary flex items-center gap-2" onclick={() => uploadInputRef.click()}>
|
||||||
|
<UploadIcon class="w-4 h-4" />
|
||||||
|
Upload File
|
||||||
|
</button>
|
||||||
<button class="btn btn-sm btn-primary flex items-center gap-2" onclick={openNewFileDialog}>
|
<button class="btn btn-sm btn-primary flex items-center gap-2" onclick={openNewFileDialog}>
|
||||||
<FileIcon class="w-4 h-4" />
|
<FileIcon class="w-4 h-4" />
|
||||||
New File
|
New File
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button class="btn btn-sm btn-primary flex items-center gap-2" onclick={openNewFolderDialog}>
|
||||||
class="btn btn-sm btn-primary flex items-center gap-2"
|
|
||||||
onclick={openNewFolderDialog}
|
|
||||||
>
|
|
||||||
<Add class="w-4 h-4" />
|
<Add class="w-4 h-4" />
|
||||||
New Folder
|
New Folder
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<!-- {/snippet} -->
|
|
||||||
|
<input
|
||||||
|
type="file"
|
||||||
|
bind:this={uploadInputRef}
|
||||||
|
onchange={handleFileUpload}
|
||||||
|
style="display: none;"
|
||||||
|
/>
|
||||||
|
|
||||||
|
{#if error}
|
||||||
|
<div class="alert alert-error mb-4">
|
||||||
|
<span>{error}</span>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
{#if uploadProgress}
|
||||||
|
<div class="mb-4">
|
||||||
|
<div class="flex justify-between text-sm mb-1">
|
||||||
|
<span>Uploading...</span>
|
||||||
|
<span>{uploadProgress.percentage.toFixed(1)}% ({formatBytes(uploadProgress.bytesTransferred)} / {formatBytes(uploadProgress.totalBytes)})</span>
|
||||||
|
</div>
|
||||||
|
<progress class="progress progress-primary w-full" value={uploadProgress.percentage} max="100"></progress>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
{#if downloadProgress}
|
||||||
|
<div class="mb-4">
|
||||||
|
<div class="flex justify-between text-sm mb-1">
|
||||||
|
<span>Downloading...</span>
|
||||||
|
<span>{downloadProgress.percentage.toFixed(1)}% ({formatBytes(downloadProgress.bytesTransferred)} / {formatBytes(downloadProgress.totalBytes)})</span>
|
||||||
|
</div>
|
||||||
|
<progress class="progress progress-primary w-full" value={downloadProgress.percentage} max="100"></progress>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
<div class="flex flex-col md:flex-row gap-4 w-full">
|
<div class="flex flex-col md:flex-row gap-4 w-full">
|
||||||
<!-- File Tree -->
|
<!-- File Tree -->
|
||||||
<div
|
<div class="w-full md:w-[300px] md:min-w-[300px] md:max-w-[300px] border-b md:border-b-0 md:border-r pb-4 md:pb-0 md:pr-4">
|
||||||
class="w-full md:w-[300px] md:min-w-[300px] md:max-w-[300px] border-b md:border-b-0 md:border-r pb-4 md:pb-0 md:pr-4"
|
<!-- Current Path -->
|
||||||
>
|
<div class="mb-4 p-2 bg-base-200 rounded font-mono text-sm flex items-center justify-between">
|
||||||
{#await getFiles()}
|
<span class="truncate">{currentPath}</span>
|
||||||
|
{#if currentPath !== '/'}
|
||||||
|
<button class="btn btn-xs btn-ghost" onclick={navigateUp}>
|
||||||
|
↑ Up
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{#if loading}
|
||||||
<Spinner />
|
<Spinner />
|
||||||
{:then files}
|
{:else}
|
||||||
<Folder
|
<!-- Directories -->
|
||||||
name="/"
|
{#each directories as dir (dir.name)}
|
||||||
files={files.root}
|
<div class="flex items-center py-1 px-2 hover:bg-base-200 rounded group">
|
||||||
expanded
|
<button class="flex items-center gap-2 flex-1" onclick={() => navigateTo(dir.name)}>
|
||||||
selected={updateSelected}
|
<FolderIcon class="w-5 h-5 text-yellow-500" />
|
||||||
onDelete={deleteFile}
|
<span class="text-sm">{dir.name}</span>
|
||||||
/>
|
</button>
|
||||||
{/await}
|
<button
|
||||||
|
class="opacity-0 group-hover:opacity-100 btn btn-xs btn-ghost btn-square"
|
||||||
|
onclick={() => handleDelete(dir.name, true)}
|
||||||
|
>
|
||||||
|
<TrashIcon class="w-4 h-4 text-error" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
|
||||||
|
<!-- Files -->
|
||||||
|
{#each files as file (file.name)}
|
||||||
|
<div class="flex items-center py-1 px-2 hover:bg-base-200 rounded group">
|
||||||
|
<button
|
||||||
|
class="flex items-center gap-2 flex-1 min-w-0"
|
||||||
|
onclick={() => loadFileContent(file.name)}
|
||||||
|
class:font-bold={selectedFile === file.name}
|
||||||
|
>
|
||||||
|
<FileIcon class="w-4 h-4 flex-shrink-0" />
|
||||||
|
<span class="text-sm truncate">{file.name}</span>
|
||||||
|
<span class="text-xs opacity-60 ml-auto flex-shrink-0">{formatBytes(file.size)}</span>
|
||||||
|
</button>
|
||||||
|
<div class="flex gap-1 opacity-0 group-hover:opacity-100 flex-shrink-0">
|
||||||
|
<button
|
||||||
|
class="btn btn-xs btn-ghost btn-square"
|
||||||
|
onclick={() => handleDownload(file.name)}
|
||||||
|
title="Download"
|
||||||
|
>
|
||||||
|
<DownloadIcon class="w-4 h-4 text-info" />
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
class="btn btn-xs btn-ghost btn-square"
|
||||||
|
onclick={() => handleDelete(file.name, false)}
|
||||||
|
title="Delete"
|
||||||
|
>
|
||||||
|
<TrashIcon class="w-4 h-4 text-error" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
|
||||||
|
{#if files.length === 0 && directories.length === 0}
|
||||||
|
<div class="text-center text-base-content/50 py-8">
|
||||||
|
Directory is empty
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- File Content -->
|
<!-- File Content -->
|
||||||
<div class="flex-1 min-w-0">
|
<div class="flex-1 min-w-0">
|
||||||
{#if filename}
|
{#if selectedFile}
|
||||||
<div
|
<div class="flex flex-col sm:flex-row justify-between items-start sm:items-center mb-4 gap-2">
|
||||||
class="flex flex-col sm:flex-row justify-between items-start sm:items-center mb-4 gap-2"
|
<h3 class="text-lg font-semibold truncate">{selectedFile}</h3>
|
||||||
>
|
|
||||||
<h3 class="text-lg font-semibold truncate">{filename}</h3>
|
|
||||||
<div class="flex gap-2">
|
<div class="flex gap-2">
|
||||||
{#if isEditing}
|
{#if isEditing}
|
||||||
<button class="btn btn-sm btn-primary" onclick={saveContent}>Save</button>
|
<button class="btn btn-sm btn-primary" onclick={saveFileContent}>
|
||||||
<button
|
Save
|
||||||
class="btn btn-sm btn-secondary"
|
</button>
|
||||||
onclick={() => (isEditing = false)}
|
<button class="btn btn-sm btn-ghost" onclick={() => {
|
||||||
>
|
isEditing = false
|
||||||
|
loadFileContent(selectedFile)
|
||||||
|
}}>
|
||||||
Cancel
|
Cancel
|
||||||
</button>
|
</button>
|
||||||
{:else}
|
{:else}
|
||||||
<button class="btn btn-sm btn-primary" onclick={() => (isEditing = true)}>
|
<button class="btn btn-sm btn-primary" onclick={() => (isEditing = true)}>
|
||||||
Edit
|
Edit
|
||||||
</button>
|
</button>
|
||||||
<button class="btn btn-sm btn-danger" onclick={() => deleteFile(filename)}>
|
<button class="btn btn-sm btn-ghost" onclick={() => handleDownload(selectedFile)}>
|
||||||
|
<DownloadIcon class="w-4 h-4 mr-1" />
|
||||||
|
Download
|
||||||
|
</button>
|
||||||
|
<button class="btn btn-sm btn-error" onclick={() => handleDelete(selectedFile, false)}>
|
||||||
Delete
|
Delete
|
||||||
</button>
|
</button>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{#await getContent(filename)}
|
{#if fileLoading}
|
||||||
<Spinner />
|
<Spinner />
|
||||||
{:then}
|
{:else if isEditing}
|
||||||
{#if isEditing}
|
|
||||||
<textarea
|
<textarea
|
||||||
class="w-full h-[300px] sm:h-[500px] font-mono p-2 bg-gray-800 text-white"
|
class="textarea textarea-bordered w-full h-[300px] sm:h-[500px] font-mono text-sm"
|
||||||
bind:value={content}
|
bind:value={fileContent}
|
||||||
></textarea>
|
></textarea>
|
||||||
{:else}
|
{:else}
|
||||||
<pre
|
<pre class="bg-base-200 p-4 rounded overflow-auto max-h-[300px] sm:max-h-[500px] text-sm">{fileContent}</pre>
|
||||||
class="bg-gray-800 p-4 rounded overflow-auto max-h-[300px] sm:max-h-[500px]">{content}</pre>
|
|
||||||
{/if}
|
{/if}
|
||||||
{/await}
|
|
||||||
{:else}
|
{:else}
|
||||||
<div class="text-center text-gray-500">Select a file to view its contents</div>
|
<div class="text-center text-base-content/50 py-16">
|
||||||
|
Select a file to view its contents
|
||||||
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<!-- </SettingsCard> -->
|
|
||||||
|
|||||||
Reference in New Issue
Block a user