-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
110 lines (99 loc) · 2.77 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"github.com/nlopes/slack"
"github.com/joho/godotenv"
"log"
"net/http"
"io/ioutil"
"os"
"sync"
"fmt"
"flag"
"time"
)
const folderName = "slack-downloads"
func main() {
godotenv.Load()
slackToken := flag.String("token", os.Getenv("SLACK_API_TOKEN"), "Set Slack API Token")
fileType := flag.String("type", "all", "Set file type")
beforeTimestamp := flag.Int64("before", time.Now().Unix(), "Set file timestamp (file older than this timestamp will be the target)")
includePrivate := flag.Bool("private", false, "Download private files")
doDelete := flag.Bool("delete", false, "Delete downloaded files from Slack")
flag.Parse()
api := slack.New(*slackToken)
files, paging, err := getFiles(api, *fileType, *beforeTimestamp, 1)
if err != nil {
log.Fatal(err)
}
if _, err := os.Stat(folderName); err != nil {
os.Mkdir(folderName, 0777)
}
slackFileDeleter := newInstance()
go slackFileDeleter.run(api)
waitGroup := sync.WaitGroup{}
for paging.Page <= paging.Pages {
for _, slackFile := range files {
if !*includePrivate && !slackFile.IsPublic {
continue
}
if _, err := os.Stat(getFileName(slackFile)); err == nil {
if *doDelete {
slackFileDeleter.delete(slackFile.ID)
}
continue
}
waitGroup.Add(1)
go write(&waitGroup, slackFile, *slackToken, *doDelete, slackFileDeleter)
}
log.Printf("files size: %d, paging: %#v\n", len(files), paging)
files, paging, err = getFiles(api, *fileType, *beforeTimestamp, paging.Page+1)
if err != nil {
log.Fatal(err)
}
}
waitGroup.Wait()
slackFileDeleter.stop()
}
func write(waitGroup *sync.WaitGroup, slackFile slack.File, slackToken string, doDelete bool, deleter SlackFileDeleter) {
defer waitGroup.Done()
req, err := http.NewRequest("GET", slackFile.URLPrivateDownload, nil)
req.Header.Set("Authorization", "Bearer "+slackToken)
client := new(http.Client)
res, err := client.Do(req)
if err != nil {
log.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println(err)
return
}
file, err := os.OpenFile(getFileName(slackFile), os.O_CREATE|os.O_WRONLY, 0666, )
if err != nil {
log.Println(err)
return
}
defer file.Close()
_, err = file.Write(body)
if err != nil {
log.Println(err)
return
}
log.Printf("Download finished: %s\n", slackFile.URLPrivateDownload)
if doDelete {
deleter.delete(slackFile.ID)
}
}
func getFiles(api *slack.Client, fileType string, beforeTimestamp int64, page int) ([]slack.File, *slack.Paging, error) {
return api.GetFiles(slack.GetFilesParameters{
Types: fileType,
Count: 1000,
Page: page,
TimestampTo: slack.JSONTime(beforeTimestamp),
})
}
func getFileName(slackFile slack.File) string {
return fmt.Sprintf("%s/%s-%s", folderName, slackFile.ID, slackFile.Name)
}