scratch – Blame information for rev 117

Subversion Repositories:
Rev:
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');
10 require_once('vendor/php-ffmpeg/php-ffmpeg/src/FFMpeg/FFMpeg.php');
11  
12 ### Load configuration.
13 $config = spyc_load_file('config.yaml');
14  
15 ### Open MIME.
16 $finfo = finfo_open(FILEINFO_MIME_TYPE);
17 if (!$finfo) {
18 http_response_code(500);
19 die('Internal server error.');
20 }
21  
22 echo json_encode(
116 office 23 array_values(
24 array_filter(
25 array_map(
26 function ($file) use ($config, $finfo) {
27 #### Get the file hash.
28 $fileHash = array_shift(explode('.', $file));
115 office 29  
116 office 30 #### Build the user path.
31 $userPath = join(
115 office 32 DIRECTORY_SEPARATOR,
33 array(
34 $config['STORE_FOLDER'],
116 office 35 $file
115 office 36 )
37 );
116 office 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']
62 )
63 );
115 office 64  
116 office 65 ### Build the user path.
66 $previewFile = join(
67 DIRECTORY_SEPARATOR,
68 array(
69 $config['STORE_FOLDER'],
70 $previewHash
71 )
72 );
73  
74 ### Do not re-create the thumbnail if it already exists.
75 if (file_exists($previewFile.'.'.'jpg')) {
76 break;
77 }
78  
79 ### Extract thumbnail.
80 $ffmpeg = FFMpeg\FFMpeg::create();
81 $video = $ffmpeg->open($userPath);
82 $frame = $video->frame(
83 FFMpeg\Coordinate\TimeCode::fromSeconds(
84 $config['VIDEO_PREVIEW_IMAGE_FRAME_SECOND']
85 )
86 );
87 $frame->save($previewFile.'.'.'jpg');
88 $opengraph = TRUE;
89 break;
90 case "GIF":
91 $opengraph = TRUE;
92 case "PNG":
93 case "HTML":
94 case "HTM":
95 case "TGA":
96 case "SVG":
97 case "JPEG":
117 office 98 case "JPG":
116 office 99 $previewHash = $fileHash;
100 break;
101 default:
102 return null;
115 office 103 }
116 office 104 return array(
105 'url' => $fileHash,
106 'type' => finfo_file($finfo, $userPath),
107 'preview' => $previewHash,
108 'opengraph' => $opengraph
115 office 109 );
116 office 110 },
111 array_values(
112 array_filter(scandir($config['STORE_FOLDER']),
113 function ($entry) use ($config) {
114 return !is_dir($entry) && filesize(
115 join(
116 DIRECTORY_SEPARATOR,
117 array(
118 $config['STORE_FOLDER'],
119 $entry
120 )
121 )
122 );
123 }
124 )
125 )
115 office 126 )
127 )
128 )
129 );
130  
131 finfo_close($finfo);