2024-05-14 18:37:41 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package fileservice
|
|
|
|
|
|
|
|
import (
|
2024-05-14 21:29:41 +02:00
|
|
|
"encoding/base64"
|
2024-05-20 20:39:23 +02:00
|
|
|
"encoding/json"
|
2024-05-14 21:29:41 +02:00
|
|
|
"fmt"
|
2024-05-14 18:37:41 +02:00
|
|
|
"os"
|
2024-05-16 09:29:58 +02:00
|
|
|
"path/filepath"
|
2024-05-14 18:37:41 +02:00
|
|
|
|
2024-05-16 09:29:58 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/util/utilfn"
|
2024-05-14 18:37:41 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wavebase"
|
|
|
|
)
|
|
|
|
|
2024-05-17 07:48:23 +02:00
|
|
|
const MaxFileSize = 10 * 1024 * 1024 // 10M
|
|
|
|
|
2024-05-14 18:37:41 +02:00
|
|
|
type FileService struct{}
|
|
|
|
|
2024-05-16 09:29:58 +02:00
|
|
|
type FileInfo struct {
|
|
|
|
Path string `json:"path"` // cleaned path
|
|
|
|
NotFound bool `json:"notfound,omitempty"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
Mode os.FileMode `json:"mode"`
|
|
|
|
ModTime int64 `json:"modtime"`
|
|
|
|
IsDir bool `json:"isdir,omitempty"`
|
|
|
|
MimeType string `json:"mimetype,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type FullFile struct {
|
|
|
|
Info *FileInfo `json:"info"`
|
|
|
|
Data64 string `json:"data64,omitempty"` // base64 encoded
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FileService) StatFile(path string) (*FileInfo, error) {
|
|
|
|
cleanedPath := filepath.Clean(wavebase.ExpandHomeDir(path))
|
|
|
|
finfo, err := os.Stat(cleanedPath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return &FileInfo{Path: wavebase.ReplaceHomeDir(path), NotFound: true}, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot stat file %q: %w", path, err)
|
|
|
|
}
|
2024-05-20 20:39:23 +02:00
|
|
|
mimeType := utilfn.DetectMimeType(cleanedPath)
|
2024-05-16 09:29:58 +02:00
|
|
|
return &FileInfo{
|
2024-05-20 20:39:23 +02:00
|
|
|
Path: cleanedPath,
|
2024-05-16 09:29:58 +02:00
|
|
|
Size: finfo.Size(),
|
|
|
|
Mode: finfo.Mode(),
|
|
|
|
ModTime: finfo.ModTime().UnixMilli(),
|
|
|
|
IsDir: finfo.IsDir(),
|
|
|
|
MimeType: mimeType,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FileService) ReadFile(path string) (*FullFile, error) {
|
|
|
|
finfo, err := fs.StatFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot stat file %q: %w", path, err)
|
|
|
|
}
|
|
|
|
if finfo.NotFound {
|
|
|
|
return &FullFile{Info: finfo}, nil
|
|
|
|
}
|
2024-05-17 07:48:23 +02:00
|
|
|
if finfo.Size > MaxFileSize {
|
|
|
|
return nil, fmt.Errorf("file %q is too large to read, use /wave/stream-file", path)
|
|
|
|
}
|
2024-05-20 20:39:23 +02:00
|
|
|
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()
|
2024-05-29 09:00:36 +02:00
|
|
|
fullFilePath := filepath.Join(finfo.Path, innerFileInfoInt.Name())
|
2024-05-20 20:39:23 +02:00
|
|
|
innerFileInfo := FileInfo{
|
2024-05-29 09:00:36 +02:00
|
|
|
Path: fullFilePath,
|
2024-05-20 20:39:23 +02:00
|
|
|
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
|
|
|
|
}
|
2024-05-16 09:29:58 +02:00
|
|
|
cleanedPath := filepath.Clean(wavebase.ExpandHomeDir(path))
|
|
|
|
barr, err := os.ReadFile(cleanedPath)
|
2024-05-14 21:29:41 +02:00
|
|
|
if err != nil {
|
2024-05-16 09:29:58 +02:00
|
|
|
return nil, fmt.Errorf("cannot read file %q: %w", path, err)
|
2024-05-14 21:29:41 +02:00
|
|
|
}
|
2024-05-16 09:29:58 +02:00
|
|
|
return &FullFile{
|
|
|
|
Info: finfo,
|
|
|
|
Data64: base64.StdEncoding.EncodeToString(barr),
|
|
|
|
}, nil
|
2024-05-14 18:37:41 +02:00
|
|
|
}
|