mediaapi/writers: Pass used member instead of entire struct

This commit is contained in:
Robert Swain 2017-05-22 15:38:51 +02:00
parent 0ecb645f27
commit 227fe64252
2 changed files with 12 additions and 12 deletions

View file

@ -191,28 +191,28 @@ func ReadAndHashAndWriteWithLimit(reqReader io.Reader, maxFileSizeBytes types.Fi
// If the Base64Hash is long enough, we split it into pieces, creating up to 2 subdirectories // 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 more manageable browsing and use the remainder as the file name.
// For example, if Base64Hash is 'qwerty', the path will be 'q/w/erty'. // For example, if Base64Hash is 'qwerty', the path will be 'q/w/erty'.
func GetPathFromMediaMetadata(m *types.MediaMetadata, absBasePath types.Path) (string, error) { func GetPathFromMediaMetadata(base64Hash types.Base64Hash, absBasePath types.Path) (string, error) {
var subPath, fileName string var subPath, fileName string
hashLen := len(m.Base64Hash) hashLen := len(base64Hash)
switch { switch {
case hashLen < 1: case hashLen < 1:
return "", fmt.Errorf("Invalid filePath (Base64Hash too short): %q", m.Base64Hash) return "", fmt.Errorf("Invalid filePath (Base64Hash too short): %q", base64Hash)
case hashLen > 255: case hashLen > 255:
return "", fmt.Errorf("Invalid filePath (Base64Hash too long - max 255 characters): %q", m.Base64Hash) return "", fmt.Errorf("Invalid filePath (Base64Hash too long - max 255 characters): %q", base64Hash)
case hashLen < 2: case hashLen < 2:
subPath = "" subPath = ""
fileName = string(m.Base64Hash) fileName = string(base64Hash)
case hashLen < 3: case hashLen < 3:
subPath = string(m.Base64Hash[0:1]) subPath = string(base64Hash[0:1])
fileName = string(m.Base64Hash[1:]) fileName = string(base64Hash[1:])
default: default:
subPath = path.Join( subPath = path.Join(
string(m.Base64Hash[0:1]), string(base64Hash[0:1]),
string(m.Base64Hash[1:2]), string(base64Hash[1:2]),
) )
fileName = string(m.Base64Hash[2:]) fileName = string(base64Hash[2:])
} }
filePath, err := filepath.Abs(path.Join( filePath, err := filepath.Abs(path.Join(
@ -256,7 +256,7 @@ func moveFile(src types.Path, dst types.Path) error {
// discard the temporary file. // discard the temporary file.
func MoveFileWithHashCheck(tmpDir types.Path, mediaMetadata *types.MediaMetadata, absBasePath types.Path, logger *log.Entry) (string, bool, error) { func MoveFileWithHashCheck(tmpDir types.Path, mediaMetadata *types.MediaMetadata, absBasePath types.Path, logger *log.Entry) (string, bool, error) {
duplicate := false duplicate := false
finalPath, err := GetPathFromMediaMetadata(mediaMetadata, absBasePath) finalPath, err := GetPathFromMediaMetadata(mediaMetadata.Base64Hash, absBasePath)
if err != nil { if err != nil {
RemoveDir(tmpDir, logger) RemoveDir(tmpDir, logger)
return "", duplicate, fmt.Errorf("failed to get file path from metadata: %q", err) return "", duplicate, fmt.Errorf("failed to get file path from metadata: %q", err)

View file

@ -184,7 +184,7 @@ func (r *downloadRequest) respondFromLocalFile(w http.ResponseWriter, absBasePat
"Content-Disposition": r.MediaMetadata.ContentDisposition, "Content-Disposition": r.MediaMetadata.ContentDisposition,
}).Infof("Downloading file") }).Infof("Downloading file")
filePath, err := fileutils.GetPathFromMediaMetadata(r.MediaMetadata, absBasePath) filePath, err := fileutils.GetPathFromMediaMetadata(r.MediaMetadata.Base64Hash, absBasePath)
if err != nil { if err != nil {
// FIXME: Remove erroneous file from database? // FIXME: Remove erroneous file from database?
r.Logger.WithError(err).Warn("Failed to get file path from metadata") r.Logger.WithError(err).Warn("Failed to get file path from metadata")