mirror of
https://github.com/wavetermdev/backup.git
synced 2026-08-05 13:57:07 -07:00
feat: pass contents of directory to front end
This mainly focuses on passing directory info to the front end. It isn't a complete version of that, but it's enough to plan out some details of the styling
This commit is contained in:
@@ -5,6 +5,7 @@ package fileservice
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -41,9 +42,9 @@ func (fs *FileService) StatFile(path string) (*FileInfo, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot stat file %q: %w", path, err)
|
||||
}
|
||||
mimeType := utilfn.DetectMimeType(path)
|
||||
mimeType := utilfn.DetectMimeType(cleanedPath)
|
||||
return &FileInfo{
|
||||
Path: wavebase.ReplaceHomeDir(path),
|
||||
Path: cleanedPath,
|
||||
Size: finfo.Size(),
|
||||
Mode: finfo.Mode(),
|
||||
ModTime: finfo.ModTime().UnixMilli(),
|
||||
@@ -63,6 +64,35 @@ func (fs *FileService) ReadFile(path string) (*FullFile, error) {
|
||||
if finfo.Size > MaxFileSize {
|
||||
return nil, fmt.Errorf("file %q is too large to read, use /wave/stream-file", path)
|
||||
}
|
||||
if finfo.IsDir {
|
||||
innerFilesEntries, err := os.ReadDir(finfo.Path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse directory %s", finfo.Path)
|
||||
}
|
||||
|
||||
var innerFilesInfo []FileInfo
|
||||
for _, innerFileEntry := range innerFilesEntries {
|
||||
innerFileInfoInt, _ := innerFileEntry.Info()
|
||||
innerFileInfo := FileInfo{
|
||||
Path: innerFileInfoInt.Name(),
|
||||
Size: innerFileInfoInt.Size(),
|
||||
Mode: innerFileInfoInt.Mode(),
|
||||
ModTime: innerFileInfoInt.ModTime().UnixMilli(),
|
||||
IsDir: innerFileInfoInt.IsDir(),
|
||||
MimeType: "",
|
||||
}
|
||||
innerFilesInfo = append(innerFilesInfo, innerFileInfo)
|
||||
}
|
||||
|
||||
filesSerialized, err := json.Marshal(innerFilesInfo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to serialize files %s", finfo.Path)
|
||||
}
|
||||
return &FullFile{
|
||||
Info: finfo,
|
||||
Data64: base64.StdEncoding.EncodeToString(filesSerialized),
|
||||
}, nil
|
||||
}
|
||||
cleanedPath := filepath.Clean(wavebase.ExpandHomeDir(path))
|
||||
barr, err := os.ReadFile(cleanedPath)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user