forked from JamBrain/JamBrain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.php
371 lines (310 loc) · 8.9 KB
/
content.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
require_once __DIR__."/../src/shrub/src/core/core.php";
$RESPONSE = [];
function TrimName($name) {
return coreSlugify_File(substr($name, 0, 32));
}
$in_part = core_GetAPIRequest();
$in_part = array_map('TrimName', $in_part);
$in_file = implode('/', $in_part);
$in_path = dirname($in_file);
$in_paths = explode('/', $in_path);
$in_basename = basename($in_file);
$in_ext_part = explode('.', $in_basename);
$in_ext_part = array_map('TrimName', $in_ext_part);
$in_name = array_shift($in_ext_part);
$in_ext = array_shift($in_ext_part);
$user_id = null;
$asset_id = null;
// Check if the path is to a legal user file
if ( ($in_paths_count = count($in_paths)) > 1 && $in_paths[$in_paths_count-1] == 'z' ) {
$user_id = hexdec(strrev(implode('', array_slice($in_paths, 0, -1))));
$RESPONSE['user'] = $user_id;
$asset_id = hexdec($in_name);
$RESPONSE['asset'] = $asset_id;
}
const SRC_DIR = 'raw';
const OUT_DIR = 'content';
$src_file = SRC_DIR.'/'.$in_path.'/'.$in_name.'.'.$in_ext;
$src_path = dirname($src_file);
$src_fullfile = __DIR__.'/'.$src_file;
$src_fullpath = __DIR__.'/'.$src_path;
$out_file = OUT_DIR.'/'.$in_file;
$out_path = dirname($out_file);
$out_fullfile = __DIR__.'/'.$out_file;
$out_fullpath = __DIR__.'/'.$out_path;
$src_relativefile = str_repeat('../', count(explode('/', $out_path))).$src_file;
// TODO: Sort extensions other than the input and output extensions, and symlink the file you should really be requesting
function Emit( $response ) {
if ( !isset($response['status']) ) {
$response = ['status' => 200] + $response;
}
http_response_code($response['status']);
header('Content-Type: application/json');
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
$json_format = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
if ( isset($_GET['pretty']) ) {
$json_format |= JSON_PRETTY_PRINT;
}
echo json_encode($response, $json_format);
exit;
}
function EmitError( $code, $message = null ) {
global $RESPONSE;
$ERROR = ['status' => $code];
if ( $message )
$ERROR['message'] = $message;
$ERROR['data'] = $RESPONSE;
Emit($ERROR);
}
// Make sure file exists, otherwise we're done
if ( !file_exists($src_file) ) {
EmitError(404, 'File not found');
}
const IMAGE_TYPE = [
'png',
'jpg',
'gif',
'webp', // Output only //
];
const IMAGE_MIME = [
'png' => 'image/png',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'webp' => 'image/webp',
];
// Globals
$out['width'] = null;
$out['height'] = null;
$out['format'] = null;
$out['color'] = null;
$out['fit'] = null;
$out['noopt'] = null;
$out['debug'] = null;
function hasChanges( &$arr ) {
foreach ( $arr as &$value ) {
if ( !is_null($value) )
return true;
}
return false;
}
function redirectToSelfAndExit() {
global $out_file;
http_response_code(302); // Temporary
header('Cache-Control: no-cache'); // Should stop CloudFlare from caching this response
// Force redirect to data
header('Location: '.
isset($_SERVER['HTTPS']) ? 'https://' : 'http://'.
$_SERVER['SERVER_NAME'].
'/'.
$out_file
);
exit;
}
function do_proc($cmd,&$data) {
$proc = proc_open(
$cmd,
[
['pipe','r'],
['pipe','w'],
['pipe','w']
],
$pipes
);
if ($proc) {
fwrite($pipes[0],$data);
fclose($pipes[0]);
$ret = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$err = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$code = proc_close($proc);
if ( $code ) {
EmitError(500, 'PROC ERROR ['.$code.']: '.$err);
exit;
}
return $ret;
}
return null;
}
// If there are any extensions left left, use them as arguments
foreach( $in_ext_part as &$value ) {
if ( strlen($value) < 1 ) {
EmitError(400, "Invalid property");
}
else if ( $value == 'debug' ) {
$out['debug'] = true;
}
else if ( $value == 'fit' ) {
$out['fit'] = true;
}
else if ( $value == 'noopt' ) {
$out['noopt'] = true;
}
else if ( array_search($value, IMAGE_TYPE) !== false ) {
$out['format'] = $value;
}
else if ( $value[0] == 'w' && ($v = intval(substr($value, 1))) > 0 ) {
if ( $v > 4096 )
EmitError(400, "Bad Width: '$v' (max 4096)");
$out['width'] = $v;
}
else if ( $value[0] == 'h' && ($v = intval(substr($value, 1))) > 0 ) {
if ( $v > 4096 )
EmitError(400, "Bad Height: '$v' (max 4096)");
$out['height'] = $v;
}
else if ( ($pos = strpos($value, 'x')) > 0 && ($w = intval($value)) > 0 && ($h = intval(substr($value, $pos+1))) > 0 ) {
if ( $w > 4096 )
EmitError(400, "Bad Width: '$w' (max 4096)");
if ( $h > 4096 )
EmitError(400, "Bad Height: '$h' (max 4096)");
$out['width'] = $w;
$out['height'] = $h;
}
else if ( $value[0] == 'c' ) {
$out['color'] = substr($value, 1, 6);
}
else {
EmitError(400, "Unknown or Invalid property: '$value'");
}
}
// If a change has been requested
if ( hasChanges($out) ) {
// confirm that final extension is an output format
if ( ($of = $in_paths[count($in_paths)-1]) && ($of == 'debug' || array_search($of, IMAGE_TYPE) !== false) ) {
EmitError(400, "Final extension must be an output format. Invalid format '$of'");
}
// Glob and make sure there are not too many files already
if ( ($globs = count(glob($out_path."/".$in_name.'.'.$in_ext.'.*'))) > 64 ) {
EmitError(400, "Too many variations of file: $globs. TODO: Purge");
}
$data = null;
$in = $src_fullfile;
// Step 0: Read file
if ( /*$src_is_video ||*/ $in_ext == 'gif' ) {
$dummy = "";
$data = do_proc(
'ffmpeg -i '.$in.' -loglevel quiet -vframes 1 -f apng pipe:1',
$dummy
);
// Force conversion (even though it should be the case) //
$file_out_convert = true;
}
else if ( $in_ext && array_search($in_ext, IMAGE_TYPE) !== false ) {
$data = file_get_contents($in);
}
else {
EmitError(400, "Unsupported input");
}
// Step 1: Parse file
$image_info = getimagesizefromstring($data);
// $image_info = getimagesize($src_fullfile);
if ( $image_info ) {
// It's faster to re-extract the data then ask the database
$asset = [
'width' => $image_info[0],
'height' => $image_info[1],
'mime' => $image_info['mime'],
'size' => strlen($data), // getimagesizefromstring only
];
$RESPONSE['asset'] = $asset;
// Step 2: Resize, Crop, and/or Convert the file
if ( $out['width'] || $out['height'] || $out['format'] != $in_ext ) {
// Strip extra data //
$option = '-strip';
// http://www.imagemagick.org/Usage/resize/
if ( $out['width'] || $out['height'] ) {
if ( $out['width'] && $out['height'] ) {
$option .= ' -resize '.$out['width'].'x'.$out['height'];
}
else if ( $out['width'] ) {
$option .= ' -resize '.$out['width'];
}
else if ( $out['height'] ) {
$option .= ' -resize x'.$out['width'];
}
// NOTE: modifiers append to resize strings, so this must come next //
if ( $out['fit'] ) {
// Fit to dimensions //
$option .= "^ -gravity center";
if ( $out['color'] )
$option .= " -background '#".$out['color']."'";
else
$option .= " -background none";
$option .= ' -extent '.$out['width'].'x'.$out['height'];
}
else {
// Don't allow resizing larger than original //
$option .= '\>';
}
}
// Run ImageMagick //
// NOTE: This will fail if there exists a file named "png:-", "webp:-", etc.
$data = do_proc(
'convert - '.$option.' '.$out['format'].':-',
$data
);
}
// Step 3: Optimize
if ( !$out['noopt'] ) {
// if ( ($file_out_ext == 'gif') ) {
// // http://www.lcdf.org/gifsicle/
// EmitErrorAndExit("ERROR: Unsupported optimizer GIF");
// }
// else
if ( ($out['format'] == 'png') ) {
// https://pngquant.org/
// https://pngquant.org/php.html
$file_out_min_quality = 60;
$file_out_max_quality = 95;
$data = do_proc(
'pngquant --quality='.$file_out_min_quality.'-'.$file_out_max_quality.' -',
$data
);
}
// else if ( ($file_out_ext == 'jpg') || ($file_out_ext == 'jpeg') ) {
// EmitErrorAndExit("ERROR: Unsupported optimizer JPEG");
// }
// else if ( ($file_out_ext == 'webp') ) {
// EmitErrorAndExit("ERROR: Unsupported optimizer WEBP");
// }
}
// Debug mode, output JSON instead of an image
if ( $out['debug'] ) {
$RESPONSE['args'] = $in_ext_part;
Emit($RESPONSE);
}
// Image output mode
else {
// Step 4: Write File
if ( !file_exists($out_fullpath) ) {
mkdir($out_fullpath, 0755, true);
}
file_put_contents($out_file, $data);
// Step 5: Redirect to self and exit
//redirectToSelfAndExit();
header("Content-Type: ".IMAGE_MIME[$out['format']]);
header("Content-Length: ".strlen($data));
header('Expires: 0');
echo $data;
exit;
}
}
else {
EmitError(400, "Not an valid transformable content type");
}
}
// No changes
else {
if ( !file_exists($out_fullpath) ) {
mkdir($out_fullpath, 0755, true);
}
symlink($src_relativefile, $out_file);
//redirectToSelfAndExit();
header("Content-Type: ".IMAGE_MIME[$in_ext]);
header("Content-Length: ".filesize($out_file));
header('Expires: 0');
readfile($out_file);
exit;
}