scratch – Diff between revs 116 and 117
?pathlinks?
Rev 116 | Rev 117 | |||
---|---|---|---|---|
1 | <?php |
1 | <?php |
|
2 | |
2 | |
|
3 | ########################################################################### |
3 | ########################################################################### |
|
4 | ## Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 ## |
4 | ## Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 ## |
|
5 | ########################################################################### |
5 | ########################################################################### |
|
6 | |
6 | |
|
7 | require_once('php/pseudocrypt.php'); |
7 | require_once('php/pseudocrypt.php'); |
|
8 | require_once('php/functions.php'); |
8 | require_once('php/functions.php'); |
|
9 | require_once('vendor/autoload.php'); |
9 | require_once('vendor/autoload.php'); |
|
10 | require_once('vendor/php-ffmpeg/php-ffmpeg/src/FFMpeg/FFMpeg.php'); |
10 | require_once('vendor/php-ffmpeg/php-ffmpeg/src/FFMpeg/FFMpeg.php'); |
|
11 | |
11 | |
|
12 | ### Load configuration. |
12 | ### Load configuration. |
|
13 | $config = spyc_load_file('config.yaml'); |
13 | $config = spyc_load_file('config.yaml'); |
|
14 | |
14 | |
|
15 | ### Open MIME. |
15 | ### Open MIME. |
|
16 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
16 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
|
17 | if (!$finfo) { |
17 | if (!$finfo) { |
|
18 | http_response_code(500); |
18 | http_response_code(500); |
|
19 | die('Internal server error.'); |
19 | die('Internal server error.'); |
|
20 | } |
20 | } |
|
21 | |
21 | |
|
22 | echo json_encode( |
22 | echo json_encode( |
|
23 | array_values( |
23 | array_values( |
|
24 | array_filter( |
24 | array_filter( |
|
25 | array_map( |
25 | array_map( |
|
26 | function ($file) use ($config, $finfo) { |
26 | function ($file) use ($config, $finfo) { |
|
27 | #### Get the file hash. |
27 | #### Get the file hash. |
|
28 | $fileHash = array_shift(explode('.', $file)); |
28 | $fileHash = array_shift(explode('.', $file)); |
|
29 | |
29 | |
|
30 | #### Build the user path. |
30 | #### Build the user path. |
|
31 | $userPath = join( |
31 | $userPath = join( |
|
32 | DIRECTORY_SEPARATOR, |
32 | DIRECTORY_SEPARATOR, |
|
33 | array( |
33 | array( |
|
34 | $config['STORE_FOLDER'], |
34 | $config['STORE_FOLDER'], |
|
35 | $file |
35 | $file |
|
36 | ) |
36 | ) |
|
37 | ); |
37 | ); |
|
38 | |
38 | |
|
39 | #### If the extension is not allowed then skip this file. |
39 | #### If the extension is not allowed then skip this file. |
|
40 | $fileExtension = pathinfo($userPath, PATHINFO_EXTENSION); |
40 | $fileExtension = pathinfo($userPath, PATHINFO_EXTENSION); |
|
41 | if (!isset($fileExtension) || |
41 | if (!isset($fileExtension) || |
|
42 | !in_array(strtoupper($fileExtension), |
42 | !in_array(strtoupper($fileExtension), |
|
43 | array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) |
43 | array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) |
|
44 | return null; |
44 | return null; |
|
45 | |
45 | |
|
46 | #### Hook for various file extensions. |
46 | #### Hook for various file extensions. |
|
47 | $opengraph = FALSE; |
47 | $opengraph = FALSE; |
|
48 | switch (strtoupper($fileExtension)) { |
48 | switch (strtoupper($fileExtension)) { |
|
49 | case "MP4": |
49 | case "MP4": |
|
50 | ### Generate a hash for the preview image. |
50 | ### Generate a hash for the preview image. |
|
51 | $previewHash = strtolower( |
51 | $previewHash = strtolower( |
|
52 | PseudoCrypt::hash( |
52 | PseudoCrypt::hash( |
|
53 | preg_replace( |
53 | preg_replace( |
|
54 | '/\D/', |
54 | '/\D/', |
|
55 | '', |
55 | '', |
|
56 | hash( |
56 | hash( |
|
57 | 'sha512', |
57 | 'sha512', |
|
58 | $fileHash |
58 | $fileHash |
|
59 | ) |
59 | ) |
|
60 | ), |
60 | ), |
|
61 | $config['ASSET_HASH_SIZE'] |
61 | $config['ASSET_HASH_SIZE'] |
|
62 | ) |
62 | ) |
|
63 | ); |
63 | ); |
|
64 | |
64 | |
|
65 | ### Build the user path. |
65 | ### Build the user path. |
|
66 | $previewFile = join( |
66 | $previewFile = join( |
|
67 | DIRECTORY_SEPARATOR, |
67 | DIRECTORY_SEPARATOR, |
|
68 | array( |
68 | array( |
|
69 | $config['STORE_FOLDER'], |
69 | $config['STORE_FOLDER'], |
|
70 | $previewHash |
70 | $previewHash |
|
71 | ) |
71 | ) |
|
72 | ); |
72 | ); |
|
73 | |
73 | |
|
74 | ### Do not re-create the thumbnail if it already exists. |
74 | ### Do not re-create the thumbnail if it already exists. |
|
75 | if (file_exists($previewFile.'.'.'jpg')) { |
75 | if (file_exists($previewFile.'.'.'jpg')) { |
|
76 | break; |
76 | break; |
|
77 | } |
77 | } |
|
78 | |
78 | |
|
79 | ### Extract thumbnail. |
79 | ### Extract thumbnail. |
|
80 | $ffmpeg = FFMpeg\FFMpeg::create(); |
80 | $ffmpeg = FFMpeg\FFMpeg::create(); |
|
81 | $video = $ffmpeg->open($userPath); |
81 | $video = $ffmpeg->open($userPath); |
|
82 | $frame = $video->frame( |
82 | $frame = $video->frame( |
|
83 | FFMpeg\Coordinate\TimeCode::fromSeconds( |
83 | FFMpeg\Coordinate\TimeCode::fromSeconds( |
|
84 | $config['VIDEO_PREVIEW_IMAGE_FRAME_SECOND'] |
84 | $config['VIDEO_PREVIEW_IMAGE_FRAME_SECOND'] |
|
85 | ) |
85 | ) |
|
86 | ); |
86 | ); |
|
87 | $frame->save($previewFile.'.'.'jpg'); |
87 | $frame->save($previewFile.'.'.'jpg'); |
|
88 | $opengraph = TRUE; |
88 | $opengraph = TRUE; |
|
89 | break; |
89 | break; |
|
90 | case "GIF": |
90 | case "GIF": |
|
91 | $opengraph = TRUE; |
91 | $opengraph = TRUE; |
|
92 | case "PNG": |
92 | case "PNG": |
|
93 | case "HTML": |
93 | case "HTML": |
|
94 | case "HTM": |
94 | case "HTM": |
|
95 | case "TGA": |
95 | case "TGA": |
|
96 | case "SVG": |
96 | case "SVG": |
|
97 | case "JPEG": |
97 | case "JPEG": |
|
- | 98 | case "JPG": |
||
98 | $previewHash = $fileHash; |
99 | $previewHash = $fileHash; |
|
99 | break; |
100 | break; |
|
100 | default: |
101 | default: |
|
101 | return null; |
102 | return null; |
|
102 | } |
103 | } |
|
103 | return array( |
104 | return array( |
|
104 | 'url' => $fileHash, |
105 | 'url' => $fileHash, |
|
105 | 'type' => finfo_file($finfo, $userPath), |
106 | 'type' => finfo_file($finfo, $userPath), |
|
106 | 'preview' => $previewHash, |
107 | 'preview' => $previewHash, |
|
107 | 'opengraph' => $opengraph |
108 | 'opengraph' => $opengraph |
|
108 | ); |
109 | ); |
|
109 | }, |
110 | }, |
|
110 | array_values( |
111 | array_values( |
|
111 | array_filter(scandir($config['STORE_FOLDER']), |
112 | array_filter(scandir($config['STORE_FOLDER']), |
|
112 | function ($entry) use ($config) { |
113 | function ($entry) use ($config) { |
|
113 | return !is_dir($entry) && filesize( |
114 | return !is_dir($entry) && filesize( |
|
114 | join( |
115 | join( |
|
115 | DIRECTORY_SEPARATOR, |
116 | DIRECTORY_SEPARATOR, |
|
116 | array( |
117 | array( |
|
117 | $config['STORE_FOLDER'], |
118 | $config['STORE_FOLDER'], |
|
118 | $entry |
119 | $entry |
|
119 | ) |
120 | ) |
|
120 | ) |
121 | ) |
|
121 | ); |
122 | ); |
|
122 | } |
123 | } |
|
123 | ) |
124 | ) |
|
124 | ) |
125 | ) |
|
125 | ) |
126 | ) |
|
126 | ) |
127 | ) |
|
127 | ) |
128 | ) |
|
128 | ); |
129 | ); |
|
129 | |
130 | |
|
130 | finfo_close($finfo); |
131 | finfo_close($finfo); |
|
131 | |
132 | |