From 5727d39eb8548b38cdbbd8c8917c3fa92fa2f845 Mon Sep 17 00:00:00 2001 From: Project_IO Date: Sun, 23 Mar 2025 14:29:58 +0900 Subject: [PATCH] feat: middle save --- internal/routes/auth.go | 11 ++------- internal/routes/mod.go | 10 ++++++-- internal/routes/privdir.go | 1 - internal/routes/worker.go | 46 ++++++++++++++++++++++++++----------- internal/service/privdir.go | 27 +++++++++++++++------- internal/service/worker.go | 11 ++++----- 6 files changed, 66 insertions(+), 40 deletions(-) delete mode 100644 internal/routes/privdir.go diff --git a/internal/routes/auth.go b/internal/routes/auth.go index 737bac8..1cb8cd2 100644 --- a/internal/routes/auth.go +++ b/internal/routes/auth.go @@ -8,13 +8,6 @@ import ( "github.com/gin-gonic/gin" ) -func authentication(group *gin.RouterGroup) { - group.POST("/login", login) - group.GET("/read", readAcc) - group.PATCH("/update", updateAcc) - group.DELETE("/delete", deleteAcc) -} - func login(ctx *gin.Context) { auth := service.NewAuthService() username := ctx.PostForm("username") @@ -77,7 +70,7 @@ func readAcc(ctx *gin.Context) { func updateAcc(ctx *gin.Context) { auth := service.NewAuthService() old := ctx.PostForm("password") - new := ctx.PostForm("new_password") + pass := ctx.PostForm("new_password") username, _, ok := ctx.Request.BasicAuth() if !ok { ctx.Status(401) @@ -90,7 +83,7 @@ func updateAcc(ctx *gin.Context) { return } - if err = auth.Update(username, new); err != nil { + if err = auth.Update(username, pass); err != nil { ctx.Status(500) _, _ = fmt.Fprintln(os.Stderr, err) return diff --git a/internal/routes/mod.go b/internal/routes/mod.go index 2a69bee..c8fd81c 100644 --- a/internal/routes/mod.go +++ b/internal/routes/mod.go @@ -13,10 +13,16 @@ func New(app *gin.Engine, version *service.Version, apiOnly bool) { app.Use(middleware.BasicAuth) api := app.Group("/api") - api.GET("/path/*path", readPath) + api.GET("/path/*path", discoverPath) api.GET("/download/*path", downloadPath) - authentication(api.Group("/auth")) + auth := api.Group("/auth") + { + auth.POST("/login", login) + auth.GET("/read", readAcc) + auth.PATCH("/update", updateAcc) + auth.DELETE("/delete", deleteAcc) + } api.GET("/version", func(ctx *gin.Context) { ctx.String(200, "%s", version.String()) diff --git a/internal/routes/privdir.go b/internal/routes/privdir.go deleted file mode 100644 index 0db51ae..0000000 --- a/internal/routes/privdir.go +++ /dev/null @@ -1 +0,0 @@ -package routes diff --git a/internal/routes/worker.go b/internal/routes/worker.go index f9f5c94..75275a7 100644 --- a/internal/routes/worker.go +++ b/internal/routes/worker.go @@ -8,14 +8,23 @@ import ( "path/filepath" ) -func readPath(ctx *gin.Context) { - worker := service.NewWorkerService() - path := ctx.Param("path") +func worker(path string) (*service.DirEntry, error) { + sv := service.NewWorkerService() + return sv.Read(path) +} - data, err := worker.Read(path) - if err != nil { - _, _ = fmt.Fprintf(os.Stderr, "%v\n", err) - ctx.Status(404) +func discoverPath(ctx *gin.Context) { + path := ctx.Param("path") + data, err := worker(path) + if data == nil { + if err != nil { + _, _ = fmt.Fprintln(os.Stderr, err) + } + + ctx.JSON(404, gin.H{ + "ok": 0, + "errno": fmt.Errorf("path %s is not exist", path), + }) return } @@ -30,7 +39,8 @@ func readPath(ctx *gin.Context) { return } - raw, err := os.ReadDir(data.Path) + var raw []os.DirEntry + raw, err = os.ReadDir(data.Path) if err != nil { _, _ = fmt.Fprintf(os.Stderr, "%v\n", err) ctx.Status(500) @@ -65,17 +75,25 @@ func readPath(ctx *gin.Context) { } func downloadPath(ctx *gin.Context) { - worker := service.NewWorkerService() path := ctx.Param("path") - data, err := worker.Read(path) - if err != nil { - _, _ = fmt.Fprintf(os.Stderr, "%v\n", err) - ctx.Status(404) + data, err := worker(path) + if data == nil { + if err != nil { + _, _ = fmt.Fprintln(os.Stderr, err) + } + + ctx.JSON(404, gin.H{ + "ok": 0, + "errno": fmt.Errorf("path %s is not exist", path), + }) return } if data.IsDir { - ctx.String(400, "current path is not file") + ctx.JSON(404, gin.H{ + "ok": 0, + "errno": "file is not exist", + }) return } diff --git a/internal/service/privdir.go b/internal/service/privdir.go index 7a02824..c7e0c39 100644 --- a/internal/service/privdir.go +++ b/internal/service/privdir.go @@ -17,6 +17,13 @@ type PrivDir struct { Owner string `json:"owner"` } +type test interface { + Create() + Read() + Update() + Delete() +} + func init() { db, err := Open() if err != nil { @@ -115,34 +122,38 @@ func (sv *PrivDirService) Delete(dirname string) error { return nil } -func (sv *PrivDirService) Query() ([]*PrivDir, error) { +func (sv *PrivDirService) Query() []PrivDir { db, err := Open() if err != nil { - return nil, err + _, _ = fmt.Fprintf(os.Stderr, "%v\n", err) + return nil } defer db.Close() stmt, err := db.Prepare("select * from PrivDir;") if err != nil { - return nil, err + _, _ = fmt.Fprintf(os.Stderr, "%v\n", err) + return nil } defer stmt.Close() rows, err := stmt.Query() if err != nil { - return nil, err + _, _ = fmt.Fprintf(os.Stderr, "%v\n", err) + return nil } defer rows.Close() - var dirs []*PrivDir + var dirs []PrivDir for rows.Next() { var data PrivDir if err = rows.Scan(&data.Id, &data.DirName, &data.Owner); err != nil { - return nil, err + _, _ = fmt.Fprintf(os.Stderr, "%v\n", err) + return nil } - dirs = append(dirs, &data) + dirs = append(dirs, data) } - return dirs, nil + return dirs } diff --git a/internal/service/worker.go b/internal/service/worker.go index dfc8c80..e90e8cb 100644 --- a/internal/service/worker.go +++ b/internal/service/worker.go @@ -22,18 +22,17 @@ func NewWorkerService() *WorkerService { } func (sv *WorkerService) Read(path string) (*DirEntry, error) { - fullpath := filepath.Join(config.INDEX_DIR, path) - info, err := os.Stat(fullpath) + fullPath := filepath.Join(config.INDEX_DIR, path) + info, err := os.Stat(fullPath) if err != nil { return nil, err } - ret := DirEntry{ + return &DirEntry{ Name: info.Name(), Date: info.ModTime().Unix(), - Path: fullpath, + Path: fullPath, FileSize: uint64(info.Size()), IsDir: info.IsDir(), - } - return &ret, nil + }, nil }