Skip to content
This repository has been archived by the owner on Oct 17, 2022. It is now read-only.

Commit

Permalink
解决rclone单程文件夹为空问题
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkLeong committed Oct 10, 2021
1 parent c2ac4d7 commit 60387f6
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 32 deletions.
43 changes: 43 additions & 0 deletions aliyun/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,49 @@ func GetList(token string, driveId string, parentFileId string) (model.FileListM
return list, nil
}

func GetFilePath(token string, driveId string, parentFileId string, fileId string, typeStr string) (string, error) {

if len(parentFileId) == 0 {
parentFileId = "root"
}
path := "/"
var list model.ListFilePath
if result, ok := cache.GoCache.Get(parentFileId + "path"); ok {
path, ok = result.(string)
if ok {
return path, nil
}
}

postData := make(map[string]interface{})
postData["drive_id"] = driveId
postData["file_id"] = fileId

data, err := json.Marshal(postData)
if err != nil {
fmt.Println("获取列表转义数据失败", err)
return "/", err
}

body := net.Post(model.APIFILEPATH, token, data)

e := json.Unmarshal(body, &list)
if e != nil {
fmt.Println(e)
}
minNum := 0
if typeStr == "folder" {
minNum = 1
}
for i := len(list.Items); i > minNum; i-- {
path += list.Items[i-1].Name + "/"
}

cache.GoCache.SetDefault(parentFileId+"path", path)

return path, nil
}

func GetFile(w http.ResponseWriter, url string, token string, rangeStr string, ifRange string) bool {

body := net.Get(w, url, token, rangeStr, ifRange)
Expand Down
3 changes: 2 additions & 1 deletion aliyun/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
const (
APIBASE = "https://api.aliyundrive.com"
APILISTURL = APIBASE + "/adrive/v3/file/list"
APIFILEPATH = APIBASE + "/adrive/v1/file/get_path"
APIREFRESHTOKENURL = APIBASE + "/token/refresh"
APIREMOVETRASH = APIBASE + "/v2/recyclebin/trash" //移动到垃圾箱
APIFILEUPDATE = APIBASE + "/v3/file/update"
Expand All @@ -13,7 +14,7 @@ const (
APIFILEUPLOADURL = APIBASE + "/v2/file/get_upload_url"
APIFILEUPLOADFILE = APIBASE + "/v2/file/create_with_proof" //"/v2/file/create"
APIFILECOMPLETE = APIBASE + "/v2/file/complete"
APIFILEDOWNLOAD =APIBASE+"/v2/file/get_download_url"
APIFILEDOWNLOAD = APIBASE + "/v2/file/get_download_url"
)

type Config struct {
Expand Down
21 changes: 21 additions & 0 deletions aliyun/model/filepath.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package model

type FilePath struct {
Name string `json:"name,omitempty"`
// created_at: "2021-09-06T07:12:29.103Z"
// domain_id: "bj29"
// drive_id: "1662258"
// encrypt_mode: "none"
// file_id: "6135bf5dfa3c0db759ce4f65b763eb895c422395"
// hidden: false
// name: "未命名文件夹"
// parent_file_id: "6135bf58e00ab306ed024e75917eb91e179505b7"
// starred: false
// status: "available"
// type: "folder"
// updated_at: "2021-09-06T07:12:29.103Z"
}

type ListFilePath struct {
Items []FilePath `json:"items"`
}
24 changes: 2 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,6 @@ type Task struct {
Id string `json:"id"`
}

//func GetDb() *gorm.DB {
// // 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
// //dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&parseTime=True&loc=Local", m.User, m.PWD, m.IP, m.Port, m.DBName)
// //db, err := gorm.Open(mysql2.Open(dsn), &gorm.Config{})
// db, err := gorm.Open(sqlite.Open("./db/casaOS.db"), &gorm.Config{})
// c, _ := db.DB()
// c.SetMaxIdleConns(10)
// c.SetMaxOpenConns(100)
// c.SetConnMaxIdleTime(time.Second * 1000)
// if err != nil {
// fmt.Println("连接数据失败!")
// panic("数据库连接失败")
// return nil
// }
// err = db.AutoMigrate(&Task{})
// if err != nil {
// fmt.Println("检查和创建数据库出错", err)
// }
// return db
//}

func main() {
//GetDb()
var port *string
Expand Down Expand Up @@ -131,7 +110,8 @@ func main() {
}
}
}

fmt.Println(req.URL)
fmt.Println(req.Method)
fs.ServeHTTP(w, req)
})
http.ListenAndServe(address, nil)
Expand Down
17 changes: 12 additions & 5 deletions webdav/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"encoding/xml"
"fmt"
"go-aliyun-webdav/aliyun"
"go-aliyun-webdav/aliyun/model"
"io"
"net/http"
Expand Down Expand Up @@ -759,7 +760,7 @@ type WalkFunc func(parent model.ListModel, info model.FileListModel, err error)
// Allowed values for depth are 0, 1 or infiniteDepth. For each visited node,
// walkFS calls walkFn. If a visited file system node is a directory and
// walkFn returns filepath.SkipDir, walkFS will skip traversal of this node.
func walkFS(ctx context.Context, fs FileSystem, depth int, parent model.ListModel, info model.FileListModel, walkFn WalkFunc, token, driver string) error {
func walkFS(ctx context.Context, fs FileSystem, depth int, parent model.ListModel, info model.FileListModel, walkFn WalkFunc, token, driver, userAgent string) error {
// This implementation is based on Walk's code in the standard path/filepath package.
err := walkFn(parent, info, nil)
if err != nil {
Expand All @@ -781,12 +782,18 @@ func walkFS(ctx context.Context, fs FileSystem, depth int, parent model.ListMode
return err
}
} else {
err = walkFS(ctx, fs, depth, fileInfo, fileList, walkFn, token, driver)
if err != nil {
if fileInfo.Type != "folder" || err != filepath.SkipDir {
return err
if fileInfo.Type == "folder" && !strings.Contains(userAgent, "RaiDrive") {
info, _ := aliyun.GetList(token, driver, fileInfo.FileId)
walkFS(ctx, fs, depth, fileInfo, info, walkFn, token, driver, userAgent)
} else {
err = walkFS(ctx, fs, depth, fileInfo, fileList, walkFn, token, driver, userAgent)
if err != nil {
if fileInfo.Type != "folder" || err != filepath.SkipDir {
return err
}
}
}

}
}
return nil
Expand Down
14 changes: 10 additions & 4 deletions webdav/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,13 +860,19 @@ func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request) (status
return err
}
href := path.Join(h.Prefix, parent.Name)
if href != "/" && fi.Type == "folder" {
href += "/"
if parent.ParentFileId == "root" {
href = "/" + parent.Name
} else {
href, _ = aliyun.GetFilePath(h.Config.Token, h.Config.DriveId, parent.ParentFileId, parent.FileId, parent.Type)
href += parent.Name
if parent.Type == "folder" {
href += "/"
}
}
return mw.write(makePropstatResponse(href, pstats))
}

walkErr := walkFS(ctx, h.FileSystem, depth, fi, list, walkFn, h.Config.Token, h.Config.DriveId)
userAgent := r.Header.Get("User-Agent")
walkErr := walkFS(ctx, h.FileSystem, depth, fi, list, walkFn, h.Config.Token, h.Config.DriveId, userAgent)
closeErr := mw.close()
if walkErr != nil {
return http.StatusInternalServerError, walkErr
Expand Down

0 comments on commit 60387f6

Please sign in to comment.