cleanup script
This commit is contained in:
parent
ede6fa0a16
commit
1094885d0d
26
upload.php
26
upload.php
@ -17,10 +17,10 @@ $webpThreadhold = 1048576; // 1MB - The minimum file size for converting to webp
|
||||
/**
|
||||
* Check if the given secret is valid
|
||||
*/
|
||||
function checkSecret($token): bool
|
||||
function checkSecret($secret): bool
|
||||
{
|
||||
global $uploadSecrets;
|
||||
return isset($token) && in_array($token, $uploadSecrets);
|
||||
return isset($secret) && in_array($secret, $uploadSecrets);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -56,11 +56,11 @@ function returnJson($data): void
|
||||
}
|
||||
|
||||
try {
|
||||
$token = $_POST['secret']; // The provided secret key
|
||||
$secret = $_POST['secret']; // The secret key
|
||||
$file = $_FILES['sharex']; // The uploaded file
|
||||
|
||||
// Check if the token is valid
|
||||
if (!checkSecret($token)) {
|
||||
if (!checkSecret($secret)) {
|
||||
returnJson(array(
|
||||
'status' => 'ERROR',
|
||||
'url' => 'Invalid or missing upload secret',
|
||||
@ -94,12 +94,13 @@ try {
|
||||
die();
|
||||
}
|
||||
|
||||
$shouldSave = true; // Whether or not the file should be saved
|
||||
$finalName = $target_file; // The final name of the file
|
||||
if ($useRandomFileNames) { // Generate a random file name if enabled
|
||||
$finalName = generateRandomString($fileNameLength) . "." . $fileType;
|
||||
}
|
||||
|
||||
$saved = false; // Whether or not the file was saved
|
||||
|
||||
// Convert the image to webp if applicable
|
||||
if (in_array($fileType, array("png", "jpeg", "jpg")) && $_FILES["sharex"]["size"] > $webpThreadhold && $shouldConvertToWebp) {
|
||||
$image = imagecreatefromstring(file_get_contents($_FILES["sharex"]["tmp_name"]));
|
||||
@ -107,19 +108,13 @@ try {
|
||||
imagewebp($image, $webp_file, $webpQuality); // Convert the image and save it
|
||||
imagedestroy($image); // Free up memory
|
||||
$finalName = $webp_file;
|
||||
$shouldSave = false;
|
||||
$saved = true;
|
||||
}
|
||||
|
||||
if ($shouldSave) {
|
||||
if (!$saved) { // If the file wasn't saved (e.g. webp conversion)
|
||||
// Move the file to the uploads folder
|
||||
if (move_uploaded_file($_FILES["sharex"]["tmp_name"], $uploadDir . $finalName)) {
|
||||
returnJson(array(
|
||||
'status' => 'OK',
|
||||
'url' => $finalName,
|
||||
'timeTaken' => getTimeTaken()
|
||||
));
|
||||
die();
|
||||
}
|
||||
$success = move_uploaded_file($_FILES["sharex"]["tmp_name"], $uploadDir . $finalName);
|
||||
if (!$success) {
|
||||
returnJson(array(
|
||||
'status' => 'ERROR',
|
||||
'url' => 'Failed to save file. Check the permissions of the upload directory.',
|
||||
@ -127,6 +122,7 @@ try {
|
||||
));
|
||||
die();
|
||||
}
|
||||
}
|
||||
returnJson(array(
|
||||
'status' => 'OK',
|
||||
'url' => $finalName,
|
||||
|
Loading…
Reference in New Issue
Block a user