api: refactor download subreddit image

This commit is contained in:
Tigor Hutasuhut 2024-04-14 00:49:36 +07:00
parent bf998e9ef9
commit 8d2d44597c

View file

@ -82,7 +82,7 @@ func (api *API) DownloadSubredditImages(ctx context.Context, subredditName strin
} }
func (api *API) downloadSubredditListImage(ctx context.Context, list reddit.Listing, params DownloadSubredditParams) error { func (api *API) downloadSubredditListImage(ctx context.Context, list reddit.Listing, params DownloadSubredditParams) error {
ctx, span := tracer.Start(ctx, "*API.downloadSubredditImage") ctx, span := tracer.Start(ctx, "*API.downloadSubredditListImage")
defer span.End() defer span.End()
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
@ -98,74 +98,74 @@ func (api *API) downloadSubredditListImage(ctx context.Context, list reddit.List
wg.Add(1) wg.Add(1)
api.imageSemaphore <- struct{}{} api.imageSemaphore <- struct{}{}
go func(ctx context.Context, post reddit.Post) { go func(ctx context.Context, post reddit.Post) {
ctx, span := tracer.Start(ctx, "*API.downloadSubredditImage.goFunc")
defer span.End()
defer func() { defer func() {
<-api.imageSemaphore <-api.imageSemaphore
wg.Done() wg.Done()
}() }()
if err := api.downloadSubredditImage(ctx, post, devices); err != nil {
log.New(ctx).Err(err).Error("failed to download subreddit image")
}
}(ctx, post)
}
wg.Wait()
return nil
}
func (api *API) downloadSubredditImage(ctx context.Context, post reddit.Post, devices []queries.Device) error {
ctx, span := tracer.Start(ctx, "*API.downloadSubredditImage")
defer span.End()
imageHandler, err := api.reddit.DownloadImage(ctx, post, api.downloadBroadcast) imageHandler, err := api.reddit.DownloadImage(ctx, post, api.downloadBroadcast)
if err != nil { if err != nil {
log.New(ctx).Err(err).Error("failed to download image") return errs.Wrapw(err, "failed to download image")
return
} }
defer imageHandler.Close() defer imageHandler.Close()
// copy to temp dir first to avoid copying incomplete files. // copy to temp dir first to avoid copying incomplete files.
tmpImageFile, err := api.copyImageToTempDir(ctx, imageHandler) tmpImageFile, err := api.copyImageToTempDir(ctx, imageHandler)
if err != nil { if err != nil {
log.New(ctx).Err(err).Error("failed to download image to temp file") return errs.Wrapw(err, "failed to download image to temp file")
return
} }
defer tmpImageFile.Close() defer tmpImageFile.Close()
w, close, err := api.createDeviceImageWriters(post, devices) w, close, err := api.createDeviceImageWriters(post, devices)
if err != nil { if err != nil {
log.New(ctx).Err(err).Error("failed to create image files") return errs.Wrapw(err, "failed to create image files")
return
} }
defer close() defer close()
_, err = io.Copy(w, tmpImageFile) _, err = io.Copy(w, tmpImageFile)
if err != nil { if err != nil {
log.New(ctx).Err(err).Error("failed to create save image files") return errs.Wrapw(err, "failed to save image files")
return
} }
thumbnailPath := post.GetThumbnailTargetPath(api.config) thumbnailPath := post.GetThumbnailTargetPath(api.config)
_, errStat := os.Stat(thumbnailPath) _, errStat := os.Stat(thumbnailPath)
if errStat == nil { if errStat == nil {
// file exist // file exist
return return nil
} }
if !errors.Is(errStat, os.ErrNotExist) { if !errors.Is(errStat, os.ErrNotExist) {
log.New(ctx).Err(err).Error("failed to check thumbail existence", "path", thumbnailPath) return errs.Wrapw(err, "failed to check thumbail existence", "path", thumbnailPath)
return
} }
thumbnailSource, err := imaging.Open(tmpImageFile.filename) thumbnailSource, err := imaging.Open(tmpImageFile.filename)
if err != nil { if err != nil {
log.New(ctx).Err(err).Error("failed to open temp thumbnail file", "filename", tmpImageFile.filename) return errs.Wrapw(err, "failed to open temp thumbnail file", "filename", tmpImageFile.filename)
return
} }
thumbnail := imaging.Resize(thumbnailSource, 256, 0, imaging.Lanczos) thumbnail := imaging.Resize(thumbnailSource, 256, 0, imaging.Lanczos)
thumbnailFile, err := os.Create(thumbnailPath) thumbnailFile, err := os.Create(thumbnailPath)
if err != nil { if err != nil {
log.New(ctx).Err(err).Error("failed to create thumbnail file", "filename", thumbnailPath) return errs.Wrapw(err, "failed to create thumbnail file", "filename", thumbnailPath)
return
} }
defer thumbnailFile.Close() defer thumbnailFile.Close()
err = jpeg.Encode(thumbnailFile, thumbnail, nil) err = jpeg.Encode(thumbnailFile, thumbnail, nil)
if err != nil { if err != nil {
log.New(ctx).Err(err).Error("failed to encode thumbnail file to jpeg", "filename", thumbnailPath) return errs.Wrapw(err, "failed to encode thumbnail file to jpeg", "filename", thumbnailPath)
return
} }
}(ctx, post)
}
wg.Wait()
return nil return nil
} }