From 007d8d5e50e1552b8deee56a6082c7261281482e Mon Sep 17 00:00:00 2001 From: kongr45gpen Date: Sun, 26 Aug 2018 22:39:59 +0300 Subject: [PATCH] Fix attachments for the new Slack export format --- cmd_fetch_attachments.go | 89 ++++++++++++++++++++++------------------ model.go | 13 +++--- 2 files changed, 56 insertions(+), 46 deletions(-) diff --git a/cmd_fetch_attachments.go b/cmd_fetch_attachments.go index 44c98ff..b39160f 100644 --- a/cmd_fetch_attachments.go +++ b/cmd_fetch_attachments.go @@ -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 diff --git a/model.go b/model.go index ce8c21e..fd05fa5 100644 --- a/model.go +++ b/model.go @@ -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.