mediaapi/writers: Fix InternalServerError responses

This commit is contained in:
Robert Swain 2017-05-31 17:41:42 +02:00
parent 57ba466da1
commit 07c821daec
2 changed files with 14 additions and 28 deletions

View file

@ -119,10 +119,8 @@ func (r *downloadRequest) doDownload(w http.ResponseWriter, cfg *config.MediaAPI
mediaMetadata, err := db.GetMediaMetadata(r.MediaMetadata.MediaID, r.MediaMetadata.Origin) mediaMetadata, err := db.GetMediaMetadata(r.MediaMetadata.MediaID, r.MediaMetadata.Origin)
if err != nil { if err != nil {
r.Logger.WithError(err).Error("Error querying the database.") r.Logger.WithError(err).Error("Error querying the database.")
return &util.JSONResponse{ resErr := jsonerror.InternalServerError()
Code: 500, return &resErr
JSON: jsonerror.InternalServerError(),
}
} }
if mediaMetadata == nil { if mediaMetadata == nil {
if r.MediaMetadata.Origin == cfg.ServerName { if r.MediaMetadata.Origin == cfg.ServerName {
@ -149,27 +147,21 @@ func (r *downloadRequest) respondFromLocalFile(w http.ResponseWriter, absBasePat
filePath, err := fileutils.GetPathFromBase64Hash(r.MediaMetadata.Base64Hash, absBasePath) filePath, err := fileutils.GetPathFromBase64Hash(r.MediaMetadata.Base64Hash, absBasePath)
if err != nil { if err != nil {
r.Logger.WithError(err).Error("Failed to get file path from metadata") r.Logger.WithError(err).Error("Failed to get file path from metadata")
return &util.JSONResponse{ resErr := jsonerror.InternalServerError()
Code: 500, return &resErr
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 {
r.Logger.WithError(err).Error("Failed to open file") r.Logger.WithError(err).Error("Failed to open file")
return &util.JSONResponse{ resErr := jsonerror.InternalServerError()
Code: 500, return &resErr
JSON: jsonerror.InternalServerError(),
}
} }
stat, err := file.Stat() stat, err := file.Stat()
if err != nil { if err != nil {
r.Logger.WithError(err).Error("Failed to stat file") r.Logger.WithError(err).Error("Failed to stat file")
return &util.JSONResponse{ resErr := jsonerror.InternalServerError()
Code: 500, return &resErr
JSON: jsonerror.InternalServerError(),
}
} }
if r.MediaMetadata.FileSizeBytes > 0 && int64(r.MediaMetadata.FileSizeBytes) != stat.Size() { if r.MediaMetadata.FileSizeBytes > 0 && int64(r.MediaMetadata.FileSizeBytes) != stat.Size() {
@ -177,10 +169,8 @@ func (r *downloadRequest) respondFromLocalFile(w http.ResponseWriter, absBasePat
"fileSizeDatabase": r.MediaMetadata.FileSizeBytes, "fileSizeDatabase": r.MediaMetadata.FileSizeBytes,
"fileSizeDisk": stat.Size(), "fileSizeDisk": stat.Size(),
}).Warn("File size in database and on-disk differ.") }).Warn("File size in database and on-disk differ.")
return &util.JSONResponse{ resErr := jsonerror.InternalServerError()
Code: 500, return &resErr
JSON: jsonerror.InternalServerError(),
}
} }
r.Logger.WithFields(log.Fields{ r.Logger.WithFields(log.Fields{
@ -202,10 +192,8 @@ func (r *downloadRequest) respondFromLocalFile(w http.ResponseWriter, absBasePat
if bytesResponded, err := io.Copy(w, file); err != nil { if bytesResponded, err := io.Copy(w, file); err != nil {
r.Logger.WithError(err).Warn("Failed to copy from cache") r.Logger.WithError(err).Warn("Failed to copy from cache")
if bytesResponded == 0 { if bytesResponded == 0 {
return &util.JSONResponse{ resErr := jsonerror.InternalServerError()
Code: 500, return &resErr
JSON: jsonerror.NotFound(fmt.Sprintf("Failed to respond with file with media ID %q", r.MediaMetadata.MediaID)),
}
} }
// If we have written any data then we have already responded with 200 OK and all we can do is close the connection // If we have written any data then we have already responded with 200 OK and all we can do is close the connection
return nil return nil

View file

@ -136,10 +136,8 @@ func (r *uploadRequest) doUpload(reqReader io.Reader, cfg *config.MediaAPI, db *
mediaMetadata, err := db.GetMediaMetadata(r.MediaMetadata.MediaID, r.MediaMetadata.Origin) mediaMetadata, err := db.GetMediaMetadata(r.MediaMetadata.MediaID, r.MediaMetadata.Origin)
if err != nil { if err != nil {
r.Logger.WithError(err).Error("Error querying the database.") r.Logger.WithError(err).Error("Error querying the database.")
return &util.JSONResponse{ resErr := jsonerror.InternalServerError()
Code: 500, return &resErr
JSON: jsonerror.InternalServerError(),
}
} }
if mediaMetadata != nil { if mediaMetadata != nil {