feat: middle save

This commit is contained in:
WH64 2025-03-23 14:29:58 +09:00
parent 8389114526
commit 5727d39eb8
6 changed files with 66 additions and 40 deletions

View file

@ -8,13 +8,6 @@ import (
"github.com/gin-gonic/gin" "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) { func login(ctx *gin.Context) {
auth := service.NewAuthService() auth := service.NewAuthService()
username := ctx.PostForm("username") username := ctx.PostForm("username")
@ -77,7 +70,7 @@ func readAcc(ctx *gin.Context) {
func updateAcc(ctx *gin.Context) { func updateAcc(ctx *gin.Context) {
auth := service.NewAuthService() auth := service.NewAuthService()
old := ctx.PostForm("password") old := ctx.PostForm("password")
new := ctx.PostForm("new_password") pass := ctx.PostForm("new_password")
username, _, ok := ctx.Request.BasicAuth() username, _, ok := ctx.Request.BasicAuth()
if !ok { if !ok {
ctx.Status(401) ctx.Status(401)
@ -90,7 +83,7 @@ func updateAcc(ctx *gin.Context) {
return return
} }
if err = auth.Update(username, new); err != nil { if err = auth.Update(username, pass); err != nil {
ctx.Status(500) ctx.Status(500)
_, _ = fmt.Fprintln(os.Stderr, err) _, _ = fmt.Fprintln(os.Stderr, err)
return return

View file

@ -13,10 +13,16 @@ func New(app *gin.Engine, version *service.Version, apiOnly bool) {
app.Use(middleware.BasicAuth) app.Use(middleware.BasicAuth)
api := app.Group("/api") api := app.Group("/api")
api.GET("/path/*path", readPath) api.GET("/path/*path", discoverPath)
api.GET("/download/*path", downloadPath) 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) { api.GET("/version", func(ctx *gin.Context) {
ctx.String(200, "%s", version.String()) ctx.String(200, "%s", version.String())

View file

@ -1 +0,0 @@
package routes

View file

@ -8,14 +8,23 @@ import (
"path/filepath" "path/filepath"
) )
func readPath(ctx *gin.Context) { func worker(path string) (*service.DirEntry, error) {
worker := service.NewWorkerService() sv := service.NewWorkerService()
path := ctx.Param("path") return sv.Read(path)
}
data, err := worker.Read(path) func discoverPath(ctx *gin.Context) {
if err != nil { path := ctx.Param("path")
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err) data, err := worker(path)
ctx.Status(404) 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 return
} }
@ -30,7 +39,8 @@ func readPath(ctx *gin.Context) {
return return
} }
raw, err := os.ReadDir(data.Path) var raw []os.DirEntry
raw, err = os.ReadDir(data.Path)
if err != nil { if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err) _, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
ctx.Status(500) ctx.Status(500)
@ -65,17 +75,25 @@ func readPath(ctx *gin.Context) {
} }
func downloadPath(ctx *gin.Context) { func downloadPath(ctx *gin.Context) {
worker := service.NewWorkerService()
path := ctx.Param("path") path := ctx.Param("path")
data, err := worker.Read(path) data, err := worker(path)
if err != nil { if data == nil {
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err) if err != nil {
ctx.Status(404) _, _ = fmt.Fprintln(os.Stderr, err)
}
ctx.JSON(404, gin.H{
"ok": 0,
"errno": fmt.Errorf("path %s is not exist", path),
})
return return
} }
if data.IsDir { if data.IsDir {
ctx.String(400, "current path is not file") ctx.JSON(404, gin.H{
"ok": 0,
"errno": "file is not exist",
})
return return
} }

View file

@ -17,6 +17,13 @@ type PrivDir struct {
Owner string `json:"owner"` Owner string `json:"owner"`
} }
type test interface {
Create()
Read()
Update()
Delete()
}
func init() { func init() {
db, err := Open() db, err := Open()
if err != nil { if err != nil {
@ -115,34 +122,38 @@ func (sv *PrivDirService) Delete(dirname string) error {
return nil return nil
} }
func (sv *PrivDirService) Query() ([]*PrivDir, error) { func (sv *PrivDirService) Query() []PrivDir {
db, err := Open() db, err := Open()
if err != nil { if err != nil {
return nil, err _, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
return nil
} }
defer db.Close() defer db.Close()
stmt, err := db.Prepare("select * from PrivDir;") stmt, err := db.Prepare("select * from PrivDir;")
if err != nil { if err != nil {
return nil, err _, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
return nil
} }
defer stmt.Close() defer stmt.Close()
rows, err := stmt.Query() rows, err := stmt.Query()
if err != nil { if err != nil {
return nil, err _, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
return nil
} }
defer rows.Close() defer rows.Close()
var dirs []*PrivDir var dirs []PrivDir
for rows.Next() { for rows.Next() {
var data PrivDir var data PrivDir
if err = rows.Scan(&data.Id, &data.DirName, &data.Owner); err != nil { 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
} }

View file

@ -22,18 +22,17 @@ func NewWorkerService() *WorkerService {
} }
func (sv *WorkerService) Read(path string) (*DirEntry, error) { func (sv *WorkerService) Read(path string) (*DirEntry, error) {
fullpath := filepath.Join(config.INDEX_DIR, path) fullPath := filepath.Join(config.INDEX_DIR, path)
info, err := os.Stat(fullpath) info, err := os.Stat(fullPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ret := DirEntry{ return &DirEntry{
Name: info.Name(), Name: info.Name(),
Date: info.ModTime().Unix(), Date: info.ModTime().Unix(),
Path: fullpath, Path: fullPath,
FileSize: uint64(info.Size()), FileSize: uint64(info.Size()),
IsDir: info.IsDir(), IsDir: info.IsDir(),
} }, nil
return &ret, nil
} }