feat: middle save

This commit is contained in:
WH64 2025-03-14 18:31:21 +09:00
parent 9cdd058a49
commit ce1fd7987f
2 changed files with 28 additions and 14 deletions

View file

@ -3,10 +3,7 @@ package routes
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings"
"git.wh64.net/devproje/kuma-archive/config"
"git.wh64.net/devproje/kuma-archive/internal/service" "git.wh64.net/devproje/kuma-archive/internal/service"
"github.com/gin-contrib/static" "github.com/gin-contrib/static"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -37,25 +34,30 @@ func New(app *gin.Engine) {
return return
} }
if !info.IsDir() { if !info.IsDir {
var split = strings.Split(path, "/") ctx.FileAttachment(info.FullPath, info.Name)
ctx.FileAttachment(filepath.Join(config.INDEX_DIR, path), split[len(split)-1])
return return
} }
entry, err := os.ReadDir(filepath.Join(config.INDEX_DIR, path)) raw, err := os.ReadDir(info.FullPath)
if err != nil { if err != nil {
ctx.Status(500) ctx.Status(500)
return return
} }
entries := make([]service.DirEntry, 0) entries := make([]service.DirEntry, 0)
for _, fd := range entry { for _, entry := range raw {
var info, _ = fd.Info() finfo, err := entry.Info()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
continue
}
entries = append(entries, service.DirEntry{ entries = append(entries, service.DirEntry{
Name: fd.Name(), Name: entry.Name(),
FileSize: uint64(info.Size()), FileSize: uint64(finfo.Size()),
FullPath: path,
IsDir: finfo.IsDir(),
}) })
} }

View file

@ -11,14 +11,26 @@ type WorkerService struct{}
type DirEntry struct { type DirEntry struct {
Name string `json:"name"` Name string `json:"name"`
FullPath string `json:"path"`
FileSize uint64 `json:"file_size"` FileSize uint64 `json:"file_size"`
IsDir bool `json:"is_dir"`
} }
func NewWorkerService() *WorkerService { func NewWorkerService() *WorkerService {
return &WorkerService{} return &WorkerService{}
} }
func (sv *WorkerService) Read(path string) (os.FileInfo, error) { func (sv *WorkerService) Read(path string) (*DirEntry, error) {
fullpath := filepath.Join(config.INDEX_DIR, path) fullpath := filepath.Join(config.INDEX_DIR, path)
return os.Stat(fullpath) info, err := os.Stat(fullpath)
if err != nil {
return nil, err
}
ret := DirEntry{
Name: info.Name(),
FullPath: fullpath,
FileSize: uint64(info.Size()),
}
return &ret, nil
} }