Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit 2aefa02

Browse files
committed
Clean up some code
1 parent af60fd8 commit 2aefa02

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

src/com/carlgo11/tempfiles/File.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
/**
99
* File class
10+
* Stores all file-related data during a file-upload/-download operation.
11+
* This data is NOT encrypted. See {@see EncryptedFile} for the encrypted equivalent.
1012
*
1113
* @package com\carlgo11\tempfiles
1214
* @since 2.2
@@ -165,7 +167,7 @@ public function setDeletionPassword(string $deletionPassword) {
165167
/**
166168
* Gets the metadata of the file if supplied.
167169
*
168-
* @param string $type Array key of the desired value.
170+
* @param string|null $type Array key of the desired value.
169171
* @return string|array Returns data of the desired array key if a $type is supplied, otherwise the entire array.
170172
* @since 2.2
171173
*/

src/com/carlgo11/tempfiles/api/Upload.php

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,39 @@ function __construct(string $method) {
1818
global $conf;
1919
try {
2020
if ($method !== 'POST') throw new Exception("Bad method. Use POST.");
21+
if (!isset($_FILES['file']) || $_FILES['file'] === NULL) throw new Exception("No file uploaded.");
2122

22-
if (isset($_FILES['file']) && $_FILES['file'] !== NULL) {
23-
$fileArray = $_FILES['file'];
24-
$file = new File($fileArray);
23+
$fileArray = $_FILES['file'];
24+
$file = new File($fileArray);
2525

26-
if (Misc::getVar('maxviews') !== NULL) $file->setMaxViews(Misc::getVar('maxviews'));
27-
if (Misc::getVar('password') !== NULL) $password = Misc::getVar('password');
28-
else $password = Misc::generatePassword(6, 20);
29-
30-
$file->setDeletionPassword(Misc::generatePassword(12, 32));
31-
32-
$file->setMetaData([
33-
'size' => $fileArray['size'],
34-
'name' => rawurlencode($fileArray['name']),
35-
'type' => $fileArray['type']
36-
]);
37-
38-
$file->setContent(file_get_contents($fileArray['tmp_name']));
26+
if (Misc::getVar('maxviews') !== NULL) {
27+
$file->setMaxViews(Misc::getVar('maxviews'));
28+
$output['maxviews'] = (int)$file->getMaxViews();
29+
}
3930

40-
DataStorage::saveFile($file, $password);
31+
if (Misc::getVar('password') !== NULL) $password = Misc::getVar('password');
32+
else {
33+
$password = Misc::generatePassword(6, 20);
34+
$output['password'] = $password;
35+
}
4136

42-
// Full URI to download the file
43-
$completeURL = sprintf($conf['download-url'], $file->getID(), $password);
37+
$file->setDeletionPassword(Misc::generatePassword(12, 32));
38+
$file->setContent(file_get_contents($fileArray['tmp_name']));
39+
$file->setMetaData([
40+
'size' => $fileArray['size'],
41+
'name' => rawurlencode($fileArray['name']),
42+
'type' => $fileArray['type']
43+
]);
4444

45-
$output = [
46-
'url' => $completeURL,
47-
'id' => $file->getID(),
48-
'deletepassword' => $file->getDeletionPassword()];
45+
if (!DataStorage::saveFile($file, $password)) throw new Exception("File-storing failed.");
4946

50-
if (Misc::getVar('password') === NULL) $output['password'] = $password;
51-
if ($file->getMaxViews() !== NULL) $output['maxviews'] = (int)$file->getMaxViews();
47+
$output = [
48+
'url' => sprintf($conf['download-url'], $file->getID(), $password),
49+
'id' => $file->getID(),
50+
'deletepassword' => $file->getDeletionPassword()];
5251

53-
syslog(LOG_INFO, $output['id'] . " created.");
54-
return parent::outputJSON($output, 201);
55-
}
52+
syslog(LOG_INFO, $output['id'] . " created.");
53+
return parent::outputJSON($output, 201);
5654
} catch (Exception $e) {
5755
parent::outputJSON(['error' => $e->getMessage()], 500);
5856
}

0 commit comments

Comments
 (0)