mediaapi/writers: Reuse removeDir throughout the package

This commit is contained in:
Robert Swain 2017-05-22 10:13:37 +02:00
parent 318531d011
commit 9af66a1963
3 changed files with 11 additions and 23 deletions

View file

@ -396,10 +396,7 @@ func (r *downloadRequest) commitFileAndMetadata(tmpDir types.Path, absBasePath t
finalPath, err := getPathFromMediaMetadata(r.MediaMetadata, absBasePath) finalPath, err := getPathFromMediaMetadata(r.MediaMetadata, absBasePath)
if err != nil { if err != nil {
r.Logger.WithError(err).Warn("Failed to get file path from metadata") r.Logger.WithError(err).Warn("Failed to get file path from metadata")
tmpDirErr := os.RemoveAll(string(tmpDir)) removeDir(tmpDir, r.Logger)
if tmpDirErr != nil {
r.Logger.WithError(tmpDirErr).WithField("dir", tmpDir).Warn("Failed to remove tmpDir")
}
return updateActiveRemoteRequests return updateActiveRemoteRequests
} }
@ -409,10 +406,7 @@ func (r *downloadRequest) commitFileAndMetadata(tmpDir types.Path, absBasePath t
) )
if err != nil { if err != nil {
r.Logger.WithError(err).WithField("dst", finalPath).Warn("Failed to move file to final destination") r.Logger.WithError(err).WithField("dst", finalPath).Warn("Failed to move file to final destination")
tmpDirErr := os.RemoveAll(string(tmpDir)) removeDir(tmpDir, r.Logger)
if tmpDirErr != nil {
r.Logger.WithError(tmpDirErr).WithField("dir", tmpDir).Warn("Failed to remove tmpDir")
}
return updateActiveRemoteRequests return updateActiveRemoteRequests
} }
@ -428,10 +422,7 @@ func (r *downloadRequest) commitFileAndMetadata(tmpDir types.Path, absBasePath t
err = db.StoreMediaMetadata(r.MediaMetadata) err = db.StoreMediaMetadata(r.MediaMetadata)
if err != nil { if err != nil {
finalDir := path.Dir(finalPath) finalDir := path.Dir(finalPath)
finalDirErr := os.RemoveAll(finalDir) removeDir(types.Path(finalDir), r.Logger)
if finalDirErr != nil {
r.Logger.WithError(finalDirErr).WithField("dir", finalDir).Warn("Failed to remove finalDir")
}
completeRemoteRequest(activeRemoteRequests, mxcURL) completeRemoteRequest(activeRemoteRequests, mxcURL)
return updateActiveRemoteRequests return updateActiveRemoteRequests
} }
@ -526,10 +517,7 @@ func (r *downloadRequest) respondFromRemoteFile(w http.ResponseWriter, absBasePa
logFields["MaxFileSizeBytes"] = maxFileSizeBytes logFields["MaxFileSizeBytes"] = maxFileSizeBytes
} }
r.Logger.WithError(fetchError).WithFields(logFields).Warn("Error while fetching file") r.Logger.WithError(fetchError).WithFields(logFields).Warn("Error while fetching file")
tmpDirErr := os.RemoveAll(string(tmpDir)) removeDir(tmpDir, r.Logger)
if tmpDirErr != nil {
r.Logger.WithError(tmpDirErr).WithField("dir", tmpDir).Warn("Failed to remove tmpDir")
}
// 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 // 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 { if bytesResponded < 1 {
r.jsonErrorResponse(w, util.JSONResponse{ r.jsonErrorResponse(w, util.JSONResponse{

View file

@ -29,6 +29,13 @@ import (
"github.com/matrix-org/util" "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/<random string> directory within baseDirectory and returns its path // createTempDir creates a tmp/<random string> directory within baseDirectory and returns its path
func createTempDir(baseDirectory types.Path) (types.Path, error) { func createTempDir(baseDirectory types.Path) (types.Path, error) {
baseTmpDir := path.Join(string(baseDirectory), "tmp") baseTmpDir := path.Join(string(baseDirectory), "tmp")

View file

@ -94,13 +94,6 @@ type uploadResponse struct {
ContentURI string `json:"content_uri"` 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 // parseAndValidateRequest parses the incoming upload request to validate and extract
// all the metadata about the media being uploaded. Returns either an uploadRequest or // all the metadata about the media being uploaded. Returns either an uploadRequest or
// an error formatted as a util.JSONResponse // an error formatted as a util.JSONResponse