Completed
Push — master ( 515139...fcdc06 )
by Dan Michael O.
19:00
created

CoverCache::download()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Colligator;
4
5
use Intervention\Image\ImageManager;
6
use League\Flysystem\AdapterInterface;
7
use League\Flysystem\Config as FlysystemConfig;
8
9
class CoverCache
10
{
11
    protected $filesystem;
12
    protected $imageManager;
13
    protected $fsConfig;
14
15
    public function __construct(AdapterInterface $filesystem, ImageManager $imageManager, FlysystemConfig $fsConfig)
16
    {
17
        $this->filesystem = $filesystem;
18
        $this->imageManager = $imageManager;
19
        $this->fsConfig = $fsConfig;
20
    }
21
22
    /**
23
     * @param string $key
24
     *
25
     * @return string
26
     */
27
    public function url($key)
28
    {
29
        return sprintf('https://s3.%s.amazonaws.com/%s/%s',
30
            config('filesystems.disks.s3.region'),
31
            config('filesystems.disks.s3.bucket'),
32
            $key
33
        );
34
    }
35
36
    /**
37
     * @param string $url
38
     * @param int    $maxHeight
39
     *
40
     * @throws \ErrorException
41
     *
42
     * @return CachedImage
43
     */
44
    public function putUrl($url, $maxHeight = 0)
45
    {
46
        return $this->store($url, null, $maxHeight);
47
    }
48
49
    /**
50
     * @param string $blob
51
     * @param int    $maxHeight
52
     *
53
     * @throws \ErrorException
54
     *
55
     * @return CachedImage
56
     */
57
    public function putBlob($blob, $maxHeight = 0)
58
    {
59
        return $this->store(null, $blob, $maxHeight);
60
    }
61
62
    /**
63
     * Retrieves the content of an URL.
64
     *
65
     * @return string
66
     */
67
    protected function download($sourceUrl)
68
    {
69
        // TODO: Use flysystem-http-downloader instead, but needs update
70
        // https://github.com/indigophp/flysystem-http-downloader/pull/2
71
        return file_get_contents($sourceUrl);
72
    }
73
74
    /**
75
     * Store a file in cache.
76
     *
77
     * @param string $sourceUrl
78
     * @param string $data
79
     * @param int $maxHeight
80
     * @return CachedImage
81
     * @throws \ErrorException
82
     */
83
    protected function store($sourceUrl = null, $data = null, $maxHeight = 0)
84
    {
85
        if (is_null($data)) {
86
            $data = $this->download($sourceUrl);
87
            if (!$data) {
88
                throw new \ErrorException('[CoverCache] Failed to download ' . $sourceUrl);
89
            }
90
        }
91
92
        $cacheKey = sha1($data);
93
94
        $img = $this->imageManager->make($data);
95
        if ($maxHeight && $img->height() > $maxHeight) {
96
            \Log::debug('[CachedImage] Resizing from ' . $img->height() . ' to ' . $maxHeight);
97
            $img->heighten($maxHeight);
98
            $data = strval($img->encode('jpg'));
99
        }
100
101
        if ($img->width() / $img->height() > 1.4) {
102
            throw new \ErrorException('[CoverCache] Not accepting images with w/h ratio > 1.4');
103
        }
104
105
106
        \Log::debug('[CachedImage] Storing image as ' . $img->width() . ' x ' . $img->height() . ', ' . strlen($data) . ' bytes');
107
        if (!$this->filesystem->write($cacheKey, $data, $this->fsConfig)) {
108
            throw new \ErrorException('[CoverCache] Failed to upload thumb to S3');
109
        }
110
111
        \Log::debug('[CachedImage] Wrote cached version as ' . $cacheKey);
112
113
        return new CachedImage($this, $cacheKey);
114
    }
115
116
    public function getMetadata($cacheKey) {
117
        $data = $this->filesystem->read($cacheKey);
118
        $contents = strval($data['contents']);
119
        $img = $this->imageManager->make($contents);
120
121
        return [
122
            'size'   => strlen($contents),
123
            'width'  => $img->width(),
124
            'height' => $img->height(),
125
            'mime'   => $img->mime(),
126
        ];
127
    }
128
129
    /**
130
     * Return a representation with height no more than $maxHeight.
131
     *
132
     * @param string $maxHeight
133
     *
134
     * @throws \ErrorException
135
     *
136
     * @return CachedImage
137
     */
138
    public function thumb($cacheKey, $maxHeight)
139
    {
140
        $data = $this->filesystem->read($cacheKey);
141
        $blob = strval($data['contents']);
142
        \Log::debug('[CachedImage] Read ' .$cacheKey . ': ' . strlen($blob) . ' bytes');
143
144
        return $this->putBlob($blob, $maxHeight);
145
    }
146
}
147