aboutsummaryrefslogtreecommitdiff
path: root/lib/image_cache.go
blob: 58cd09bbb0f504d47433965857d7eb78d67001aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package lib

import (
	"fmt"
	"image"
	"image/jpeg"
	"io"
	"net/http"
	"os"
	"path/filepath"
	"yato/config"
)

// ImageCache handles caching and retrieving images
type ImageCache struct {
	cacheDir string
}

// NewImageCache creates a new ImageCache
func NewImageCache() *ImageCache {
	cacheDir := filepath.Join(config.ConfigDir, config.AppName, "cache")
	return &ImageCache{cacheDir: cacheDir}
}

// GetImage retrieves an image, either from cache or by downloading it
func (c *ImageCache) GetImage(mediaType string, malID int, size string, url string) (image.Image, error) {
	cachePath := c.getCachePath(mediaType, malID, size)

	// Check if the image is already cached
	if img, err := c.loadFromCache(cachePath); err == nil {
		return img, nil
	}

	// If not cached, download and cache the image
	return c.downloadAndCache(url, cachePath)
}

func (c *ImageCache) getCachePath(mediaType string, malID int, size string) string {
	return filepath.Join(c.cacheDir, mediaType, fmt.Sprintf("%d", malID), size+".jpg")
}

func (c *ImageCache) loadFromCache(path string) (image.Image, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	img, err := jpeg.Decode(file)
	if err != nil {
		return nil, err
	}

	return img, nil
}

func (c *ImageCache) downloadAndCache(url, cachePath string) (image.Image, error) {
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("failed to download image: %s", resp.Status)
	}

	// Ensure the cache directory exists
	if err := os.MkdirAll(filepath.Dir(cachePath), 0755); err != nil {
		return nil, err
	}

	// Create the cache file
	cacheFile, err := os.Create(cachePath)
	if err != nil {
		return nil, err
	}
	defer cacheFile.Close()

	// Download and write to cache file
	_, err = io.Copy(cacheFile, resp.Body)
	if err != nil {
		return nil, err
	}

	// Reset file pointer and decode the image
	_, err = cacheFile.Seek(0, 0)
	if err != nil {
		return nil, err
	}

	img, err := jpeg.Decode(cacheFile)
	if err != nil {
		return nil, err
	}

	return img, nil
}