827
|
1 #!/usr/bin/php
|
|
2 <?
|
|
3 require_once "mconfig.inc.php";
|
|
4 require_once "msite.inc.php";
|
|
5
|
|
6 stCheckCLIExec();
|
|
7
|
|
8
|
882
|
9 function wtExecOrDie($exe, $args)
|
|
10 {
|
|
11 echo "EXEC: ".$exe." ".$args."\n";
|
|
12 //passthru($exe." ".$args) == 0 or die("Error executing ".$exe.":\n".$args);
|
|
13 }
|
|
14
|
|
15
|
|
16 function wtConvertImage($inFilename, $inFileType, $outFilename, $setDim, $setFormat, $setQuality, $thumb)
|
|
17 {
|
|
18 global $setPreviewPath;
|
|
19
|
|
20 if (($outDim = stGetSetting($setDim)) === FALSE ||
|
|
21 ($outFormat = stGetSetting($osetFormat)) === FALSE ||
|
|
22 ($outQuality = stGetSetting($setQuality)) === FALSE)
|
|
23 {
|
|
24 die("Missing one of res/format/quality settings for '".$outFilename."'\n");
|
|
25 }
|
|
26
|
884
|
27 if ($inFileType == "gfx")
|
|
28 {
|
|
29 // Oh great .. we need gfxconv here because Imagick handles ILBM like shit
|
|
30 $filename = tempnam($setPreviewPath, "tmp");
|
|
31 wtExecOrDie(
|
|
32 "/usr/local/bin/gfxconv",
|
|
33 escapeshellarg($inFilename)." -f png -o ".escapeshellarg($filename));
|
|
34 }
|
|
35 else
|
|
36 $filename = $inFilename;
|
|
37
|
882
|
38 // convert -resize 640x480 -background black -gravity center -extent 640x480
|
|
39 // -unsharp "0x0.75+0.75+0.008" lol.lbm -quality 95 test.jpg
|
884
|
40
|
|
41 // Create conversion entity
|
|
42 $img = new Imagick($filename);
|
|
43 if ($img === false)
|
|
44 die("Oh noes! ImageMagick could not digest the file '".$filename."' (".$inFilename.")\n");
|
|
45
|
|
46 // Get dimensions, setup background
|
|
47 $dim = $img->getImageGeometry();
|
|
48 $img->setBackgroundColor(imagick::COLOR_BLACK);
|
|
49 $img->setGravity(imagick::GRAVITY_CENTER);
|
|
50
|
|
51 // Act based on image size vs. desired size and $thumb mode
|
|
52 if ($thumb || $dim["width"] > $outDim[0] || $dim["height"] > $outDim[1])
|
|
53 {
|
|
54 // Image is larger
|
|
55 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_QUADRATIC, 1);
|
|
56 $img->setExtent($outDim[0], $outDim[1]);
|
|
57 $img->normalizeImage();
|
|
58 $img->unsharpMaskImage(0, 0.5, 1, 0.05);
|
|
59 }
|
|
60 else
|
|
61 if ($dim["width"] < $outDim[0] || $dim["height"] < $outDim[1])
|
|
62 {
|
|
63 // Image is smaller than requested dimension(s)?
|
|
64 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_POINT, 1);
|
|
65 $img->setExtent($outDim[0], $outDim[1]);
|
|
66 }
|
|
67
|
|
68 $img->setFormat($outFormat);
|
|
69 $img->setCompressionQuality($outQuality);
|
|
70
|
|
71 $img->stripImage();
|
|
72 $img->writeImage($outFilename);
|
|
73 $img->removeImage();
|
882
|
74 }
|
|
75
|
|
76
|
|
77 function wtRenderSample()
|
|
78 {
|
|
79 $sfreq = intval(stGetSetting("sampleFreq", 44100));
|
|
80 $sduration = intval(stGetSetting("sampleDuration", 30));
|
|
81 $schannels = intval(stGetSetting("sampleChannels", 1));
|
|
82
|
884
|
83 wtExecOrDie(
|
|
84 "/usr/local/bin/openmpt123",
|
|
85
|
|
86 "--quiet --render ".
|
|
87 "--samplerate ".$sfreq." ".
|
|
88 "--channels ".$schannels." ".
|
|
89 "--playtime ".$sduration." ".
|
|
90 escapeshellarg($filename)." -o ".escapeshellarg($sampleFile));
|
882
|
91 }
|
|
92
|
|
93
|
|
94 function wtConvertSample()
|
|
95 {
|
|
96 $sfreq = intval(stGetSetting("sampleFreq", 44100));
|
|
97 $sduration = intval(stGetSetting("sampleDuration", 30));
|
|
98 $schannels = intval(stGetSetting("sampleChannels", 1));
|
|
99
|
884
|
100 // Convert to desired formats
|
|
101 // avconv -t 35 -i xxx_sample.wav -f ogg -c:a vorbis -b:a 95 -ac 1 -map_metadata -1 xxx_sample.ogg
|
|
102 $exe = "/usr/local/bin/avconv";
|
|
103 /*
|
|
104 foreach (...)
|
|
105 {
|
|
106 $args =
|
|
107 "-y -t ".intval($sduration)." ".
|
|
108 "-i ".escapeshellarg($filename)." ".
|
|
109
|
|
110 "-f ".$format." -c:a ".$codec." ".$qualityopt." ".$qualityvalue." ".
|
|
111 "-ac ".$schannels." ".
|
|
112 "-map_metadata -1 ".
|
|
113 escapeshellarg($outFilename);
|
|
114 }
|
|
115 */
|
882
|
116 }
|
|
117
|
|
118
|
|
119 function wtPurgeDir($path)
|
|
120 {
|
|
121 if (file_exists($path))
|
|
122 wtExecOrDie("/bin/echo", "-fR ".escapeshellarg($path));
|
|
123 }
|
|
124
|
|
125
|
|
126 function wtScanArchive($compo, $efile, $filename)
|
|
127 {
|
|
128 global $setEntryPath;
|
|
129
|
|
130 $path = stMakePath(FALSE, FALSE, array($setEntryPath, "UNPACKS", $filename));
|
|
131
|
|
132 echo "Attempting to scan archive file '".$filename."' ...\n";
|
|
133
|
|
134 // Check file type before doing anything
|
|
135 switch ($efile["id"])
|
|
136 {
|
|
137 case "LHA":
|
|
138 $exe = "/usr/bin/lha";
|
|
139 $args = "e ".escapeshellarg($filename);
|
|
140 break;
|
|
141
|
|
142 case "ZIP":
|
|
143 $exe = "/usr/bin/unzip";
|
|
144 $args = "-d ".escapeshellarg($path)." ".escapeshellarg($filename);
|
|
145 break;
|
|
146
|
|
147 default:
|
|
148 die("Unsupported archive file type: ".$efile["id"]."\n");
|
|
149 }
|
|
150
|
|
151 // Create temporary directory
|
|
152 wtPurgeDir($path);
|
|
153 stMakeDir($path, 0700);
|
|
154
|
|
155 if (!is_dir($path) || chdir($path) === false)
|
|
156 die("Failed to chdir to ".$path."\n");
|
|
157
|
|
158 // Unpack archive
|
|
159 wtExecOrDie($exe, $args);
|
|
160
|
|
161 // Scan through files ...
|
|
162 $dir = opendir($path);
|
|
163 while (($dentry = readdir($dir)) !== false)
|
|
164 {
|
|
165 }
|
|
166 closedir($dir);
|
|
167
|
|
168 wtPurgeDir($path);
|
|
169 }
|
|
170
|
|
171
|
|
172 function wtHandleEntryPreview($compo, $entry, $mode)
|
|
173 {
|
|
174 /*
|
|
175 if (previewFile does not exist || previewFile older than entryFile)
|
|
176 {
|
|
177 if (entry is module file)
|
|
178 {
|
|
179 render sample
|
|
180 }
|
|
181 else
|
|
182 if (entry is image file)
|
|
183 {
|
|
184 convert image
|
|
185 }
|
|
186 }
|
|
187
|
|
188 // Based on compo / entry preview type ..
|
|
189
|
|
190 // Convert preview image file, if any
|
|
191 // Create thumbnail for it, too
|
|
192 wtConvertImage($inFilename, $outFilename,
|
|
193 "previewImageSize", "previewImageType",
|
|
194 "previewImageQuality", FALSE);
|
|
195
|
|
196 wtConvertImage($inFilename, $outFilename,
|
|
197 "previewThumbSize", "previewThumbType",
|
|
198 "previewThumbQuality", TRUE);
|
|
199 */
|
|
200 }
|
|
201
|
|
202
|
884
|
203 function wtHandleEntry($compo, $entry, $mode)
|
|
204 {
|
|
205 }
|
|
206
|
|
207
|
827
|
208 //
|
|
209 // Create directories
|
|
210 //
|
|
211 function stMakeDir($path, $perm)
|
|
212 {
|
|
213 if (!file_exists($path))
|
|
214 {
|
|
215 echo " - Creating ".$path."\n";
|
|
216 if (mkdir($path, $perm, TRUE) === false)
|
|
217 die("Could not create directory '".$path."'\n");
|
|
218 }
|
|
219 }
|
|
220
|
|
221
|
|
222 function stInitializeDirs()
|
|
223 {
|
|
224 global $setEntryPath, $setPreviewPath, $setThumbDir,
|
|
225 $setEntryPathPerms, $setPrevPathPerms;
|
|
226
|
|
227 echo "Checking for missing directories ...\n";
|
|
228 stMakeDir($setEntryPath, $setEntryPathPerms);
|
|
229 stMakeDir($setPreviewPath, $setPrevPathPerms);
|
880
|
230 stMakeDir(stMakePath(FALSE, FALSE, array($setPreviewPath, $setThumbDir)), $setPrevPathPerms);
|
827
|
231
|
|
232 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo)
|
|
233 {
|
|
234 stMakeDir(stMakePath(FALSE, FALSE, array($setEntryPath, $compo["cpath"])), $setEntryPathPerms);
|
|
235 }
|
|
236 }
|
|
237
|
|
238
|
|
239 //
|
|
240 // Main program starts
|
|
241 //
|
|
242 if ($argc < 2)
|
|
243 {
|
|
244 echo
|
|
245 "faptool - Do stuff with FAPWeb database\n".
|
|
246 "(C) Copyright 2014 ccr/TNSP\n".
|
|
247 "\n".
|
|
248 "Usage: ".$argv[0]." <mode> [args]\n".
|
|
249 "Where mode is one of following:\n".
|
|
250 "\n".
|
|
251 " init\n".
|
|
252 " Create directories for entries and previews, if needed.\n".
|
|
253 "\n".
|
881
|
254 " previews <cmd> [args]\n".
|
|
255 " Where <cmd> is one of:\n".
|
|
256 " status - List files and show what is missing, etc.\n".
|
827
|
257 "\n";
|
|
258 exit;
|
|
259 }
|
|
260
|
|
261 // Try to connect to database
|
|
262 $spec = stGetSetting("sqlDB");
|
|
263 if (($db = stConnectSQLDBSpec($spec)) === false)
|
|
264 die("Could not connect to SQL database '".$spec."'.\n");
|
|
265
|
|
266 echo "Using database spec '".$spec."'.\n";
|
|
267
|
|
268 // Fetch non-"hardcoded" settings from SQL database
|
|
269 stReloadSettings();
|
|
270
|
|
271 // Set some globals for our benefit
|
|
272 $setEntryPath = stGetSetting("entryPath");
|
|
273 $setPreviewPath = stGetSetting("previewPath");
|
|
274 $setPreviewURL = stGetSetting("previewURL");
|
|
275 $setThumbDir = stGetSetting("thumbnailSubDir");
|
832
|
276 $setEntryPathPerms = stGetSetting("entryPathPerms");
|
|
277 $setPrevPathPerms = stGetSetting("previewPathPerms");
|
827
|
278
|
|
279 if ($setEntryPath === FALSE || $setPreviewPath === FALSE ||
|
832
|
280 $setPreviewURL === FALSE || $setThumbDir === FALSE ||
|
|
281 $setEntryPathPerms === FALSE || $setPrevPathPerms === FALSE)
|
827
|
282 {
|
831
|
283 die("Some required settings not defined in mconfig.inc.php!\n");
|
827
|
284 }
|
|
285
|
|
286
|
|
287 // Act according to specified command
|
884
|
288 switch (substr(stCArgLC(1), 0, 3))
|
827
|
289 {
|
884
|
290 case "ini":
|
827
|
291 //
|
|
292 // Initialize the data directories etc
|
|
293 //
|
|
294 stInitializeDirs();
|
|
295 break;
|
|
296
|
884
|
297 case "pre":
|
881
|
298 //
|
|
299 // Preview files handling
|
|
300 //
|
|
301 $mode = substr(stCArgLC(2), 0, 3);
|
|
302 switch ($mode)
|
|
303 {
|
|
304 case "sta":
|
|
305 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo)
|
|
306 {
|
|
307 printf(
|
|
308 "\n".
|
|
309 "--[ #%03d - %-30s ]---\n".
|
|
310 "PrevType : %s\n".
|
884
|
311 "PrevPath : %s\n",
|
|
312 $compo["id"], substr($compo["name"], 0, 30),
|
881
|
313 $previewTypeList[$compo["preview_type"]][0],
|
|
314 stMakePath(FALSE, FALSE, array($setPreviewPath, $compo["cpath"])));
|
|
315
|
|
316 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry)
|
|
317 wtHandleEntryPreview($compo, $entry, $mode);
|
|
318 }
|
|
319 break;
|
|
320
|
|
321 default:
|
|
322 if ($mode == "")
|
|
323 die("ERROR! Previews command requires a sub-command argument.\n");
|
|
324 else
|
|
325 die("ERROR! Invalid previews sub-command '".stCArg(2)."'.\n");
|
|
326 break;
|
|
327 }
|
|
328 break;
|
|
329
|
827
|
330 default:
|
|
331 echo "ERROR! Invalid operation mode '".stCArg(1)."'.\n";
|
|
332 break;
|
|
333 }
|
|
334
|
|
335 ?> |