From 9af66a196315ecf7cb963aae32bbda4471891a5d Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Mon, 22 May 2017 10:13:37 +0200 Subject: [PATCH] mediaapi/writers: Reuse removeDir throughout the package --- .../dendrite/mediaapi/writers/download.go | 20 ++++--------------- .../dendrite/mediaapi/writers/fileutils.go | 7 +++++++ .../dendrite/mediaapi/writers/upload.go | 7 ------- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/mediaapi/writers/download.go b/src/github.com/matrix-org/dendrite/mediaapi/writers/download.go index 54bb3d5b1..30d262a46 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/writers/download.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/writers/download.go @@ -396,10 +396,7 @@ func (r *downloadRequest) commitFileAndMetadata(tmpDir types.Path, absBasePath t finalPath, err := getPathFromMediaMetadata(r.MediaMetadata, absBasePath) if err != nil { r.Logger.WithError(err).Warn("Failed to get file path from metadata") - tmpDirErr := os.RemoveAll(string(tmpDir)) - if tmpDirErr != nil { - r.Logger.WithError(tmpDirErr).WithField("dir", tmpDir).Warn("Failed to remove tmpDir") - } + removeDir(tmpDir, r.Logger) return updateActiveRemoteRequests } @@ -409,10 +406,7 @@ func (r *downloadRequest) commitFileAndMetadata(tmpDir types.Path, absBasePath t ) if err != nil { r.Logger.WithError(err).WithField("dst", finalPath).Warn("Failed to move file to final destination") - tmpDirErr := os.RemoveAll(string(tmpDir)) - if tmpDirErr != nil { - r.Logger.WithError(tmpDirErr).WithField("dir", tmpDir).Warn("Failed to remove tmpDir") - } + removeDir(tmpDir, r.Logger) return updateActiveRemoteRequests } @@ -428,10 +422,7 @@ func (r *downloadRequest) commitFileAndMetadata(tmpDir types.Path, absBasePath t err = db.StoreMediaMetadata(r.MediaMetadata) if err != nil { finalDir := path.Dir(finalPath) - finalDirErr := os.RemoveAll(finalDir) - if finalDirErr != nil { - r.Logger.WithError(finalDirErr).WithField("dir", finalDir).Warn("Failed to remove finalDir") - } + removeDir(types.Path(finalDir), r.Logger) completeRemoteRequest(activeRemoteRequests, mxcURL) return updateActiveRemoteRequests } @@ -526,10 +517,7 @@ func (r *downloadRequest) respondFromRemoteFile(w http.ResponseWriter, absBasePa logFields["MaxFileSizeBytes"] = maxFileSizeBytes } r.Logger.WithError(fetchError).WithFields(logFields).Warn("Error while fetching file") - tmpDirErr := os.RemoveAll(string(tmpDir)) - if tmpDirErr != nil { - r.Logger.WithError(tmpDirErr).WithField("dir", tmpDir).Warn("Failed to remove tmpDir") - } + removeDir(tmpDir, r.Logger) // Note: if we have responded with any data in the body at all then we have already sent 200 OK and we can only abort at this point if bytesResponded < 1 { r.jsonErrorResponse(w, util.JSONResponse{ diff --git a/src/github.com/matrix-org/dendrite/mediaapi/writers/fileutils.go b/src/github.com/matrix-org/dendrite/mediaapi/writers/fileutils.go index 2b6bf16be..d158a1029 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/writers/fileutils.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/writers/fileutils.go @@ -29,6 +29,13 @@ import ( "github.com/matrix-org/util" ) +func removeDir(dir types.Path, logger *log.Entry) { + dirErr := os.RemoveAll(string(dir)) + if dirErr != nil { + logger.WithError(dirErr).WithField("dir", dir).Warn("Failed to remove directory") + } +} + // createTempDir creates a tmp/ directory within baseDirectory and returns its path func createTempDir(baseDirectory types.Path) (types.Path, error) { baseTmpDir := path.Join(string(baseDirectory), "tmp") diff --git a/src/github.com/matrix-org/dendrite/mediaapi/writers/upload.go b/src/github.com/matrix-org/dendrite/mediaapi/writers/upload.go index f5c8322fd..132af87fd 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/writers/upload.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/writers/upload.go @@ -94,13 +94,6 @@ type uploadResponse struct { ContentURI string `json:"content_uri"` } -func removeDir(dir types.Path, logger *log.Entry) { - dirErr := os.RemoveAll(string(dir)) - if dirErr != nil { - logger.WithError(dirErr).WithField("dir", dir).Warn("Failed to remove directory") - } -} - // parseAndValidateRequest parses the incoming upload request to validate and extract // all the metadata about the media being uploaded. Returns either an uploadRequest or // an error formatted as a util.JSONResponse