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
|
* Check if the given secret is valid
|
||||||
*/
|
*/
|
||||||
function checkSecret($token): bool
|
function checkSecret($secret): bool
|
||||||
{
|
{
|
||||||
global $uploadSecrets;
|
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 {
|
try {
|
||||||
$token = $_POST['secret']; // The provided secret key
|
$secret = $_POST['secret']; // The secret key
|
||||||
$file = $_FILES['sharex']; // The uploaded file
|
$file = $_FILES['sharex']; // The uploaded file
|
||||||
|
|
||||||
// Check if the token is valid
|
// Check if the token is valid
|
||||||
if (!checkSecret($token)) {
|
if (!checkSecret($secret)) {
|
||||||
returnJson(array(
|
returnJson(array(
|
||||||
'status' => 'ERROR',
|
'status' => 'ERROR',
|
||||||
'url' => 'Invalid or missing upload secret',
|
'url' => 'Invalid or missing upload secret',
|
||||||
@ -94,12 +94,13 @@ try {
|
|||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
$shouldSave = true; // Whether or not the file should be saved
|
|
||||||
$finalName = $target_file; // The final name of the file
|
$finalName = $target_file; // The final name of the file
|
||||||
if ($useRandomFileNames) { // Generate a random file name if enabled
|
if ($useRandomFileNames) { // Generate a random file name if enabled
|
||||||
$finalName = generateRandomString($fileNameLength) . "." . $fileType;
|
$finalName = generateRandomString($fileNameLength) . "." . $fileType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$saved = false; // Whether or not the file was saved
|
||||||
|
|
||||||
// Convert the image to webp if applicable
|
// Convert the image to webp if applicable
|
||||||
if (in_array($fileType, array("png", "jpeg", "jpg")) && $_FILES["sharex"]["size"] > $webpThreadhold && $shouldConvertToWebp) {
|
if (in_array($fileType, array("png", "jpeg", "jpg")) && $_FILES["sharex"]["size"] > $webpThreadhold && $shouldConvertToWebp) {
|
||||||
$image = imagecreatefromstring(file_get_contents($_FILES["sharex"]["tmp_name"]));
|
$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
|
imagewebp($image, $webp_file, $webpQuality); // Convert the image and save it
|
||||||
imagedestroy($image); // Free up memory
|
imagedestroy($image); // Free up memory
|
||||||
$finalName = $webp_file;
|
$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
|
// Move the file to the uploads folder
|
||||||
if (move_uploaded_file($_FILES["sharex"]["tmp_name"], $uploadDir . $finalName)) {
|
$success = move_uploaded_file($_FILES["sharex"]["tmp_name"], $uploadDir . $finalName);
|
||||||
returnJson(array(
|
if (!$success) {
|
||||||
'status' => 'OK',
|
|
||||||
'url' => $finalName,
|
|
||||||
'timeTaken' => getTimeTaken()
|
|
||||||
));
|
|
||||||
die();
|
|
||||||
}
|
|
||||||
returnJson(array(
|
returnJson(array(
|
||||||
'status' => 'ERROR',
|
'status' => 'ERROR',
|
||||||
'url' => 'Failed to save file. Check the permissions of the upload directory.',
|
'url' => 'Failed to save file. Check the permissions of the upload directory.',
|
||||||
@ -127,6 +122,7 @@ try {
|
|||||||
));
|
));
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
returnJson(array(
|
returnJson(array(
|
||||||
'status' => 'OK',
|
'status' => 'OK',
|
||||||
'url' => $finalName,
|
'url' => $finalName,
|
||||||
|
Loading…
Reference in New Issue
Block a user