Skip to content

Commit

Permalink
🚧 获取链接
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Mar 12, 2021
1 parent e4d206d commit abe9d92
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
49 changes: 49 additions & 0 deletions server/controllers/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package controllers

import (
"github.com/Xhofe/alist/alidrive"
"github.com/Xhofe/alist/server/models"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"path/filepath"
)

// get request bean
type GetReq struct {
Path string `json:"path" binding:"required"`
Password string `json:"password"`
}

// handle get request
func Get(c *gin.Context) {
var get GetReq
if err := c.ShouldBindJSON(&get); err != nil {
c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error()))
return
}
log.Debugf("list:%+v", get)
dir, name := filepath.Split(get.Path)
file, err := models.GetFileByDirAndName(dir, name)
if err != nil {
if file == nil {
c.JSON(200, MetaResponse(404, "Path not found."))
return
}
c.JSON(200, MetaResponse(500, err.Error()))
return
}
if file.Password != "" && file.Password != get.Password {
if get.Password == "" {
c.JSON(200, MetaResponse(401, "need password."))
} else {
c.JSON(200, MetaResponse(401, "wrong password."))
}
return
}
down, err := alidrive.GetDownLoadUrl(file.FileId)
if err != nil {
c.JSON(200, MetaResponse(500, err.Error()))
return
}
c.JSON(200, DataResponse(down))
}
1 change: 1 addition & 0 deletions server/models/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type File struct {
ContentType string `json:"content_type"`
Size int64 `json:"size"`
Password string `json:"password"`
Url string `json:"url" gorm:"-"`
}

func (file *File) Create() error {
Expand Down
3 changes: 2 additions & 1 deletion server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ func InitApiRouter(engine *gin.Engine) {
apiV2 := engine.Group("/api")
{
apiV2.GET("/info", controllers.Info)
apiV2.POST("/get", controllers.Get)
apiV2.POST("/path", controllers.Path)
apiV2.POST("/office_preview", controllers.OfficePreview)
apiV2.POST("/local_search", controllers.LocalSearch)
apiV2.POST("/global_search", controllers.GlobalSearch)
apiV2.GET("/rebuild/*password", controllers.RebuildTree)
}
engine.GET("/d/*path", controllers.Down)
engine.GET("/rebuild/*password", controllers.RebuildTree)
}

0 comments on commit abe9d92

Please sign in to comment.