From 0fcf74014d623a359ea49d6685be8e6ee18f6869 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Mon, 22 May 2017 16:41:07 +0200 Subject: [PATCH] mediaapi/fileutils: Improve function name and API docs --- .../matrix-org/dendrite/mediaapi/fileutils/fileutils.go | 9 ++++----- .../matrix-org/dendrite/mediaapi/writers/download.go | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/mediaapi/fileutils/fileutils.go b/src/github.com/matrix-org/dendrite/mediaapi/fileutils/fileutils.go index 6779be537..64f4e46da 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/fileutils/fileutils.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/fileutils/fileutils.go @@ -186,12 +186,11 @@ func WriteTempFile(reqReader io.Reader, maxFileSizeBytes types.FileSizeBytes, ab return types.Base64Hash(base64.URLEncoding.EncodeToString(hash[:])), types.FileSizeBytes(bytesResponded), types.FileSizeBytes(bytesWritten), tmpDir, nil } -// GetPathFromMediaMetadata validates and constructs the on-disk path to the media -// based on its Base64Hash +// GetPathFromBase64Hash evaluates the path to a media file from its Base64Hash // If the Base64Hash is long enough, we split it into pieces, creating up to 2 subdirectories // for more manageable browsing and use the remainder as the file name. // For example, if Base64Hash is 'qwerty', the path will be 'q/w/erty'. -func GetPathFromMediaMetadata(base64Hash types.Base64Hash, absBasePath types.Path) (string, error) { +func GetPathFromBase64Hash(base64Hash types.Base64Hash, absBasePath types.Path) (string, error) { var subPath, fileName string hashLen := len(base64Hash) @@ -249,14 +248,14 @@ func moveFile(src types.Path, dst types.Path) error { return nil } -// MoveFileWithHashCheck attempts to move the file src to dst and checks for hash collisions based on metadata +// MoveFileWithHashCheck checks for hash collisions when moving a temporary file to its destination based on metadata // Check if destination file exists. As the destination is based on a hash of the file data, // if it exists and the file size does not match then there is a hash collision for two different files. If // it exists and the file size matches, it is believable that it is the same file and we can just // discard the temporary file. func MoveFileWithHashCheck(tmpDir types.Path, mediaMetadata *types.MediaMetadata, absBasePath types.Path, logger *log.Entry) (string, bool, error) { duplicate := false - finalPath, err := GetPathFromMediaMetadata(mediaMetadata.Base64Hash, absBasePath) + finalPath, err := GetPathFromBase64Hash(mediaMetadata.Base64Hash, absBasePath) if err != nil { RemoveDir(tmpDir, logger) return "", duplicate, fmt.Errorf("failed to get file path from metadata: %q", err) 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 2f8185f5f..7eae64620 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/writers/download.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/writers/download.go @@ -184,7 +184,7 @@ func (r *downloadRequest) respondFromLocalFile(w http.ResponseWriter, absBasePat "Content-Disposition": r.MediaMetadata.ContentDisposition, }).Infof("Downloading file") - filePath, err := fileutils.GetPathFromMediaMetadata(r.MediaMetadata.Base64Hash, absBasePath) + filePath, err := fileutils.GetPathFromBase64Hash(r.MediaMetadata.Base64Hash, absBasePath) if err != nil { // FIXME: Remove erroneous file from database? r.Logger.WithError(err).Warn("Failed to get file path from metadata")