This repository has been archived by the owner on Nov 6, 2024. It is now read-only.
forked from chyroc/go-aliyundrive
-
Notifications
You must be signed in to change notification settings - Fork 5
/
list.go
131 lines (116 loc) · 4.16 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Copyright 2022 chyroc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package aliyundrive
import (
"context"
"net/http"
"time"
)
type FileListReq struct {
GetAll bool `json:"get_all"`
ShareID string `json:"share_id"` // drive_id 和 share_id 必选传其中一个
DriveID string `json:"drive_id"` // drive_id 和 share_id 必选传其中一个
ParentFileID string `json:"parent_file_id"`
Marker string `json:"marker"`
Limit int `json:"limit"`
All bool `json:"all"`
URLExpireSec int `json:"url_expire_sec"`
ImageThumbnailProcess string `json:"image_thumbnail_process"`
ImageURLProcess string `json:"image_url_process"`
VideoThumbnailProcess string `json:"video_thumbnail_process"`
Fields string `json:"fields"`
OrderBy string `json:"order_by"`
OrderDirection string `json:"order_direction"`
}
type FileListResp struct {
Items []*File `json:"items"`
NextMarker string `json:"next_marker"`
}
type File struct {
DriveID string `json:"drive_id"`
DomainID string `json:"domain_id"`
FileID string `json:"file_id"`
Name string `json:"name"`
Type string `json:"type"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Hidden bool `json:"hidden"`
Starred bool `json:"starred"`
Status string `json:"status"`
UserMeta string `json:"user_meta,omitempty"`
ParentFileID string `json:"parent_file_id"`
EncryptMode string `json:"encrypt_mode"`
ContentType string `json:"content_type,omitempty"`
FileExtension string `json:"file_extension,omitempty"`
MimeType string `json:"mime_type,omitempty"`
MimeExtension string `json:"mime_extension,omitempty"`
Size int64 `json:"size,omitempty"`
Crc64Hash string `json:"crc64_hash,omitempty"`
ContentHash string `json:"content_hash,omitempty"`
ContentHashName string `json:"content_hash_name,omitempty"`
DownloadURL string `json:"download_url,omitempty"`
URL string `json:"url,omitempty"`
Thumbnail string `json:"thumbnail,omitempty"`
Category string `json:"category,omitempty"`
PunishFlag int `json:"punish_flag,omitempty"`
}
func (r *AliyunDrive) Lists(ctx context.Context, request *FileListReq) (*FileListResp, error) {
if !request.GetAll {
return r.List(ctx, request)
}
marker := ""
var items []*File
for {
request.Marker = marker
resp, err := r.List(ctx, request)
if err != nil {
return nil, err
}
items = append(items, resp.Items...)
marker = resp.NextMarker
if marker == "" || len(resp.Items) < request.Limit {
break
}
}
return &FileListResp{Items: items, NextMarker: ""}, nil
}
// @TODO
func (r *AliyunDrive) List(ctx context.Context, request *FileListReq) (*FileListResp, error) {
if request.ParentFileID == "" {
request.ParentFileID = RootFileID
}
if request.Limit == 0 {
request.Limit = 100
}
request.URLExpireSec = 1600
request.ImageThumbnailProcess = "image/resize,w_400/format,jpeg"
request.ImageURLProcess = "image/resize,w_1920/format,jpeg"
request.VideoThumbnailProcess = "video/snapshot,t_0,f_jpg,ar_auto,w_300"
request.Fields = "*"
request.OrderBy = "updated_at"
request.OrderDirection = "DESC"
req := &config{
Method: http.MethodPost,
URL: "https://api.aliyundrive.com/adrive/v3/file/list",
Body: request,
}
var result FileListResp
_, err := r.request(ctx, req, &result)
if err != nil {
return nil, err
}
return &result, nil
}