scratch – Diff between revs 124 and 139
?pathlinks?
Rev 124 | Rev 139 | |||
---|---|---|---|---|
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 | |
10 | |
|
11 | ### FFMpeg |
11 | ### FFMpeg |
|
12 | require_once('vendor/php-ffmpeg/php-ffmpeg/src/FFMpeg/FFMpeg.php'); |
12 | require_once('vendor/php-ffmpeg/php-ffmpeg/src/FFMpeg/FFMpeg.php'); |
|
13 | |
13 | |
|
14 | ### Load configuration. |
14 | ### Load configuration. |
|
15 | $config = spyc_load_file('config.yaml'); |
15 | $config = spyc_load_file('config.yaml'); |
|
- | 16 | |
||
- | 17 | ### If tags were specified then check whether the hash matches any tags. |
||
- | 18 | $tags = array(); |
||
- | 19 | if(isset($_GET['tags']) && !empty($_GET['tags'])) { |
||
- | 20 | ## Extract the token field values. |
||
- | 21 | $tags = array_filter( |
||
- | 22 | array_map( |
||
- | 23 | function($item) { |
||
- | 24 | return $item->value; |
||
- | 25 | }, |
||
- | 26 | array_values( |
||
- | 27 | json_decode( |
||
- | 28 | stripslashes( |
||
- | 29 | $_GET['tags'] |
||
- | 30 | ) |
||
- | 31 | ) |
||
- | 32 | ) |
||
- | 33 | ) |
||
- | 34 | ); |
||
- | 35 | |
||
- | 36 | ## Connect or create the scratch database. |
||
- | 37 | $db = new PDO('sqlite:db/scratch.sqlite3'); |
||
- | 38 | |
||
- | 39 | ## Set the error mode to exceptions. |
||
- | 40 | $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
- | 41 | |
||
- | 42 | try { |
||
- | 43 | $db->beginTransaction(); |
||
- | 44 | |
||
- | 45 | ## Create tags table if it does not exist. |
||
- | 46 | $db->query('CREATE TABLE IF NOT EXISTS "tags" ("hash" text NOT NULL COLLATE NOCASE, "tag" text COLLATE NOCASE, UNIQUE("hash", "tag") ON CONFLICT REPLACE)'); |
||
- | 47 | |
||
- | 48 | ## Select all the hashes that match supplied tags. |
||
- | 49 | $q = $db->prepare('SELECT "hash" FROM "tags" WHERE tag IN ('.implode(',', array_fill(0, count($tags), '?')).')'); |
||
- | 50 | foreach($tags as $i => $tag) |
||
- | 51 | $q->bindValue($i+1, $tag); |
||
- | 52 | |
||
- | 53 | $result = $q->execute(); |
||
- | 54 | $tags = $q->fetchAll(PDO::FETCH_COLUMN, 0); |
||
- | 55 | $db->commit(); |
||
- | 56 | } catch(Exception $e) { |
||
- | 57 | error_log($e); |
||
- | 58 | ## Rollback. |
||
- | 59 | $db->rollback(); |
||
- | 60 | } |
||
- | 61 | } |
||
16 | |
62 | |
|
17 | ### Open MIME. |
63 | ### Open MIME. |
|
18 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
64 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
|
19 | if (!$finfo) { |
65 | if (!$finfo) { |
|
20 | http_response_code(500); |
66 | http_response_code(500); |
|
21 | die('Internal server error.'); |
67 | die('Internal server error.'); |
|
22 | } |
68 | } |
|
23 | |
69 | |
|
24 | echo json_encode( |
70 | echo json_encode( |
|
25 | array_values( |
71 | array_values( |
|
26 | array_filter( |
72 | array_filter( |
|
27 | array_map( |
73 | array_map( |
|
28 | function ($file) use ($config, $finfo) { |
74 | function ($file) use ($config, $finfo, $tags) { |
|
29 | #### Get the file hash. |
75 | #### Get the file hash. |
|
30 | $fileHash = array_shift(explode('.', $file)); |
76 | $fileHash = array_shift(explode('.', $file)); |
|
31 | |
77 | |
|
32 | #### Build the user path. |
78 | #### Build the user path. |
|
33 | $userPath = join( |
79 | $userPath = join( |
|
34 | DIRECTORY_SEPARATOR, |
80 | DIRECTORY_SEPARATOR, |
|
35 | array( |
81 | array( |
|
36 | $config['STORE_FOLDER'], |
82 | $config['STORE_FOLDER'], |
|
37 | $file |
83 | $file |
|
38 | ) |
84 | ) |
|
39 | ); |
85 | ); |
|
40 | |
86 | |
|
41 | #### If the extension is not allowed then skip this file. |
87 | #### If the extension is not allowed then skip this file. |
|
42 | $fileExtension = pathinfo($userPath, PATHINFO_EXTENSION); |
88 | $fileExtension = pathinfo($userPath, PATHINFO_EXTENSION); |
|
43 | if (!isset($fileExtension) || |
89 | if (!isset($fileExtension) || |
|
44 | !in_array(strtoupper($fileExtension), |
90 | !in_array(strtoupper($fileExtension), |
|
45 | array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) |
91 | array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) |
|
46 | return null; |
92 | return null; |
|
47 | |
93 | |
|
48 | #### Hook for various file extensions. |
94 | #### Hook for various file extensions. |
|
49 | $opengraph = FALSE; |
95 | $opengraph = FALSE; |
|
50 | switch (strtoupper($fileExtension)) { |
96 | switch (strtoupper($fileExtension)) { |
|
51 | case "MP4": |
97 | case "MP4": |
|
52 | ### Generate a hash for the preview image. |
98 | ### Generate a hash for the preview image. |
|
53 | $previewHash = strtolower( |
99 | $previewHash = strtolower( |
|
54 | PseudoCrypt::hash( |
100 | PseudoCrypt::hash( |
|
55 | preg_replace( |
101 | preg_replace( |
|
56 | '/\D/', |
102 | '/\D/', |
|
57 | '', |
103 | '', |
|
58 | hash( |
104 | hash( |
|
59 | 'sha512', |
105 | 'sha512', |
|
60 | $fileHash |
106 | $fileHash |
|
61 | ) |
107 | ) |
|
62 | ), |
108 | ), |
|
63 | $config['ASSET_HASH_SIZE'] |
109 | $config['ASSET_HASH_SIZE'] |
|
64 | ) |
110 | ) |
|
65 | ); |
111 | ); |
|
66 | |
112 | |
|
67 | ### Build the user path. |
113 | ### Build the user path. |
|
68 | $previewFile = join( |
114 | $previewFile = join( |
|
69 | DIRECTORY_SEPARATOR, |
115 | DIRECTORY_SEPARATOR, |
|
70 | array( |
116 | array( |
|
71 | $config['STORE_FOLDER'], |
117 | $config['STORE_FOLDER'], |
|
72 | $previewHash |
118 | $previewHash |
|
73 | ) |
119 | ) |
|
74 | ); |
120 | ); |
|
75 | |
121 | |
|
76 | ### Do not re-create the thumbnail if it already exists. |
122 | ### Do not re-create the thumbnail if it already exists. |
|
77 | if (file_exists($previewFile.'.'.'jpg')) { |
123 | if (file_exists($previewFile.'.'.'jpg')) { |
|
78 | $opengraph = TRUE; |
124 | $opengraph = TRUE; |
|
79 | break; |
125 | break; |
|
80 | } |
126 | } |
|
81 | |
127 | |
|
82 | ### Extract thumbnail. |
128 | ### Extract thumbnail. |
|
83 | $ffmpeg = FFMpeg\FFMpeg::create(); |
129 | $ffmpeg = FFMpeg\FFMpeg::create(); |
|
84 | $video = $ffmpeg->open($userPath); |
130 | $video = $ffmpeg->open($userPath); |
|
85 | $frame = $video->frame( |
131 | $frame = $video->frame( |
|
86 | FFMpeg\Coordinate\TimeCode::fromSeconds( |
132 | FFMpeg\Coordinate\TimeCode::fromSeconds( |
|
87 | $config['VIDEO_PREVIEW_IMAGE_FRAME_SECOND'] |
133 | $config['VIDEO_PREVIEW_IMAGE_FRAME_SECOND'] |
|
88 | ) |
134 | ) |
|
89 | ); |
135 | ); |
|
90 | $frame->save($previewFile.'.'.'jpg'); |
136 | $frame->save($previewFile.'.'.'jpg'); |
|
91 | $opengraph = TRUE; |
137 | $opengraph = TRUE; |
|
92 | break; |
138 | break; |
|
93 | case "GIF": |
139 | case "GIF": |
|
94 | $opengraph = TRUE; |
140 | $opengraph = TRUE; |
|
95 | case "PNG": |
141 | case "PNG": |
|
96 | case "HTML": |
142 | case "HTML": |
|
97 | case "HTM": |
143 | case "HTM": |
|
98 | case "TGA": |
144 | case "TGA": |
|
99 | case "SVG": |
145 | case "SVG": |
|
100 | case "JPEG": |
146 | case "JPEG": |
|
101 | case "JPG": |
147 | case "JPG": |
|
102 | $previewHash = $fileHash; |
148 | $previewHash = $fileHash; |
|
103 | break; |
149 | break; |
|
104 | default: |
150 | default: |
|
105 | return null; |
151 | return null; |
|
106 | } |
152 | } |
|
- | 153 | |
||
- | 154 | if(!empty($tags) && !in_array($fileHash, $tags)) { |
||
- | 155 | return array(); |
||
- | 156 | } |
||
- | 157 | |
||
107 | return array( |
158 | return array( |
|
108 | 'url' => $fileHash, |
159 | 'url' => $fileHash, |
|
109 | 'type' => finfo_file($finfo, $userPath), |
160 | 'type' => finfo_file($finfo, $userPath), |
|
110 | 'preview' => $previewHash, |
161 | 'preview' => $previewHash, |
|
111 | 'opengraph' => $opengraph |
162 | 'opengraph' => $opengraph |
|
112 | ); |
163 | ); |
|
113 | }, |
164 | }, |
|
114 | array_values( |
165 | array_values( |
|
115 | array_filter(scandir($config['STORE_FOLDER']), |
166 | array_filter(scandir($config['STORE_FOLDER']), |
|
116 | function ($entry) use ($config) { |
167 | function ($entry) use ($config) { |
|
117 | return !is_dir($entry) && filesize( |
168 | return !is_dir($entry) && filesize( |
|
118 | join( |
169 | join( |
|
119 | DIRECTORY_SEPARATOR, |
170 | DIRECTORY_SEPARATOR, |
|
120 | array( |
171 | array( |
|
121 | $config['STORE_FOLDER'], |
172 | $config['STORE_FOLDER'], |
|
122 | $entry |
173 | $entry |
|
123 | ) |
174 | ) |
|
124 | ) |
175 | ) |
|
125 | ); |
176 | ); |
|
126 | } |
177 | } |
|
127 | ) |
178 | ) |
|
128 | ) |
179 | ) |
|
129 | ) |
180 | ) |
|
130 | ) |
181 | ) |
|
131 | ) |
182 | ) |
|
132 | ); |
183 | ); |
|
133 | |
184 | |
|
134 | finfo_close($finfo); |
185 | finfo_close($finfo); |
|
135 | |
186 | |
|
- | 187 |
|
||
- | 188 | |
||
- | 189 | |
||
- | 190 | |