mediaapi/writers/download: Escalate corrupt db/file cases to errors

main
Robert Swain 2017-05-31 14:33:49 +02:00
parent 9606ea28ce
commit f0c717b0a1
1 changed files with 9 additions and 12 deletions

View File

@ -148,30 +148,27 @@ func (r *downloadRequest) doDownload(w http.ResponseWriter, cfg *config.MediaAPI
func (r *downloadRequest) respondFromLocalFile(w http.ResponseWriter, absBasePath types.Path) *util.JSONResponse { func (r *downloadRequest) respondFromLocalFile(w http.ResponseWriter, absBasePath types.Path) *util.JSONResponse {
filePath, err := fileutils.GetPathFromBase64Hash(r.MediaMetadata.Base64Hash, absBasePath) filePath, err := fileutils.GetPathFromBase64Hash(r.MediaMetadata.Base64Hash, absBasePath)
if err != nil { if err != nil {
// FIXME: Remove erroneous file from database? r.Logger.WithError(err).Error("Failed to get file path from metadata")
r.Logger.WithError(err).Warn("Failed to get file path from metadata")
return &util.JSONResponse{ return &util.JSONResponse{
Code: 404, Code: 500,
JSON: jsonerror.NotFound(fmt.Sprintf("File with media ID %q does not exist", r.MediaMetadata.MediaID)), JSON: jsonerror.InternalServerError(),
} }
} }
file, err := os.Open(filePath) file, err := os.Open(filePath)
defer file.Close() defer file.Close()
if err != nil { if err != nil {
// FIXME: Remove erroneous file from database? r.Logger.WithError(err).Error("Failed to open file")
r.Logger.WithError(err).Warn("Failed to open file")
return &util.JSONResponse{ return &util.JSONResponse{
Code: 404, Code: 500,
JSON: jsonerror.NotFound(fmt.Sprintf("File with media ID %q does not exist", r.MediaMetadata.MediaID)), JSON: jsonerror.InternalServerError(),
} }
} }
stat, err := file.Stat() stat, err := file.Stat()
if err != nil { if err != nil {
// FIXME: Remove erroneous file from database? r.Logger.WithError(err).Error("Failed to stat file")
r.Logger.WithError(err).Warn("Failed to stat file")
return &util.JSONResponse{ return &util.JSONResponse{
Code: 404, Code: 500,
JSON: jsonerror.NotFound(fmt.Sprintf("File with media ID %q does not exist", r.MediaMetadata.MediaID)), JSON: jsonerror.InternalServerError(),
} }
} }