aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper/DownloadImages.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/DownloadImages.php')
-rw-r--r--src/Wallabag/CoreBundle/Helper/DownloadImages.php44
1 files changed, 22 insertions, 22 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/DownloadImages.php b/src/Wallabag/CoreBundle/Helper/DownloadImages.php
index ed888cdb..252ba57c 100644
--- a/src/Wallabag/CoreBundle/Helper/DownloadImages.php
+++ b/src/Wallabag/CoreBundle/Helper/DownloadImages.php
@@ -2,12 +2,12 @@
2 2
3namespace Wallabag\CoreBundle\Helper; 3namespace Wallabag\CoreBundle\Helper;
4 4
5use Psr\Log\LoggerInterface;
6use Symfony\Component\DomCrawler\Crawler;
7use GuzzleHttp\Client; 5use GuzzleHttp\Client;
8use GuzzleHttp\Message\Response; 6use GuzzleHttp\Message\Response;
9use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser; 7use Psr\Log\LoggerInterface;
8use Symfony\Component\DomCrawler\Crawler;
10use Symfony\Component\Finder\Finder; 9use Symfony\Component\Finder\Finder;
10use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
11 11
12class DownloadImages 12class DownloadImages
13{ 13{
@@ -31,17 +31,6 @@ class DownloadImages
31 } 31 }
32 32
33 /** 33 /**
34 * Setup base folder where all images are going to be saved.
35 */
36 private function setFolder()
37 {
38 // if folder doesn't exist, attempt to create one and store the folder name in property $folder
39 if (!file_exists($this->baseFolder)) {
40 mkdir($this->baseFolder, 0755, true);
41 }
42 }
43
44 /**
45 * Process the html and extract image from it, save them to local and return the updated html. 34 * Process the html and extract image from it, save them to local and return the updated html.
46 * 35 *
47 * @param int $entryId ID of the entry 36 * @param int $entryId ID of the entry
@@ -97,9 +86,9 @@ class DownloadImages
97 $relativePath = $this->getRelativePath($entryId); 86 $relativePath = $this->getRelativePath($entryId);
98 } 87 }
99 88
100 $this->logger->debug('DownloadImages: working on image: '.$imagePath); 89 $this->logger->debug('DownloadImages: working on image: ' . $imagePath);
101 90
102 $folderPath = $this->baseFolder.'/'.$relativePath; 91 $folderPath = $this->baseFolder . '/' . $relativePath;
103 92
104 // build image path 93 // build image path
105 $absolutePath = $this->getAbsoluteLink($url, $imagePath); 94 $absolutePath = $this->getAbsoluteLink($url, $imagePath);
@@ -123,7 +112,7 @@ class DownloadImages
123 } 112 }
124 113
125 $hashImage = hash('crc32', $absolutePath); 114 $hashImage = hash('crc32', $absolutePath);
126 $localPath = $folderPath.'/'.$hashImage.'.'.$ext; 115 $localPath = $folderPath . '/' . $hashImage . '.' . $ext;
127 116
128 try { 117 try {
129 $im = imagecreatefromstring($res->getBody()); 118 $im = imagecreatefromstring($res->getBody());
@@ -156,7 +145,7 @@ class DownloadImages
156 145
157 imagedestroy($im); 146 imagedestroy($im);
158 147
159 return $this->wallabagUrl.'/assets/images/'.$relativePath.'/'.$hashImage.'.'.$ext; 148 return $this->wallabagUrl . '/assets/images/' . $relativePath . '/' . $hashImage . '.' . $ext;
160 } 149 }
161 150
162 /** 151 /**
@@ -167,7 +156,7 @@ class DownloadImages
167 public function removeImages($entryId) 156 public function removeImages($entryId)
168 { 157 {
169 $relativePath = $this->getRelativePath($entryId); 158 $relativePath = $this->getRelativePath($entryId);
170 $folderPath = $this->baseFolder.'/'.$relativePath; 159 $folderPath = $this->baseFolder . '/' . $relativePath;
171 160
172 $finder = new Finder(); 161 $finder = new Finder();
173 $finder 162 $finder
@@ -183,6 +172,17 @@ class DownloadImages
183 } 172 }
184 173
185 /** 174 /**
175 * Setup base folder where all images are going to be saved.
176 */
177 private function setFolder()
178 {
179 // if folder doesn't exist, attempt to create one and store the folder name in property $folder
180 if (!file_exists($this->baseFolder)) {
181 mkdir($this->baseFolder, 0755, true);
182 }
183 }
184
185 /**
186 * Generate the folder where we are going to save images based on the entry url. 186 * Generate the folder where we are going to save images based on the entry url.
187 * 187 *
188 * @param int $entryId ID of the entry 188 * @param int $entryId ID of the entry
@@ -192,8 +192,8 @@ class DownloadImages
192 private function getRelativePath($entryId) 192 private function getRelativePath($entryId)
193 { 193 {
194 $hashId = hash('crc32', $entryId); 194 $hashId = hash('crc32', $entryId);
195 $relativePath = $hashId[0].'/'.$hashId[1].'/'.$hashId; 195 $relativePath = $hashId[0] . '/' . $hashId[1] . '/' . $hashId;
196 $folderPath = $this->baseFolder.'/'.$relativePath; 196 $folderPath = $this->baseFolder . '/' . $relativePath;
197 197
198 if (!file_exists($folderPath)) { 198 if (!file_exists($folderPath)) {
199 mkdir($folderPath, 0777, true); 199 mkdir($folderPath, 0777, true);
@@ -270,7 +270,7 @@ class DownloadImages
270 } 270 }
271 271
272 if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'], true)) { 272 if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'], true)) {
273 $this->logger->error('DownloadImages: Processed image with not allowed extension. Skipping: '.$imagePath); 273 $this->logger->error('DownloadImages: Processed image with not allowed extension. Skipping: ' . $imagePath);
274 274
275 return false; 275 return false;
276 } 276 }