-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (84 loc) · 2.02 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"path"
"path/filepath"
"sort"
"strings"
"github.com/openset/aliyundrive/api"
)
const (
KB = 1024
MB = 1024 * KB
GB = 1024 * MB
)
var home, _ = os.UserHomeDir()
var allFilesPath = filepath.Join(home, "all_files.json")
func main() {
AllFiles()
DeleteDuplicateFile()
}
func AllFiles() {
allFiles := make(map[string][]api.FileSearchItemV3)
err := Walk("", "root", func(filePath string, item api.FileSearchItemV3) {
item.FullName = path.Join(filePath, item.Name)
allFiles[item.ContentHash] = append(allFiles[item.ContentHash], item)
n := len(allFiles[item.ContentHash])
fmt.Println(n, item.FullName)
})
fmt.Println(err)
data, _ := json.MarshalIndent(allFiles, "", "\t")
_ = os.WriteFile(allFilesPath, data, os.ModePerm)
}
func Walk(filePath, root string, fn func(string, api.FileSearchItemV3)) error {
if root != "root" && !strings.HasPrefix(filePath, "") {
return nil
}
result, err := api.FileSearchV3()
if err != nil {
return err
}
for _, item := range result.Items {
if item.Type == "folder" {
filePath := path.Join(filePath, item.Name)
err = Walk(filePath, item.FileID, fn)
if err != nil {
return err
}
} else {
fn(filePath, item)
}
}
return nil
}
func DeleteDuplicateFile() {
allFiles := make(map[string][]api.FileListItemV3)
data, err := os.ReadFile(allFilesPath)
check(err)
err = json.Unmarshal(data, &allFiles)
check(err)
fileIds := make([]string, 0)
for _, items := range allFiles {
if len(items) < 2 || items[0].Size < 0*MB {
continue
}
sort.Slice(items, func(i, j int) bool {
return items[i].Name < items[j].Name || items[i].CreatedAt.Before(items[j].CreatedAt)
})
for _, item := range items[1:] {
fileIds = append(fileIds, item.FileID)
fmt.Println(items[0].FullName, item.FullName, err)
}
}
result, _ := api.RecycleBinTrashBatchV2("9680003", fileIds)
data, err = json.MarshalIndent(result, "", "\t")
fmt.Printf("%s\n%v\n", data, err)
}
func check(err error) {
if err != nil {
log.Fatalln(err)
}
}