-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhandler.php
264 lines (219 loc) · 10.9 KB
/
handler.php
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
* Created by PhpStorm.
* User: Youi
* Date: 2015-12-09
* Time: 21:53
*/
class handler {
/**
* @var mc|null $mc
*/
private static $mc;
public static function loadMemcached($mc = null) {
!static::$mc && (static::$mc = $mc ?: new mc());
}
/**
* get post identity params from url
* @param string $url
* @return array|bool params for constructing api query array('post_domain'=>'xx.tumblr.com', 'post_id'=>xxxx)
*/
private static function parseUrlParam($url) {
if (preg_match('<https?://(.+)/post/(\d+)>', $url, $match)) {
return [
'post_domain' => $match[1],
'post_id' => $match[2]
];
}
else return false;
}
public static function handle($url, $packImages = false) {
$postParam = static::parseUrlParam($url);
$quickInfo = null;
# try to process it
try {
# a valid tumblr url given
if ($postParam) {
$cachedInfo = Input::fetchQuickInfoCache($postParam);
# quick response info found
if ($cachedInfo) {
# make just header response
if ($_SERVER['REQUEST_METHOD'] === 'HEAD') {
syslog(LOG_INFO, "HEAD Response.");
foreach ($cachedInfo['headers'] as $header) {
header($header);
}
}
# make content response
else {
syslog(LOG_INFO, "Content Response.");
$content = $cachedInfo['content'];
switch ($cachedInfo['type']) {
case 'html':
Output::echoHtmlFile($content);
break;
case 'video':
case 'singlePhoto':
Output::redirect($content);
break;
case 'htmlZip':
Output::echoZipFile($content);
break;
case 'error':
Output::echoTxtFile($content);
break;
}
}
}
# no quick response found, we got to process it
else {
$postJSON = Input::fetchPostInfoCache($postParam) ?: Input::queryTumblrApi($postParam);
# post json gotten
if ($postJSON) {
# save post info to memcached
Output::setPostInfoCache($postParam, $postJSON);
$postInfo = $postJSON['posts'][0];
$postType = Content::parsePostType($postInfo);
$parserName = 'parse' . ucfirst($postType);
switch ($postType) {
case 'audio':
case 'answer':
case 'conversation':
case 'link':
case 'regular':
case 'quote':
$output = Content::$parserName($postInfo);
$zipStr = Content::getHtmlZipPack($output);
Output::echoZipFile($zipStr);
$quickInfo = [
'type' => 'htmlZip',
'content' => $zipStr
];
break;
case 'video':
$output = Content::$parserName($postInfo);
# video source parsed
if ($output) {
Output::redirect($output);
$quickInfo = [
'type' => 'video',
'content' => $output
];
}
# no video parsed
else {
$errMsg = "Can't not parse video post, maybe it's too complicated to get the video source out.";
throw new Exception($errMsg);
}
break;
case 'unknow':
case 'photo':
default:
$photoUrls = Content::$parserName($postInfo);
$photoCount = count($photoUrls);
# photo found
if ($photoCount > 0) {
# one photo
if ($photoCount === 1) {
Output::redirect($photoUrls[0]);
$quickInfo = [
'type' => 'singlePhoto',
'content' => $photoUrls[0]
];
}
# multi photo
else {
# to make a images pack
if ($packImages) {
$imagesCache = Input::fetchImagesCache($photoUrls);
# statement variables
{
$total = count($photoUrls);
$cached = count($imagesCache);
$fetched = 0;
$startTime = microtime(true);
}
# get images
$imagesCont = array_fill_keys($photoUrls, null);
$randomOrder = array_values($photoUrls); shuffle($randomOrder);
foreach ($randomOrder as $imgUrl) {
$fileName = basename($imgUrl);
# image in cache found
if (isset($imagesCache[$fileName])) {
$imagesCont[$imgUrl] = &$imagesCache[$fileName];
}
# not in cache
else {
$imagesCont[$imgUrl] = Input::fetchImage($imgUrl); # fetch from network
$imagesCont[$imgUrl] && static::$mc->singleSet($fileName, $imagesCont[$imgUrl]); # write to cache
$fetched++;
}
}
# output
$zipPack = Content::getImagesZipPack($imagesCont);
Output::echoZipFile($zipPack);
# statement record
$timeUsed = number_format(microtime(true) - $startTime, 3, '.', '');
syslog(LOG_INFO, "Total: $total, From cache: $cached, From network: $fetched, Time used: {$timeUsed}s");
# refresh cache
static::$mc->touchKeys(array_keys($imagesCache));
# Output::writeImagesToCache($images, array_keys($imagesCache));
$quickInfo = [
'HEADOnly' => true,
'headers' => []
];
}
# to make a download page
else {
$page = Content::getImagesDownPage($photoUrls);
$readme = "Sever overloading all the time so no more images packing, open the htm file with google chrome and DIY thank you.\r\n服务器扛不住,取消图片打包,请使用谷歌浏览器打开htm文件自行下载,靴靴。";
$zipStr = Content::getHtmlZipPack($page, null, $readme);
Output::echoZipFile($zipStr);
$quickInfo = [
'type' => 'htmlZip',
'content' => $zipStr
];
}
}
}
# no photo found
else {
$errMsg = "No images found in the tumblr post.";
throw new Exception($errMsg);
}
break;
}
}
# no post json back from tumblr
else {
$postParam = false; # don't write quick response
$errMsg = 'No post info back from Tumblr.';
throw new Exception($errMsg);
}
}
}
# not a valid tumblr url
else {
$errMsg = "Not a valid tumblr URL.";
throw new Exception($errMsg);
}
}
# catch error, generate and output error record
catch (Exception $e) {
$errText = Content::getErrorText($e->getMessage());
$quickInfo = [
'type' => 'error',
'content' => $errText
];
Output::echoTxtFile($errText);
}
# write error record or quick response
finally {
if ($postParam && $quickInfo) {
$quickInfo['headers'] = headers_list();
Output::setQuickInfoCache($postParam, $quickInfo);
}
}
return true;
}
}