forked from grundleborg/slack-advanced-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_fetch_attachments.go
178 lines (151 loc) · 4.78 KB
/
cmd_fetch_attachments.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
package main
import (
"archive/zip"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
func fetchAttachments(inputArchive string, outputArchive string, slackApiToken string) error {
// Check the parameters.
if len(inputArchive) == 0 {
fmt.Printf("fetch-attachments command requires --input-archive to be specified.\n")
os.Exit(1)
}
if len(outputArchive) == 0 {
fmt.Printf("fetch-attachments command requires --output-archive to be specified.\n")
os.Exit(1)
}
// Open the input archive.
r, err := zip.OpenReader(inputArchive)
if err != nil {
fmt.Printf("Could not open input archive for reading: %s\n", inputArchive)
os.Exit(1)
}
defer r.Close()
// Open the output archive.
f, err := os.Create(outputArchive)
if err != nil {
fmt.Printf("Could not open the output archive for writing: %s\n\n%s", outputArchive, err)
os.Exit(1)
}
defer f.Close()
// Create a zip writer on the output archive.
w := zip.NewWriter(f)
// Run through all the files in the input archive.
for _, file := range r.File {
// Open the file from the input archive.
inReader, err := file.Open()
if err != nil {
fmt.Printf("Failed to open file in input archive: %s\n\n%s", file.Name, err)
os.Exit(1)
}
// Read the file into a byte array.
inBuf, err := ioutil.ReadAll(inReader)
if err != nil {
fmt.Printf("Failed to read file in input archive: %s\n\n%s", file.Name, err)
}
// Now write this file to the output archive.
outFile, err := w.Create(file.Name)
if err != nil {
fmt.Printf("Failed to create file in output archive: %s\n\n%s", file.Name, err)
os.Exit(1)
}
_, err = outFile.Write(inBuf)
if err != nil {
fmt.Printf("Failed to write file in output archive: %s\n\n%s", file.Name, err)
}
// Check if the file name matches the pattern for files we need to parse.
splits := strings.Split(file.Name, "/")
if len(splits) == 2 && !strings.HasPrefix(splits[0], "__") && strings.HasSuffix(splits[1], ".json") {
// Parse this file.
err = processChannelFile(w, file, inBuf, slackApiToken)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
}
}
// Close the output zip writer.
err = w.Close()
if err != nil {
fmt.Printf("Failed to close the output archive.\n\n%s", err)
}
return nil
}
func processChannelFile(w *zip.Writer, file *zip.File, inBuf []byte, token string) error {
// Parse the JSON of the file.
var posts []SlackPost
if err := json.Unmarshal(inBuf, &posts); err != nil {
return errors.New("Couldn't parse the JSON file: " + file.Name + "\n\n" + err.Error() + "\n")
}
// Loop through all the posts.
for _, post := range posts {
// Support for legacy file_share posts.
if post.Subtype == "file_share" {
// Check there's a File property.
if post.File == nil {
log.Print("++++++ file_share post has no File property: " + post.Ts + "\n")
continue
}
// Add the file as a single item in the array of the post's files.
post.Files = []*SlackFile{post.File}
}
// If the post doesn't contain any files, move on.
if post.Files == nil {
continue
}
client := &http.Client{}
// Loop through all the files.
for _, file := range post.Files {
// Check there's an Id, Name and either UrlPrivateDownload or UrlPrivate property.
if len(file.Id) < 1 || len(file.Name) < 1 || !(len(file.UrlPrivate) > 0 || len(file.UrlPrivateDownload) > 0) {
log.Print("++++++ file_share post has missing properties on it's File object: " + post.Ts + "\n")
continue
}
// Figure out the download URL to use.
var downloadUrl string
if len(file.UrlPrivateDownload) > 0 {
downloadUrl = file.UrlPrivateDownload
} else {
downloadUrl = file.UrlPrivate
}
// Build the output file path.
outputPath := "__uploads/" + file.Id + "/" + file.Name
// Create the file in the zip output file.
outFile, err := w.Create(outputPath)
if err != nil {
log.Print("++++++ Failed to create output file in output archive: " + outputPath + "\n\n" + err.Error() + "\n")
continue
}
// Fetch the file.
req, err := http.NewRequest("GET", downloadUrl, nil)
if err != nil {
log.Print("++++++ Failed to create file download request: " + downloadUrl)
continue
}
if (token != "") {
req.Header.Add("Authorization", "Bearer " + token)
}
response, err := client.Do(req)
if err != nil {
log.Print("++++++ Failed to download the file: " + downloadUrl)
continue
}
defer response.Body.Close()
// Save the file to the output zip file.
_, err = io.Copy(outFile, response.Body)
if err != nil {
log.Print("++++++ Failed to write the downloaded file to the output archive: " + downloadUrl + "\n\n" + err.Error() + "\n")
}
// Success at last.
fmt.Printf("Downloaded attachment into output archive: %s.\n", file.Id)
}
}
return nil
}