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