From 547e73846df7ef7a7bfc9df2cc0453b36ee086b4 Mon Sep 17 00:00:00 2001 From: Tigor Hutasuhut Date: Tue, 30 Apr 2024 12:56:41 +0700 Subject: [PATCH] api: isImageExists now returns false if the image does not exist in the target path or database --- api/download_subreddit_images.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/api/download_subreddit_images.go b/api/download_subreddit_images.go index f55d6c4..d5c22d5 100644 --- a/api/download_subreddit_images.go +++ b/api/download_subreddit_images.go @@ -276,22 +276,14 @@ func (api *API) isImageExists(ctx context.Context, post reddit.Post, device *mod ctx, span := tracer.Start(ctx, "*API.IsImageExists") defer span.End() - _, err := models.Images.Query(ctx, api.db, + _, errQuery := models.Images.Query(ctx, api.db, models.SelectWhere.Images.DeviceID.EQ(device.ID), models.SelectWhere.Images.PostID.EQ(post.GetID()), ).One() - if err != nil { - if err.Error() == "sql: no rows in result set" { - return false - } - } - // Image does not exist in target path. - if _, err := os.Stat(post.GetImageTargetPath(api.config, device)); err != nil { - return false - } + _, errStat := os.Stat(post.GetImageTargetPath(api.config, device)) - return true + return errQuery == nil && errStat == nil } func shouldDownloadPostForDevice(post reddit.Post, device *models.Device) bool {