scratch – Blame information for rev 124

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