diff --git a/upload.php b/upload.php index 0e60b92..23b93ae 100644 --- a/upload.php +++ b/upload.php @@ -1,8 +1,12 @@ $status, 'url' => $message, 'processingTime' => round($timeTaken, 2) . "ms"); + $json = array('status' => $status, 'url' => $message, 'processingTime' => round($timeTaken ?? 0, 2) . "ms"); echo(json_encode($json)); die(); } -$token = $_POST['secret']; // The provided secret key -$file = $_FILES['sharex']; // The uploaded file +try { + $token = $_POST['secret']; // The provided secret key + $file = $_FILES['sharex']; // The uploaded file -// Check if the token is valid -if (!checkToken($token)) { - $timeTaken = microtime(true) - $before; - returnJson('ERROR', 'Invalid or missing secret key', $timeTaken); - die(); -} - -// Check if the file was uploaded -if (!isset($file)) { - $timeTaken = microtime(true) - $before; - returnJson('ERROR', 'No file was uploaded', $timeTaken); - die(); -} - -$target_file = $_FILES["sharex"]["name"]; // File name -$fileType = pathinfo($target_file, PATHINFO_EXTENSION); // File extension (e.g. png, jpg, etc.) - -// Check if the file already exists -if (file_exists($uploadDir . $target_file)) { - $timeTaken = microtime(true) - $before; - returnJson('ERROR', 'File already exists', $timeTaken); - 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; -} - -// Convert the image to webp if applicable -if (in_array($fileType, array("png", "jpeg", "jpg"))) { - $image = imagecreatefromstring(file_get_contents($_FILES["sharex"]["tmp_name"])); - $webp_file = pathinfo($finalName, PATHINFO_FILENAME) . ".webp"; - imagewebp($image, $webp_file, 90); - imagedestroy($image); - $finalName = $webp_file; - $shouldSave = false; -} - -if ($shouldSave) { - // Move the file to the uploads folder - if (move_uploaded_file($_FILES["sharex"]["tmp_name"], $uploadDir . $finalName)) { + // Check if the token is valid + if (!checkToken($token)) { $timeTaken = microtime(true) - $before; - returnJson('OK', $finalName, $timeTaken); - } else { - $timeTaken = microtime(true) - $before; - returnJson('ERROR', 'File upload failed. Does the folder exist and did you CHMOD the folder?', $timeTaken); + returnJson('ERROR', 'Invalid or missing secret key', $timeTaken); + die(); } + + // Check if the file was uploaded + if (!isset($file)) { + $timeTaken = microtime(true) - $before; + returnJson('ERROR', 'No file was uploaded', $timeTaken); + die(); + } + + $target_file = $_FILES["sharex"]["name"]; // File name + $fileType = pathinfo($target_file, PATHINFO_EXTENSION); // File extension (e.g. png, jpg, etc.) + + // Check if the file already exists + if (file_exists($uploadDir . $target_file)) { + $timeTaken = microtime(true) - $before; + returnJson('ERROR', 'File already exists', $timeTaken); + 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; + } + + // Convert the image to webp if applicable + if (in_array($fileType, array("png", "jpeg", "jpg"))) { + $image = imagecreatefromstring(file_get_contents($_FILES["sharex"]["tmp_name"])); + $webp_file = pathinfo($finalName, PATHINFO_FILENAME) . ".webp"; + imagewebp($image, $webp_file, 90); + imagedestroy($image); + $finalName = $webp_file; + $shouldSave = false; + } + + if ($shouldSave) { + // Move the file to the uploads folder + if (move_uploaded_file($_FILES["sharex"]["tmp_name"], $uploadDir . $finalName)) { + $timeTaken = microtime(true) - $before; + returnJson('OK', $finalName, $timeTaken); + } else { + $timeTaken = microtime(true) - $before; + returnJson('ERROR', 'File upload failed. Does the folder exist and did you CHMOD the folder?', $timeTaken); + } + die(); + } + returnJson('OK', $finalName, $timeTaken); +} catch (Exception $e) { + $timeTaken = microtime(true) - $before; + returnJson('ERROR', $e->getMessage(), $timeTaken); die(); } -returnJson('OK', $finalName, $timeTaken); ?> \ No newline at end of file