forked from nekaru-storage/gdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
64 lines (55 loc) · 1.66 KB
/
delete.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
package drive
import (
"context"
"fmt"
"io"
"google.golang.org/api/drive/v3"
"google.golang.org/api/googleapi"
)
type DeleteArgs struct {
Out io.Writer
Id string
Recursive bool
}
func (self *Drive) Delete(args DeleteArgs) error {
f, err := self.service.Files.Get(args.Id).SupportsTeamDrives(true).Fields("name", "mimeType").Do()
if err != nil {
return fmt.Errorf("Failed to get file: %s", err)
}
if isDir(f) && !args.Recursive {
return fmt.Errorf("'%s' is a directory, use the 'recursive' flag to delete directories", f.Name)
}
if false {
controlledStop := fmt.Errorf("Controlled stop")
var files []*drive.File
pageSize := 50
self.service.Files.List().SupportsTeamDrives(true).IncludeTeamDriveItems(true).Q(fmt.Sprintf("'%s' in parents", args.Id)).Fields([]googleapi.Field{"nextPageToken", "files(id,name)"}...).PageSize(int64(pageSize)).Pages(context.TODO(), func(fl *drive.FileList) error {
if len(fl.Files) != 0 {
files = append(files, fl.Files...)
} else {
return controlledStop
}
return nil
})
for _, f := range files {
fmt.Printf("Deleting file: %s\n", f.Name)
err = self.service.Files.Delete(f.Id).SupportsTeamDrives(true).Do()
if err != nil {
return fmt.Errorf("Failed to delete file: %s", err)
}
}
}
err = self.service.Files.Delete(args.Id).SupportsTeamDrives(true).Do()
if err != nil {
return fmt.Errorf("Failed to delete file: %s", err)
}
fmt.Fprintf(args.Out, "Deleted '%s'\n", f.Name)
return nil
}
func (self *Drive) deleteFile(fileId string) error {
err := self.service.Files.Delete(fileId).Do()
if err != nil {
return fmt.Errorf("Failed to delete file: %s", err)
}
return nil
}