Skip to content

Commit

Permalink
Merge pull request #10 from kongr45gpen/master
Browse files Browse the repository at this point in the history
Fix files for the new Slack export format
  • Loading branch information
grundleborg authored Jun 28, 2019
2 parents aa5f4aa + 007d8d5 commit 1e31742
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 46 deletions.
89 changes: 49 additions & 40 deletions cmd_fetch_attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,57 +102,66 @@ func processChannelFile(w *zip.Writer, file *zip.File, inBuf []byte) error {

// Loop through all the posts.
for _, post := range posts {
// We only care about file_share posts.
if post.Subtype != "file_share" {
continue
}
// 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
}

// 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}
}

// Check there's an Id, Name and either UrlPrivateDownload or UrlPrivate property.
if len(post.File.Id) < 1 || len(post.File.Name) < 1 || !(len(post.File.UrlPrivate) > 0 || len(post.File.UrlPrivateDownload) > 0) {
log.Print("++++++ file_share post has missing properties on it's File object: " + post.Ts + "\n")
// If the post doesn't contain any files, move on.
if post.Files == nil {
continue
}

// Figure out the download URL to use.
var downloadUrl string
if len(post.File.UrlPrivateDownload) > 0 {
downloadUrl = post.File.UrlPrivateDownload
} else {
downloadUrl = post.File.UrlPrivate
}
// 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
}

// Build the output file path.
outputPath := "__uploads/" + post.File.Id + "/" + post.File.Name
// Figure out the download URL to use.
var downloadUrl string
if len(file.UrlPrivateDownload) > 0 {
downloadUrl = file.UrlPrivateDownload
} else {
downloadUrl = file.UrlPrivate
}

// 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
}
// Build the output file path.
outputPath := "__uploads/" + file.Id + "/" + file.Name

// Fetch the file.
response, err := http.Get(downloadUrl)
if err != nil {
log.Print("++++++ Failed to donwload the file: " + downloadUrl)
continue
}
defer response.Body.Close()
// 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
}

// 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")
}
// Fetch the file.
response, err := http.Get(downloadUrl)
if err != nil {
log.Print("++++++ Failed to donwload the file: " + downloadUrl)
continue
}
defer response.Body.Close()

// Success at last.
fmt.Printf("Downloaded attachment into output archive: %s.\n", post.File.Id)
// 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
Expand Down
13 changes: 7 additions & 6 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ type SlackFile struct {
}

type SlackPost struct {
User string `json:"user"`
Type string `json:"type"`
Subtype string `json:"subtype"`
Text string `json:"text"`
Ts string `json:"ts"`
File *SlackFile `json:"file"`
User string `json:"user"`
Type string `json:"type"`
Subtype string `json:"subtype"`
Text string `json:"text"`
Ts string `json:"ts"`
File *SlackFile `json:"file"`
Files []*SlackFile `json:"files"`
}

// As it appears in users.json and /api/users.list.
Expand Down

0 comments on commit 1e31742

Please sign in to comment.