✨ 增加视频接口
This commit is contained in:
81
alidrive/post_json.go
Normal file
81
alidrive/post_json.go
Normal file
@ -0,0 +1,81 @@
|
||||
package alidrive
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// convert body to json
|
||||
func BodyToJson(url string, req interface{}, resp RespHandle, drive *conf.Drive) error {
|
||||
if body, err := DoPost(url, req, drive.AccessToken); err != nil {
|
||||
log.Errorf("doPost出错:%s", err.Error())
|
||||
return err
|
||||
} else {
|
||||
if err = json.Unmarshal(body, &resp); err != nil {
|
||||
log.Errorf("解析json[%s]出错:%s", string(body), err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
if resp.IsAvailable() {
|
||||
return nil
|
||||
}
|
||||
if resp.GetCode() == conf.AccessTokenInvalid {
|
||||
resp.SetCode("")
|
||||
if RefreshToken(drive) {
|
||||
return BodyToJson(url, req, resp, drive)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf(resp.GetMessage())
|
||||
}
|
||||
|
||||
// do post request
|
||||
func DoPost(url string, request interface{}, auth string) (body []byte, err error) {
|
||||
var (
|
||||
resp *http.Response
|
||||
)
|
||||
requestBody := new(bytes.Buffer)
|
||||
err = json.NewEncoder(requestBody).Encode(request)
|
||||
if err != nil {
|
||||
log.Errorf("创建requestBody出错:%s", err.Error())
|
||||
}
|
||||
req, err := http.NewRequest("POST", url, requestBody)
|
||||
log.Debugf("do_post_req:%+v", req)
|
||||
if err != nil {
|
||||
log.Errorf("创建request出错:%s", err.Error())
|
||||
return
|
||||
}
|
||||
if auth != "" {
|
||||
req.Header.Set("authorization", conf.Bearer+auth)
|
||||
}
|
||||
req.Header.Add("content-type", "application/json")
|
||||
req.Header.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
|
||||
req.Header.Add("origin", "https://aliyundrive.com")
|
||||
req.Header.Add("accept", "*/*")
|
||||
req.Header.Add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
|
||||
req.Header.Add("Connection", "keep-alive")
|
||||
|
||||
for retryCount := 3; retryCount >= 0; retryCount-- {
|
||||
if resp, err = conf.Client.Do(req); err != nil && strings.Contains(err.Error(), "timeout") {
|
||||
<-time.After(time.Second)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
log.Errorf("请求阿里云盘api时出错:%s", err.Error())
|
||||
return
|
||||
}
|
||||
if body, err = ioutil.ReadAll(resp.Body); err != nil {
|
||||
log.Errorf("读取api返回内容失败")
|
||||
}
|
||||
log.Debugf("请求返回信息:%s", string(body))
|
||||
return
|
||||
}
|
||||
|
@ -65,3 +65,9 @@ type OfficePreviewUrlReq struct {
|
||||
DriveId string `json:"drive_id"`
|
||||
FileId string `json:"file_id"`
|
||||
}
|
||||
|
||||
// video preview url request bean
|
||||
type VideoPreviewUrlReq struct {
|
||||
DriveId string `json:"drive_id"`
|
||||
FileId string `json:"file_id"`
|
||||
}
|
@ -1,15 +1,8 @@
|
||||
package alidrive
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// get file
|
||||
@ -115,69 +108,16 @@ func GetOfficePreviewUrl(fileId string, drive *conf.Drive) (*OfficePreviewUrlRes
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
// convert body to json
|
||||
func BodyToJson(url string, req interface{}, resp RespHandle, drive *conf.Drive) error {
|
||||
if body, err := DoPost(url, req, drive.AccessToken); err != nil {
|
||||
log.Errorf("doPost出错:%s", err.Error())
|
||||
return err
|
||||
} else {
|
||||
if err = json.Unmarshal(body, &resp); err != nil {
|
||||
log.Errorf("解析json[%s]出错:%s", string(body), err.Error())
|
||||
return err
|
||||
}
|
||||
// get video preview url
|
||||
func GetVideoPreviewUrl(fileId string, drive *conf.Drive) (*VideoPreviewUrlResp, error) {
|
||||
url := conf.Conf.AliDrive.ApiUrl + "/databox/get_video_play_info"
|
||||
req := VideoPreviewUrlReq{
|
||||
DriveId: drive.DefaultDriveId,
|
||||
FileId: fileId,
|
||||
}
|
||||
if resp.IsAvailable() {
|
||||
return nil
|
||||
var resp VideoPreviewUrlResp
|
||||
if err := BodyToJson(url, req, &resp, drive); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.GetCode() == conf.AccessTokenInvalid {
|
||||
resp.SetCode("")
|
||||
if RefreshToken(drive) {
|
||||
return BodyToJson(url, req, resp, drive)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf(resp.GetMessage())
|
||||
}
|
||||
|
||||
// do post request
|
||||
func DoPost(url string, request interface{}, auth string) (body []byte, err error) {
|
||||
var (
|
||||
resp *http.Response
|
||||
)
|
||||
requestBody := new(bytes.Buffer)
|
||||
err = json.NewEncoder(requestBody).Encode(request)
|
||||
if err != nil {
|
||||
log.Errorf("创建requestBody出错:%s", err.Error())
|
||||
}
|
||||
req, err := http.NewRequest("POST", url, requestBody)
|
||||
log.Debugf("do_post_req:%+v", req)
|
||||
if err != nil {
|
||||
log.Errorf("创建request出错:%s", err.Error())
|
||||
return
|
||||
}
|
||||
if auth != "" {
|
||||
req.Header.Set("authorization", conf.Bearer+auth)
|
||||
}
|
||||
req.Header.Add("content-type", "application/json")
|
||||
req.Header.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
|
||||
req.Header.Add("origin", "https://aliyundrive.com")
|
||||
req.Header.Add("accept", "*/*")
|
||||
req.Header.Add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
|
||||
req.Header.Add("Connection", "keep-alive")
|
||||
|
||||
for retryCount := 3; retryCount >= 0; retryCount-- {
|
||||
if resp, err = conf.Client.Do(req); err != nil && strings.Contains(err.Error(), "timeout") {
|
||||
<-time.After(time.Second)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
log.Errorf("请求阿里云盘api时出错:%s", err.Error())
|
||||
return
|
||||
}
|
||||
if body, err = ioutil.ReadAll(resp.Body); err != nil {
|
||||
log.Errorf("读取api返回内容失败")
|
||||
}
|
||||
log.Debugf("请求返回信息:%s", string(body))
|
||||
return
|
||||
return &resp, nil
|
||||
}
|
@ -1,10 +1,6 @@
|
||||
package alidrive
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -150,35 +146,11 @@ type OfficePreviewUrlResp struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
||||
|
||||
// check password
|
||||
func HasPassword(files *Files) string {
|
||||
fileList := files.Items
|
||||
for i, file := range fileList {
|
||||
if strings.HasPrefix(file.Name, ".password-") {
|
||||
files.Items = fileList[:i+copy(fileList[i:], fileList[i+1:])]
|
||||
return file.Name[10:]
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Deprecated: check readme, implemented by the front end now
|
||||
func HasReadme(files *Files) string {
|
||||
fileList := files.Items
|
||||
for _, file := range fileList {
|
||||
if file.Name == "Readme.md" {
|
||||
resp, err := http.Get(file.Url)
|
||||
if err != nil {
|
||||
log.Errorf("Get Readme出错:%s", err.Error())
|
||||
return ""
|
||||
}
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Errorf("读取 Readme出错:%s", err.Error())
|
||||
return ""
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
type VideoPreviewUrlResp struct {
|
||||
RespError
|
||||
TemplateList []struct {
|
||||
TemplateId string `json:"template_id"`
|
||||
Status string `json:"status"`
|
||||
Url string `json:"url"`
|
||||
} `json:"template_list"`
|
||||
}
|
||||
|
41
alidrive/utils.go
Normal file
41
alidrive/utils.go
Normal file
@ -0,0 +1,41 @@
|
||||
package alidrive
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// check password
|
||||
func HasPassword(files *Files) string {
|
||||
fileList := files.Items
|
||||
for i, file := range fileList {
|
||||
if strings.HasPrefix(file.Name, ".password-") {
|
||||
files.Items = fileList[:i+copy(fileList[i:], fileList[i+1:])]
|
||||
return file.Name[10:]
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Deprecated: check readme, implemented by the front end now
|
||||
func HasReadme(files *Files) string {
|
||||
fileList := files.Items
|
||||
for _, file := range fileList {
|
||||
if file.Name == "Readme.md" {
|
||||
resp, err := http.Get(file.Url)
|
||||
if err != nil {
|
||||
log.Errorf("Get Readme出错:%s", err.Error())
|
||||
return ""
|
||||
}
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Errorf("读取 Readme出错:%s", err.Error())
|
||||
return ""
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
@ -22,7 +22,7 @@ var (
|
||||
var Conf = new(Config)
|
||||
|
||||
const (
|
||||
VERSION = "v1.0.1"
|
||||
VERSION = "v1.0.2"
|
||||
|
||||
ImageThumbnailProcess = "image/resize,w_50"
|
||||
VideoThumbnailProcess = "video/snapshot,t_0,f_jpg,w_50"
|
||||
|
@ -31,3 +31,4 @@ func OfficePreview(c *gin.Context) {
|
||||
}
|
||||
c.JSON(200, DataResponse(preview))
|
||||
}
|
||||
|
||||
|
33
server/controllers/video_preview.go
Normal file
33
server/controllers/video_preview.go
Normal file
@ -0,0 +1,33 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/alidrive"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type VideoPreviewReq struct {
|
||||
FileId string `json:"file_id" binding:"required"`
|
||||
}
|
||||
|
||||
// handle video_preview request
|
||||
func VideoPreview(c *gin.Context) {
|
||||
drive := utils.GetDriveByName(c.Param("drive"))
|
||||
if drive == nil {
|
||||
c.JSON(200, MetaResponse(400, "drive isn't exist."))
|
||||
return
|
||||
}
|
||||
var req VideoPreviewReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error()))
|
||||
return
|
||||
}
|
||||
log.Debugf("preview_req:%+v", req)
|
||||
preview, err := alidrive.GetVideoPreviewUrl(req.FileId, drive)
|
||||
if err != nil {
|
||||
c.JSON(200, MetaResponse(500, err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(200, DataResponse(preview))
|
||||
}
|
@ -27,6 +27,7 @@ func InitApiRouter(engine *gin.Engine) {
|
||||
apiV2.POST("/get", controllers.Get)
|
||||
apiV2.POST("/path", controllers.Path)
|
||||
apiV2.POST("/office_preview/:drive", controllers.OfficePreview)
|
||||
apiV2.POST("/video_preview/:drive", controllers.VideoPreview)
|
||||
apiV2.POST("/local_search", controllers.LocalSearch)
|
||||
apiV2.POST("/global_search", controllers.GlobalSearch)
|
||||
apiV2.GET("/rebuild/:drive/:password", controllers.RebuildTree)
|
||||
|
Reference in New Issue
Block a user