scratch – Diff between revs 124 and 139
?pathlinks?
Rev 124 | Rev 139 | |||
---|---|---|---|---|
Line 12... | Line 12... | |||
12 | require_once('vendor/php-ffmpeg/php-ffmpeg/src/FFMpeg/FFMpeg.php'); |
12 | require_once('vendor/php-ffmpeg/php-ffmpeg/src/FFMpeg/FFMpeg.php'); |
|
Line 13... | Line 13... | |||
13 | |
13 | |
|
14 | ### Load configuration. |
14 | ### Load configuration. |
|
Line -... | Line 15... | |||
- | 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 | } |
||
15 | $config = spyc_load_file('config.yaml'); |
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) { |
|
Line 23... | Line 69... | |||
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": |
|
Line 61... | Line 107... | |||
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( |
|
Line 102... | Line 148... | |||
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 |
|
Line 129... | Line 180... | |||
129 | ) |
180 | ) |
|
130 | ) |
181 | ) |
|
131 | ) |
182 | ) |
|
132 | ); |
183 | ); |
|
Line 133... | Line 184... | |||
133 | |
184 | |
|
134 | finfo_close($finfo); |
185 | finfo_close($finfo); |