-
Notifications
You must be signed in to change notification settings - Fork 44
/
getfilesfromfolder.go
308 lines (293 loc) · 8.17 KB
/
getfilesfromfolder.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Package main (getfilesfromfolder.go) :
// These methods are for downloading all files from a shared folder of Google Drive.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
getfilelist "github.com/tanaikech/go-getfilelist"
drive "google.golang.org/api/drive/v3"
"google.golang.org/api/googleapi/transport"
)
const (
driveAPI = "https://www.googleapis.com/drive/v3/files"
)
// mime2ext : Convert mimeType to extension.
func mime2ext(mime string) string {
var obj map[string]interface{}
json.Unmarshal([]byte(mimeVsEx), &obj)
res, _ := obj[mime].(string)
return res
}
// ext2mime : Convert extension to mimeType.
func ext2mime(ext string) string {
var obj map[string]interface{}
json.Unmarshal([]byte(extVsmime), &obj)
res, _ := obj[ext].(string)
return res
}
// downloadFileByAPIKey : Download file using API key.
func (p *para) downloadFileByAPIKey(file *drive.File) error {
u, err := url.Parse(driveAPI)
if err != nil {
return err
}
u.Path = path.Join(u.Path, file.Id)
q := u.Query()
q.Set("key", p.APIKey)
if strings.Contains(file.MimeType, "application/vnd.google-apps") {
u.Path = path.Join(u.Path, "export")
q.Set("mimeType", file.WebViewLink)
} else {
q.Set("alt", "media")
}
u.RawQuery = q.Encode()
bkWorkDir := p.WorkDir
bkFilename := p.Filename
p.WorkDir = file.WebContentLink
p.Filename = file.Name
timeOut := func(size int64) int64 {
if size == 0 {
switch {
case size < 100000000:
return 3600
case size > 100000000:
return 0
}
}
return 0
}(file.Size)
p.Client = &http.Client{
Timeout: time.Duration(timeOut) * time.Second,
}
res, err := p.fetch(u.String())
if err != nil {
return err
}
if res.StatusCode != 200 {
r, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
defer res.Body.Close()
if p.SkipError {
fmt.Printf("!! Downloading '%s' (fileId: %s) was skipped by an error. Status code is %d.\n", file.Name, file.Id, res.StatusCode)
p.WorkDir = bkWorkDir
p.Filename = bkFilename
return nil
}
return fmt.Errorf("%s", r)
}
p.saveFile(res)
p.WorkDir = bkWorkDir
p.Filename = bkFilename
return nil
}
// makeFileByCondition : Make file by condition.
func (p *para) makeFileByCondition(file *drive.File) error {
if er := chkFile(filepath.Join(file.WebContentLink, file.Name)); er {
if !p.OverWrite && !p.Skip {
return fmt.Errorf("'%s' is existing. If you want to overwrite, please use an option '--overwrite'", file.WebContentLink)
}
if p.OverWrite && !p.Skip {
return p.downloadFileByAPIKey(file)
}
if !p.Disp && p.Skip {
fmt.Printf("Downloading '%s' was skipped because of existing.\n", file.Name)
}
} else {
return p.downloadFileByAPIKey(file)
}
return nil
}
// makeDir : Make a directory by checking duplication.
func (p *para) makeDir(folder string) error {
if er := chkFile(folder); !er {
if err := os.Mkdir(folder, 0777); err != nil {
return err
}
} else {
if !p.OverWrite && !p.Skip {
return fmt.Errorf("'%s' is existing. If you want to overwrite, please use an option '--overwrite'", folder)
}
}
return nil
}
// chkFile : Check the existence of file and directory in local PC.
func chkFile(name string) bool {
_, err := os.Stat(name)
return err == nil
}
// makeDirByCondition : Make directory by condition.
func (p *para) makeDirByCondition(dir string) error {
var err error
if er := chkFile(dir); er {
if !p.OverWrite && !p.Skip {
return fmt.Errorf("'%s' is existing. If you want to overwrite, please use option '--overwrite' or '--skip'", dir)
}
if p.OverWrite && !p.Skip {
if err = p.makeDir(dir); err != nil {
return err
}
}
if !p.Disp && p.Skip {
fmt.Printf("Creating '%s' was skipped because of existing.\n", dir)
}
} else {
if err = p.makeDir(dir); err != nil {
return err
}
}
return nil
}
// initDownload : Download files by Drive API using API key.
func (p *para) initDownload(fileList *getfilelist.FileListDl) error {
var err error
if !p.Disp {
fmt.Printf("Download files from a folder '%s'.\n", fileList.SearchedFolder.Name)
fmt.Printf("There are %d files and %d folders in the folder.\n", fileList.TotalNumberOfFiles, fileList.TotalNumberOfFolders-1)
fmt.Println("Starting download.")
}
idToName := map[string]interface{}{}
for i, e := range fileList.FolderTree.Folders {
idToName[e] = fileList.FolderTree.Names[i]
}
for _, e := range fileList.FileList {
path := p.WorkDir
if p.Notcreatetopdirectory {
e.FolderTree = append(e.FolderTree[:0], e.FolderTree[1:]...)
}
for _, dir := range e.FolderTree {
path = filepath.Join(path, idToName[dir].(string))
}
if path != p.WorkDir {
err = p.makeDirByCondition(path)
if err != nil {
return err
}
}
for _, file := range e.Files {
if file.MimeType != "application/vnd.google-apps.script" {
file.WebContentLink = path // Substituting
p.Size = file.Size
err = p.makeFileByCondition(file)
if err != nil {
return err
}
} else {
if !p.Disp {
fmt.Printf("'%s' is a project file. Project file cannot be downloaded using API key.\n", file.Name)
}
}
}
}
return nil
}
// defFormat : Default download format
func defFormat(mime string) string {
var df map[string]interface{}
json.Unmarshal([]byte(defaultformat), &df)
dmime, _ := df[mime].(string)
return dmime
}
// extToMime : Convert from extension to mimeType of the file on Local.
func extToMime(ext string) string {
var fm map[string]interface{}
json.Unmarshal([]byte(extVsmime), &fm)
st, _ := fm[strings.Replace(strings.ToLower(ext), ".", "", 1)].(string)
return st
}
// dupChkFoldersFiles : Check duplication of folder names and filenames.
func (p *para) dupChkFoldersFiles(fileList *getfilelist.FileListDl) {
dupChk1 := map[string]bool{}
cnt1 := 2
for i, folderName := range fileList.FolderTree.Names {
if !dupChk1[folderName] {
dupChk1[folderName] = true
} else {
fileList.FolderTree.Names[i] = folderName + "_" + strconv.Itoa(cnt1)
}
}
extt := strings.ToLower(p.Ext)
for i, list := range fileList.FileList {
if len(list.Files) > 0 {
dupChk2 := map[string]bool{}
cnt2 := 2
for j, file := range list.Files {
if !dupChk2[file.Name] {
dupChk2[file.Name] = true
} else {
ext := filepath.Ext(file.Name)
if ext != "" {
fileList.FileList[i].Files[j].Name = file.Name[0:len(file.Name)-len(ext)] + "_" + strconv.Itoa(cnt2) + ext
} else {
fileList.FileList[i].Files[j].Name = file.Name + "_" + strconv.Itoa(cnt2)
}
cnt2++
}
mime := defFormat(file.MimeType)
if extt != "" {
if mime != "" {
cmime := func() string {
if (extt == "txt" || extt == "text") && file.MimeType == "application/vnd.google-apps.spreadsheet" {
return extToMime("csv")
} else if extt == "zip" && file.MimeType == "application/vnd.google-apps.presentation" {
return extToMime("pptx")
}
return extToMime(extt)
}()
if cmime != "" {
fileList.FileList[i].Files[j].WebViewLink = cmime // Substituting as OutMimeType
} else {
fileList.FileList[i].Files[j].WebViewLink = mime // Substituting as OutMimeType
}
}
} else {
fileList.FileList[i].Files[j].WebViewLink = mime // Substituting as OutMimeType
}
if file.MimeType != "application/vnd.google-apps.script" {
ext := filepath.Ext(file.Name)
if ext == "" {
fileList.FileList[i].Files[j].Name += mime2ext(fileList.FileList[i].Files[j].WebViewLink)
}
}
}
}
}
}
// getFilesFromFolder: This method is the main method for downloading all files in a shread folder.
func (p *para) getFilesFromFolder() error {
client := &http.Client{
Transport: &transport.APIKey{Key: p.APIKey},
}
fileList, err := func() (*getfilelist.FileListDl, error) {
if len(p.InputtedMimeType) > 0 {
return getfilelist.Folder(p.SearchID).MimeType(p.InputtedMimeType).Do(client)
}
return getfilelist.Folder(p.SearchID).Do(client)
}()
if err != nil {
return err
}
if p.ShowFileInf {
r, err := json.Marshal(fileList)
if err != nil {
return err
}
fmt.Printf("%s\n", r)
return nil
}
p.dupChkFoldersFiles(fileList)
if err := p.initDownload(fileList); err != nil {
return err
}
return nil
}