-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.php
108 lines (96 loc) · 3.26 KB
/
upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
use Flow\Basic;
use Flow\Config;
use Flow\Request;
use submitShow\Database;
use submitShow\Recording;
require_once 'processing/requireAuth.php';
require_once 'vendor/autoload.php';
require_once 'processing/Database.php';
require_once 'processing/Recording.php';
require_once 'processing/Input.php';
require_once 'processing/Storage.php';
$config = require 'processing/config.php';
$database = new Database();
$recording = new Recording();
// prepare the input from the form
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = Input::sanitise($_POST["name"]);
$presenter = Input::sanitise($_POST["presenter"]);
$date = Input::sanitise($_POST["date"]);
} else if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$name = Input::sanitise($_GET["name"]);
$presenter = Input::sanitise($_GET["presenter"]);
$date = Input::sanitise($_GET["date"]);
} else {
// Invalid request type.
http_response_code(406);
exit();
}
try {
$recording->setName($name);
$recording->setPresenter($presenter);
$recording->setStart($date);
} catch (Exception $exception) {
error_log("Invalid show metadata: " . $exception->getMessage());
http_response_code(406);
exit;
}
$flowConfig = new Config();
$flowConfig->setTempDir($config["tempDirectory"]);
$request = new Request();
try {
$recording->setFileExtension($request->getFileName());
$extension = $recording->getExtension();
} catch (Exception $exception) {
error_log($exception->getMessage());
http_response_code(500);
exit;
}
if ($extension !== "mp3" &&
$extension !== "m4a" &&
$extension !== "mp4" &&
$extension !== "aac") {
// cancel upload
http_response_code(406);
exit;
} else if ($request->getTotalSize() > $config["maxShowFileSize"]) { // if the file is too large
// cancel upload
http_response_code(406);
exit;
}
try {
$fileName = $recording->getFileName();
} catch (Exception $exception) {
error_log($exception->getMessage());
http_response_code(500);
exit;
}
// set the path to upload to
$uploadPath = $config["holdingDirectory"] . "/" . $fileName;
if (!Storage::createParentDirectories($uploadPath)) {
error_log("Failed to create parent directories for " . $uploadPath);
http_response_code(500);
exit;
}
// If this is the final chunk of the file
if (Basic::save($uploadPath, $flowConfig, $request)) {
try {
// Write metadata about the show into the file
$removingMetadataLocation = $uploadPath . "-meta." . $extension;
shell_exec("ffmpeg -i \"$uploadPath\" -map_metadata -1 -metadata title=\"" . $recording->getName() . " " . $recording->get6DigitStartDate() . "\" -metadata artist=\"" . $recording->getPresenter() . "\" -c:v copy -c:a copy \"$removingMetadataLocation\"");
// move metadata-removed file back to the upload path
rename($removingMetadataLocation, $uploadPath);
} catch (Exception $e) {
error_log($e->getMessage());
http_response_code(500);
exit;
}
// Log upload completed
try {
if (!empty($_SESSION['samlNameId']))
$database->log($_SESSION['samlNameId'], "upload", $fileName);
} catch (Exception $e) {
error_log("Failed to log file upload action from " . $_SESSION["samlNameId"] . ".");
}
}