scratch – Blame information for rev 139
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
115 | office | 1 | <?php |
2 | |||
3 | ########################################################################### |
||
4 | ## Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 ## |
||
5 | ########################################################################### |
||
6 | |||
7 | require_once('php/pseudocrypt.php'); |
||
8 | require_once('php/functions.php'); |
||
9 | require_once('vendor/autoload.php'); |
||
124 | office | 10 | |
11 | ### FFMpeg |
||
115 | office | 12 | require_once('vendor/php-ffmpeg/php-ffmpeg/src/FFMpeg/FFMpeg.php'); |
13 | |||
14 | ### Load configuration. |
||
15 | $config = spyc_load_file('config.yaml'); |
||
16 | |||
139 | office | 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 | } |
||
62 | |||
115 | office | 63 | ### Open MIME. |
64 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
||
65 | if (!$finfo) { |
||
66 | http_response_code(500); |
||
67 | die('Internal server error.'); |
||
68 | } |
||
69 | |||
70 | echo json_encode( |
||
116 | office | 71 | array_values( |
72 | array_filter( |
||
73 | array_map( |
||
139 | office | 74 | function ($file) use ($config, $finfo, $tags) { |
116 | office | 75 | #### Get the file hash. |
76 | $fileHash = array_shift(explode('.', $file)); |
||
139 | office | 77 | |
116 | office | 78 | #### Build the user path. |
79 | $userPath = join( |
||
115 | office | 80 | DIRECTORY_SEPARATOR, |
81 | array( |
||
82 | $config['STORE_FOLDER'], |
||
116 | office | 83 | $file |
115 | office | 84 | ) |
85 | ); |
||
139 | office | 86 | |
116 | office | 87 | #### If the extension is not allowed then skip this file. |
88 | $fileExtension = pathinfo($userPath, PATHINFO_EXTENSION); |
||
89 | if (!isset($fileExtension) || |
||
90 | !in_array(strtoupper($fileExtension), |
||
91 | array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) |
||
92 | return null; |
||
139 | office | 93 | |
116 | office | 94 | #### Hook for various file extensions. |
95 | $opengraph = FALSE; |
||
96 | switch (strtoupper($fileExtension)) { |
||
97 | case "MP4": |
||
98 | ### Generate a hash for the preview image. |
||
99 | $previewHash = strtolower( |
||
100 | PseudoCrypt::hash( |
||
101 | preg_replace( |
||
102 | '/\D/', |
||
103 | '', |
||
104 | hash( |
||
105 | 'sha512', |
||
106 | $fileHash |
||
107 | ) |
||
108 | ), |
||
109 | $config['ASSET_HASH_SIZE'] |
||
110 | ) |
||
111 | ); |
||
139 | office | 112 | |
116 | office | 113 | ### Build the user path. |
114 | $previewFile = join( |
||
115 | DIRECTORY_SEPARATOR, |
||
116 | array( |
||
117 | $config['STORE_FOLDER'], |
||
118 | $previewHash |
||
119 | ) |
||
120 | ); |
||
139 | office | 121 | |
116 | office | 122 | ### Do not re-create the thumbnail if it already exists. |
123 | if (file_exists($previewFile.'.'.'jpg')) { |
||
124 | office | 124 | $opengraph = TRUE; |
116 | office | 125 | break; |
126 | } |
||
139 | office | 127 | |
116 | office | 128 | ### Extract thumbnail. |
129 | $ffmpeg = FFMpeg\FFMpeg::create(); |
||
130 | $video = $ffmpeg->open($userPath); |
||
131 | $frame = $video->frame( |
||
132 | FFMpeg\Coordinate\TimeCode::fromSeconds( |
||
133 | $config['VIDEO_PREVIEW_IMAGE_FRAME_SECOND'] |
||
134 | ) |
||
135 | ); |
||
136 | $frame->save($previewFile.'.'.'jpg'); |
||
137 | $opengraph = TRUE; |
||
138 | break; |
||
139 | case "GIF": |
||
140 | $opengraph = TRUE; |
||
141 | case "PNG": |
||
142 | case "HTML": |
||
143 | case "HTM": |
||
144 | case "TGA": |
||
145 | case "SVG": |
||
146 | case "JPEG": |
||
117 | office | 147 | case "JPG": |
116 | office | 148 | $previewHash = $fileHash; |
149 | break; |
||
150 | default: |
||
151 | return null; |
||
115 | office | 152 | } |
139 | office | 153 | |
154 | if(!empty($tags) && !in_array($fileHash, $tags)) { |
||
155 | return array(); |
||
156 | } |
||
157 | |||
116 | office | 158 | return array( |
159 | 'url' => $fileHash, |
||
160 | 'type' => finfo_file($finfo, $userPath), |
||
161 | 'preview' => $previewHash, |
||
162 | 'opengraph' => $opengraph |
||
115 | office | 163 | ); |
116 | office | 164 | }, |
165 | array_values( |
||
166 | array_filter(scandir($config['STORE_FOLDER']), |
||
167 | function ($entry) use ($config) { |
||
168 | return !is_dir($entry) && filesize( |
||
169 | join( |
||
170 | DIRECTORY_SEPARATOR, |
||
171 | array( |
||
172 | $config['STORE_FOLDER'], |
||
173 | $entry |
||
174 | ) |
||
175 | ) |
||
176 | ); |
||
177 | } |
||
178 | ) |
||
179 | ) |
||
115 | office | 180 | ) |
181 | ) |
||
182 | ) |
||
183 | ); |
||
184 | |||
139 | office | 185 | finfo_close($finfo); |