summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCorey Hulen <corey@hulen.com>2016-03-08 12:10:31 -0800
committerCorey Hulen <corey@hulen.com>2016-03-08 12:10:31 -0800
commita1ec47eb780f6d244d1d3d50e6078f907e8b7fd7 (patch)
tree4c46ed31fff46c4b1adc0b29c3c90157348920d9
parentcbf84c8beaa0896daaf82ecdb63f236da4d64c0e (diff)
parent5c29d4f64935d0516e9d72d1d8cc734bcf295a87 (diff)
downloadchat-a1ec47eb780f6d244d1d3d50e6078f907e8b7fd7.tar.gz
chat-a1ec47eb780f6d244d1d3d50e6078f907e8b7fd7.tar.bz2
chat-a1ec47eb780f6d244d1d3d50e6078f907e8b7fd7.zip
Merge pull request #2388 from mattermost/plt-1789
PLT-1789 Deleting posts with files now renames the file so that public links t…
-rw-r--r--api/file.go35
-rw-r--r--api/post.go18
-rw-r--r--i18n/en.json20
3 files changed, 71 insertions, 2 deletions
diff --git a/api/file.go b/api/file.go
index 0011afd5b..9150e4bfe 100644
--- a/api/file.go
+++ b/api/file.go
@@ -547,6 +547,41 @@ func writeFile(f []byte, path string) *model.AppError {
return nil
}
+func moveFile(oldPath, newPath string) *model.AppError {
+ if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 {
+ fileData := make(chan []byte)
+ getFileAndForget(oldPath, fileData)
+ fileBytes := <-fileData
+
+ if fileBytes == nil {
+ return model.NewLocAppError("moveFile", "api.file.move_file.get_from_s3.app_error", nil, "")
+ }
+
+ var auth aws.Auth
+ auth.AccessKey = utils.Cfg.FileSettings.AmazonS3AccessKeyId
+ auth.SecretKey = utils.Cfg.FileSettings.AmazonS3SecretAccessKey
+
+ s := s3.New(auth, awsRegion())
+ bucket := s.Bucket(utils.Cfg.FileSettings.AmazonS3Bucket)
+
+ if err := bucket.Del(oldPath); err != nil {
+ return model.NewLocAppError("moveFile", "api.file.move_file.delete_from_s3.app_error", nil, err.Error())
+ }
+
+ if err := writeFile(fileBytes, newPath); err != nil {
+ return err
+ }
+ } else if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_LOCAL {
+ if err := os.Rename(utils.Cfg.FileSettings.Directory+oldPath, utils.Cfg.FileSettings.Directory+newPath); err != nil {
+ return model.NewLocAppError("moveFile", "api.file.move_file.rename.app_error", nil, err.Error())
+ }
+ } else {
+ return model.NewLocAppError("moveFile", "api.file.move_file.configured.app_error", nil, "")
+ }
+
+ return nil
+}
+
func writeFileLocally(f []byte, path string) *model.AppError {
if err := os.MkdirAll(filepath.Dir(path), 0774); err != nil {
return model.NewLocAppError("writeFile", "api.file.write_file_locally.create_dir.app_error", nil, err.Error())
diff --git a/api/post.go b/api/post.go
index e6560a8e8..cd78b16f0 100644
--- a/api/post.go
+++ b/api/post.go
@@ -1094,6 +1094,7 @@ func deletePost(c *Context, w http.ResponseWriter, r *http.Request) {
message.Add("post", post.ToJson())
PublishAndForget(message)
+ DeletePostFilesAndForget(c.Session.TeamId, post)
result := make(map[string]string)
result["id"] = postId
@@ -1101,6 +1102,23 @@ func deletePost(c *Context, w http.ResponseWriter, r *http.Request) {
}
}
+func DeletePostFilesAndForget(teamId string, post *model.Post) {
+ go func() {
+ if len(post.Filenames) == 0 {
+ return
+ }
+
+ prefix := "teams/" + teamId + "/channels/" + post.ChannelId + "/users/" + post.UserId + "/"
+ for _, filename := range post.Filenames {
+ splitUrl := strings.Split(filename, "/")
+ oldPath := prefix + splitUrl[len(splitUrl)-2] + "/" + splitUrl[len(splitUrl)-1]
+ newPath := prefix + splitUrl[len(splitUrl)-2] + "/deleted_" + splitUrl[len(splitUrl)-1]
+ moveFile(oldPath, newPath)
+ }
+
+ }()
+}
+
func getPostsBefore(c *Context, w http.ResponseWriter, r *http.Request) {
getPostsBeforeOrAfter(c, w, r, true)
}
diff --git a/i18n/en.json b/i18n/en.json
index b7ebbb8b1..6ac5ce5c2 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -456,8 +456,24 @@
"translation": "S3 is not supported for local storage export."
},
{
- "id": "api.export.write_file.app_error",
- "translation": "Unable to write to export file"
+ "id": "api.file.move_file.get_from_s3.app_error",
+ "translation": "Unable to get file from S3."
+ },
+ {
+ "id": "api.file.move_file.delete_from_s3.app_error",
+ "translation": "Unable to delete file from S3."
+ },
+ {
+ "id": "api.file.move_file.rename.app_error",
+ "translation": "Unable to move file locally."
+ },
+ {
+ "id": "api.file.move_file.configured.app_error",
+ "translation": "File storage not configured properly. Please configure for either S3 or local server file storage."
+ },
+ {
+ "id": "api.file.file_upload.exceeds",
+ "translation": "File exceeds max image size."
},
{
"id": "api.file.file_upload.exceeds",