1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: famoser |
5
|
|
|
* Date: 04/09/2017 |
6
|
|
|
* Time: 13:29 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Famoser\XKCDCache\Services; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Exception; |
13
|
|
|
use Famoser\XKCDCache\Entities\Comic; |
14
|
|
|
use Famoser\XKCDCache\Exceptions\ServerException; |
15
|
|
|
use Famoser\XKCDCache\Models\XKCD\XKCDJson; |
16
|
|
|
use Famoser\XKCDCache\Services\Base\BaseService; |
17
|
|
|
use Famoser\XKCDCache\Services\Interfaces\CacheServiceInterface; |
18
|
|
|
use Famoser\XKCDCache\Types\Downloader; |
19
|
|
|
use Famoser\XKCDCache\Types\DownloadStatus; |
20
|
|
|
use Famoser\XKCDCache\Types\ServerError; |
21
|
|
|
use ZipArchive; |
22
|
|
|
|
23
|
|
|
class CacheService extends BaseService implements CacheServiceInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* creates a zip file of all the images contained in the image folder with the target number as filename |
27
|
|
|
* |
28
|
|
|
* @param $number |
29
|
|
|
* @return bool |
30
|
|
|
* @throws ServerException |
31
|
|
|
*/ |
32
|
3 |
|
public function createImageZip($number) |
33
|
|
|
{ |
34
|
|
|
try { |
35
|
3 |
|
$zip = new ZipArchive(); |
36
|
3 |
|
$filename = $this->getSettingService()->getZipCachePath() . DIRECTORY_SEPARATOR . $number . ".zip"; |
37
|
|
|
|
38
|
3 |
|
if ($zip->open($filename, ZipArchive::CREATE) !== true) { |
39
|
|
|
$this->getLoggingService()->log("could not create zip file at " . $filename); |
40
|
|
|
} |
41
|
|
|
|
42
|
3 |
|
$zip->addGlob($this->getSettingService()->getImageCachePath() . DIRECTORY_SEPARATOR . "*"); |
43
|
3 |
|
$this->getLoggingService()->log("created zip file with " . $zip->numFiles . " files. status: " . $zip->status); |
44
|
3 |
|
$zip->close(); |
45
|
3 |
|
return true; |
46
|
|
|
} catch (Exception $ex) { |
47
|
|
|
$this->getLoggingService()->log("failed to create zip: " . $ex); |
48
|
|
|
throw new ServerException(ServerError::ZIP_FAILED); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* returns the newest XKCD comic |
54
|
|
|
* |
55
|
|
|
* @return Comic|null |
56
|
|
|
*/ |
57
|
4 |
|
public function getNewestComic() |
58
|
|
|
{ |
59
|
4 |
|
return $this->getDatabaseService()->getSingleFromDatabase(new Comic(), null, null, "num DESC"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* persists the passed XKCD comic |
64
|
|
|
* |
65
|
|
|
* @param XKCDJson $XKCDComic |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
3 |
|
public function persistComic($XKCDComic) |
69
|
|
|
{ |
70
|
3 |
|
$dbService = $this->getDatabaseService(); |
71
|
|
|
|
72
|
|
|
//construct comic |
73
|
3 |
|
$comic = new Comic(); |
74
|
3 |
|
$comic->num = $XKCDComic->num; |
75
|
3 |
|
$comic->status = DownloadStatus::SUCCESSFUL; |
76
|
3 |
|
$comic->link = $XKCDComic->link; |
77
|
3 |
|
$comic->news = $XKCDComic->news; |
78
|
3 |
|
$comic->transcript = $XKCDComic->transcript; |
79
|
3 |
|
$comic->safe_title = $XKCDComic->safe_title; |
80
|
3 |
|
$comic->alt = $XKCDComic->alt; |
81
|
3 |
|
$comic->img = $XKCDComic->img; |
82
|
3 |
|
$comic->title = $XKCDComic->title; |
83
|
3 |
|
$comic->publish_date = strtotime($XKCDComic->day . "." . $XKCDComic->month . "." . $XKCDComic->year); |
84
|
3 |
|
$comic->download_date_time = time(); |
85
|
3 |
|
$comic->downloaded_by = Downloader::VERSION_1; |
86
|
3 |
|
$comic->json = json_encode($XKCDComic); |
87
|
3 |
|
$comic->filename = substr($comic->img, strrpos($comic->img, "/") + 1); |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
//download image |
91
|
3 |
|
$contents = file_get_contents($comic->img); |
92
|
3 |
|
file_put_contents($this->getSettingService()->getImageCachePath() . DIRECTORY_SEPARATOR . $comic->filename, $contents); |
93
|
|
|
} catch (Exception $ex) { |
94
|
|
|
$comic->status = DownloadStatus::IMAGE_DOWNLOAD_FAILED; |
95
|
|
|
$this->getLoggingService()->log("could not download comic " . $XKCDComic->num . ": " . $ex); |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
return $dbService->saveToDatabase($comic); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* returns the path of the zip for this number |
103
|
|
|
* |
104
|
|
|
* @param $number |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
1 |
|
private function constructZipPath($number) |
108
|
|
|
{ |
109
|
1 |
|
return $this->getSettingService()->getZipCachePath() . DIRECTORY_SEPARATOR . $number . ".zip"; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* returns the file size of the zip with the specified number |
114
|
|
|
* |
115
|
|
|
* @param $number |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
1 |
|
public function getFileSizeOfZip($number) |
119
|
|
|
{ |
120
|
1 |
|
$zipPath = $this->constructZipPath($number); |
121
|
1 |
|
return filesize($zipPath); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* returns the content of the zip with the specified number |
126
|
|
|
* |
127
|
|
|
* @param $number |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
1 |
|
public function getContentOfZip($number) |
131
|
|
|
{ |
132
|
1 |
|
$zipPath = $this->constructZipPath($number); |
133
|
1 |
|
return file_get_contents($zipPath); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* returns the number of the newest zip |
138
|
|
|
* returns false if none found |
139
|
|
|
* |
140
|
|
|
* @return int|false |
141
|
|
|
* @throws ServerException |
142
|
|
|
* @throws ServerException |
143
|
|
|
*/ |
144
|
1 |
|
public function getNewestZip() |
145
|
|
|
{ |
146
|
1 |
|
$newestComic = $this->getNewestComic(); |
147
|
1 |
|
$currentNum = $newestComic->num; |
148
|
|
|
do { |
149
|
1 |
|
$zipPath = $this->constructZipPath($currentNum); |
150
|
1 |
|
$zipExists = file_exists($zipPath); |
151
|
1 |
|
} while (!$zipExists && $currentNum-- > 0); |
152
|
1 |
|
if ($zipExists) { |
153
|
1 |
|
return $currentNum; |
154
|
|
|
} |
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
} |