Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
linters
Browse files Browse the repository at this point in the history
  • Loading branch information
adnull committed Oct 1, 2024
1 parent ff87ec3 commit c860640
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions mediaapi/routing/url_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func makeUrlPreviewHandler(
}
}

urlParsed, err := url.Parse(pUrl)
if err != nil {
urlParsed, perr := url.Parse(pUrl)
if perr != nil {
return util.ErrorResponse(ErrorMissingUrl)
}

Expand Down Expand Up @@ -175,7 +175,6 @@ func makeUrlPreviewHandler(

var result *types.UrlPreview
var err error
var imgReader *http.Response
var mediaData *types.MediaMetadata
var width, height int

Expand All @@ -184,7 +183,7 @@ func makeUrlPreviewHandler(
result = getPreviewFromHTML(resp, urlParsed)
if result.ImageUrl != "" {
// In case of an image in the preview we download it
if imgReader, err = downloadUrl(result.ImageUrl, time.Duration(cfg.UrlPreviewTimeout)*time.Second); err == nil {
if imgReader, err := downloadUrl(result.ImageUrl, time.Duration(cfg.UrlPreviewTimeout)*time.Second); err == nil {
mediaData, width, height, _ = downloadAndStoreImage("url_preview", req.Context(), imgReader, cfg, device, db, activeThumbnailGeneration, logger)
}
// We don't show the original image in the preview
Expand Down Expand Up @@ -344,12 +343,12 @@ func downloadAndStoreImage(
if cfg.MaxFileSizeBytes > 0 {
reqReader = io.LimitReader(reqReader, int64(cfg.MaxFileSizeBytes)+1)
}
hash, bytesWritten, tmpDir, err := fileutils.WriteTempFile(ctx, reqReader, cfg.AbsBasePath)
if err != nil {
logger.WithError(err).WithFields(log.Fields{
hash, bytesWritten, tmpDir, fileErr := fileutils.WriteTempFile(ctx, reqReader, cfg.AbsBasePath)
if fileErr != nil {
logger.WithError(fileErr).WithFields(log.Fields{
"MaxFileSizeBytes": cfg.MaxFileSizeBytes,
}).Warn("Error while transferring file")
return nil, width, height, err
return nil, width, height, fileErr
}
defer fileutils.RemoveDir(tmpDir, logger)

Expand All @@ -362,7 +361,6 @@ func downloadAndStoreImage(
existingMetadata, err := db.GetMediaMetadataByHash(
ctx, hash, cfg.Matrix.ServerName,
)

if err != nil {
logger.WithError(err).Error("unable to get media metadata by hash")
return nil, width, height, err
Expand All @@ -376,18 +374,18 @@ func downloadAndStoreImage(
if err != nil {
return nil, width, height, err
}
width, height, err := thumbnailer.GetImageSize(string(filePath))
width, height, err = thumbnailer.GetImageSize(string(filePath))
if err != nil {
return nil, width, height, err
}
return existingMetadata, width, height, nil
}

tmpFileName := filepath.Join(string(tmpDir), "content")
fileType, err := detectFileType(tmpFileName, logger)
if err != nil {
fileType, typeErr := detectFileType(tmpFileName, logger)
if typeErr != nil {
logger.WithError(err).Error("unable to detect file type")
return nil, width, height, err
return nil, width, height, typeErr
}
logger.WithField("contentType", fileType).Debug("uploaded file is an image")

Expand Down Expand Up @@ -418,10 +416,10 @@ func downloadAndStoreImage(

}

thumbnailFileInfo, err := os.Stat(thumbnailPath)
if err != nil {
logger.WithError(err).Error("unable to get thumbnail file info")
return nil, width, height, err
thumbnailFileInfo, statErr := os.Stat(thumbnailPath)
if statErr != nil {
logger.WithError(statErr).Error("unable to get thumbnail file info")
return nil, width, height, statErr
}

r := &uploadRequest{
Expand All @@ -432,10 +430,10 @@ func downloadAndStoreImage(
}

// Move the thumbnail to the media store
mediaID, err := r.generateMediaID(ctx, db)
if err != nil {
logger.WithError(err).Error("unable to generate media ID")
return nil, width, height, err
mediaID, mediaErr := r.generateMediaID(ctx, db)
if mediaErr != nil {
logger.WithError(mediaErr).Error("unable to generate media ID")
return nil, width, height, mediaErr
}
mediaMetaData := &types.MediaMetadata{
MediaID: mediaID,
Expand All @@ -448,10 +446,10 @@ func downloadAndStoreImage(
UserID: userid,
}

finalPath, err := fileutils.GetPathFromBase64Hash(mediaMetaData.Base64Hash, cfg.AbsBasePath)
if err != nil {
logger.WithError(err).Error("unable to get path from base64 hash")
return nil, width, height, err
finalPath, pathErr := fileutils.GetPathFromBase64Hash(mediaMetaData.Base64Hash, cfg.AbsBasePath)
if pathErr != nil {
logger.WithError(pathErr).Error("unable to get path from base64 hash")
return nil, width, height, pathErr
}
err = fileutils.MoveFile(types.Path(thumbnailPath), types.Path(finalPath))
if err != nil {
Expand Down

0 comments on commit c860640

Please sign in to comment.