diff --git a/internal/routes/mod.go b/internal/routes/mod.go index 2cf1f99..277e399 100644 --- a/internal/routes/mod.go +++ b/internal/routes/mod.go @@ -3,10 +3,7 @@ package routes import ( "fmt" "os" - "path/filepath" - "strings" - "git.wh64.net/devproje/kuma-archive/config" "git.wh64.net/devproje/kuma-archive/internal/service" "github.com/gin-contrib/static" "github.com/gin-gonic/gin" @@ -37,25 +34,30 @@ func New(app *gin.Engine) { return } - if !info.IsDir() { - var split = strings.Split(path, "/") - ctx.FileAttachment(filepath.Join(config.INDEX_DIR, path), split[len(split)-1]) - + if !info.IsDir { + ctx.FileAttachment(info.FullPath, info.Name) return } - entry, err := os.ReadDir(filepath.Join(config.INDEX_DIR, path)) + raw, err := os.ReadDir(info.FullPath) if err != nil { ctx.Status(500) return } entries := make([]service.DirEntry, 0) - for _, fd := range entry { - var info, _ = fd.Info() + for _, entry := range raw { + finfo, err := entry.Info() + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + continue + } + entries = append(entries, service.DirEntry{ - Name: fd.Name(), - FileSize: uint64(info.Size()), + Name: entry.Name(), + FileSize: uint64(finfo.Size()), + FullPath: path, + IsDir: finfo.IsDir(), }) } diff --git a/internal/service/worker.go b/internal/service/worker.go index a78dd09..c24e3f1 100644 --- a/internal/service/worker.go +++ b/internal/service/worker.go @@ -11,14 +11,26 @@ type WorkerService struct{} type DirEntry struct { Name string `json:"name"` + FullPath string `json:"path"` FileSize uint64 `json:"file_size"` + IsDir bool `json:"is_dir"` } func NewWorkerService() *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) - 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 }