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"
)
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

View file

@ -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())

View file

@ -1 +0,0 @@
package routes

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}