Compare commits
No commits in common. "780ea18f5bf189320aea840ac4de5247dd270be8" and "48265f6d1b494b2734449dc56224466e2d6da9a4" have entirely different histories.
780ea18f5b
...
48265f6d1b
@ -3,43 +3,27 @@ events {
|
|||||||
}
|
}
|
||||||
|
|
||||||
http {
|
http {
|
||||||
# Log format
|
access_log /dev/stdout;
|
||||||
log_format main '[$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
|
error_log /dev/stdout;
|
||||||
|
|
||||||
access_log /dev/stdout main; # Log access to stdout
|
include mime.types;
|
||||||
error_log /dev/stdout; # Log errors to stdout
|
default_type application/octet-stream;
|
||||||
|
|
||||||
include mime.types; # Include the mime types file
|
|
||||||
default_type application/octet-stream; # Default type
|
|
||||||
|
|
||||||
sendfile on; # Send files directly from disk
|
|
||||||
keepalive_timeout 15; # Keep connections alive for 15 seconds
|
|
||||||
types_hash_max_size 4096; # Max number of mime types
|
|
||||||
|
|
||||||
# TCP optimizations
|
|
||||||
tcp_nopush on; # Send headers in one packet
|
|
||||||
tcp_nodelay on; # Don't wait for packets to be full
|
|
||||||
|
|
||||||
|
sendfile on;
|
||||||
|
tcp_nopush on;
|
||||||
|
tcp_nodelay on;
|
||||||
|
keepalive_timeout 15;
|
||||||
|
types_hash_max_size 4096;
|
||||||
|
|
||||||
server {
|
server {
|
||||||
server_name _; # Listen on all hostnames
|
server_name _;
|
||||||
listen 80; # Listen on port 80
|
listen 80;
|
||||||
|
|
||||||
root /var/www/html; # Serve files from /var/www/html
|
root /var/www/html;
|
||||||
index index.html index.htm; # Serve index.html and index.htm by default
|
index index.html index.htm;
|
||||||
|
|
||||||
# Gzip
|
|
||||||
gzip on;
|
|
||||||
gzip_disable "msie6";
|
|
||||||
gzip_vary on;
|
|
||||||
gzip_proxied any;
|
|
||||||
gzip_comp_level 8;
|
|
||||||
gzip_buffers 16 64k;
|
|
||||||
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
|
|
||||||
|
|
||||||
client_max_body_size 500M; # Will get replaced by the environment variable MAX_UPLOAD_SIZE
|
client_max_body_size 500M; # Will get replaced by the environment variable MAX_UPLOAD_SIZE
|
||||||
|
|
||||||
# Upload endpoint
|
|
||||||
location /upload.php {
|
location /upload.php {
|
||||||
try_files $uri =404;
|
try_files $uri =404;
|
||||||
|
|
||||||
@ -51,15 +35,9 @@ http {
|
|||||||
include fastcgi_params;
|
include fastcgi_params;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Serve your files
|
|
||||||
location / {
|
location / {
|
||||||
expires 7d;
|
expires 7d;
|
||||||
|
|
||||||
open_file_cache max=1000 inactive=60s;
|
|
||||||
open_file_cache_valid 60s;
|
|
||||||
open_file_cache_min_uses 1;
|
|
||||||
open_file_cache_errors on;
|
|
||||||
|
|
||||||
# Serve the file directly from disk
|
# Serve the file directly from disk
|
||||||
try_files $uri $uri/ =404;
|
try_files $uri $uri/ =404;
|
||||||
}
|
}
|
||||||
|
@ -26,15 +26,8 @@ sed -i "s/^post_max_size = .*/post_max_size = ${MAX_UPLOAD_SIZE}/" /etc/php/8.1/
|
|||||||
# Set max upload size for nginx
|
# Set max upload size for nginx
|
||||||
sed -i "s/client_max_body_size 500M;/client_max_body_size ${MAX_UPLOAD_SIZE};/" /etc/nginx/nginx.conf
|
sed -i "s/client_max_body_size 500M;/client_max_body_size ${MAX_UPLOAD_SIZE};/" /etc/nginx/nginx.conf
|
||||||
|
|
||||||
function start() {
|
# Start Nginx
|
||||||
echo "Starting PHP & Nginx"
|
echo "Starting PHP & Nginx"
|
||||||
/etc/init.d/php8.1-fpm start &&
|
/etc/init.d/php8.1-fpm start &&
|
||||||
chmod 777 /run/php/php8.1-fpm.sock &&
|
chmod 777 /run/php/php8.1-fpm.sock &&
|
||||||
nginx -g 'daemon off;'
|
nginx -g 'daemon off;'
|
||||||
}
|
|
||||||
|
|
||||||
# Start Nginx and retry if it fails
|
|
||||||
until start; do
|
|
||||||
echo "Nginx failed to start, retrying in 5 seconds..."
|
|
||||||
sleep 5
|
|
||||||
done
|
|
45
upload.php
45
upload.php
@ -3,7 +3,6 @@
|
|||||||
/**
|
/**
|
||||||
* DO NOT TOUCH!!!!!!!!
|
* DO NOT TOUCH!!!!!!!!
|
||||||
*/
|
*/
|
||||||
$SCRIPT_VERSION = "0.1.0"; // The version of the script
|
|
||||||
$before = microtime(true); // Start time of the script
|
$before = microtime(true); // Start time of the script
|
||||||
$defaultSecretKey = "set me"; // The default secret key
|
$defaultSecretKey = "set me"; // The default secret key
|
||||||
header('Content-type:application/json;charset=utf-8'); // Set the response content type to JSON
|
header('Content-type:application/json;charset=utf-8'); // Set the response content type to JSON
|
||||||
@ -81,38 +80,19 @@ function returnJson($data): void
|
|||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Log to nginx
|
|
||||||
*/
|
|
||||||
function logToNginx($message): void
|
|
||||||
{
|
|
||||||
error_log($message);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$secret = isset($_POST['secret']) ? $_POST['secret'] : null; // The secret key
|
$secret = $_POST['secret']; // The secret key
|
||||||
$file = isset($_FILES['sharex']) ? $_FILES['sharex'] : null; // The uploaded file
|
$file = $_FILES['sharex']; // The uploaded file
|
||||||
|
|
||||||
// Page to show if someone visits the upload script
|
|
||||||
if ($secret == null && $file == null) {
|
|
||||||
returnJson(array(
|
|
||||||
'status' => 'OK',
|
|
||||||
'url' => 'Welcome to the ShareX PHP Uploader! v' . $SCRIPT_VERSION,
|
|
||||||
// Remove this if you don't want to show the support URL
|
|
||||||
'support' => "For support, visit - https://git.fascinated.cc/Fascinated/sharex-php-uploader",
|
|
||||||
'timeTaken' => getTimeTaken()
|
|
||||||
));
|
|
||||||
die();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the token is valid
|
// Check if the token is valid
|
||||||
if (!checkSecret($secret)) {
|
if (!checkSecret($secret)) {
|
||||||
returnJson(array(
|
returnJson(array(
|
||||||
'status' => 'ERROR',
|
'status' => 'ERROR',
|
||||||
'url' => 'Invalid or missing upload secret',
|
'url' => 'Invalid or missing upload secret',
|
||||||
|
// Remove this if you don't want to show the support URL
|
||||||
|
'support' => "For support, visit - https://git.fascinated.cc/Fascinated/sharex-php-uploader",
|
||||||
'timeTaken' => getTimeTaken()
|
'timeTaken' => getTimeTaken()
|
||||||
));
|
));
|
||||||
logToNginx("An upload was attempted with an invalid secret key: " . $secret);
|
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,7 +103,6 @@ try {
|
|||||||
'url' => 'You need to set your upload secret in the configuration section of the upload.php file',
|
'url' => 'You need to set your upload secret in the configuration section of the upload.php file',
|
||||||
'timeTaken' => getTimeTaken()
|
'timeTaken' => getTimeTaken()
|
||||||
));
|
));
|
||||||
logToNginx("An upload was attempted with the default secret key");
|
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,26 +113,23 @@ try {
|
|||||||
'url' => 'No file was uploaded',
|
'url' => 'No file was uploaded',
|
||||||
'timeTaken' => getTimeTaken()
|
'timeTaken' => getTimeTaken()
|
||||||
));
|
));
|
||||||
logToNginx("An upload was attempted without providing a file");
|
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
$originalFileName = preg_replace("/[^A-Za-z0-9_.]/", '', $_FILES["sharex"]["name"]); // Remove unwanted characters
|
$target_file = preg_replace("/[^A-Za-z0-9_.]/", '', $_FILES["sharex"]["name"]); // Remove unwanted characters
|
||||||
$fileType = pathinfo($originalFileName, PATHINFO_EXTENSION); // File extension (e.g. png, jpg, etc.)
|
$fileType = pathinfo($target_file, PATHINFO_EXTENSION); // File extension (e.g. png, jpg, etc.)
|
||||||
$fileSize = $_FILES["sharex"]["size"]; // File size in bytes
|
|
||||||
|
|
||||||
// Check if the file already exists
|
// Check if the file already exists
|
||||||
if (file_exists($uploadDir . $originalFileName)) {
|
if (file_exists($uploadDir . $target_file)) {
|
||||||
returnJson(array(
|
returnJson(array(
|
||||||
'status' => 'ERROR',
|
'status' => 'ERROR',
|
||||||
'url' => 'File already exists',
|
'url' => 'File already exists',
|
||||||
'timeTaken' => getTimeTaken()
|
'timeTaken' => getTimeTaken()
|
||||||
));
|
));
|
||||||
logToNginx("An upload was attempted with a file that already exists: " . $originalFileName);
|
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
$finalName = $originalFileName; // 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;
|
||||||
}
|
}
|
||||||
@ -179,8 +155,6 @@ try {
|
|||||||
$image->clear();
|
$image->clear();
|
||||||
$image->destroy();
|
$image->destroy();
|
||||||
|
|
||||||
$fileSize = filesize($webp_file); // Update the file size
|
|
||||||
|
|
||||||
// Update the final filename
|
// Update the final filename
|
||||||
$finalName = $webp_file;
|
$finalName = $webp_file;
|
||||||
$needsToBeSaved = false;
|
$needsToBeSaved = false;
|
||||||
@ -195,7 +169,6 @@ try {
|
|||||||
'url' => 'Failed to save file. Check the permissions of the upload directory.',
|
'url' => 'Failed to save file. Check the permissions of the upload directory.',
|
||||||
'timeTaken' => getTimeTaken()
|
'timeTaken' => getTimeTaken()
|
||||||
));
|
));
|
||||||
logToNginx("An upload was attempted but the file could not be saved: " . $finalName);
|
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -204,7 +177,6 @@ try {
|
|||||||
'url' => $finalName,
|
'url' => $finalName,
|
||||||
'timeTaken' => getTimeTaken()
|
'timeTaken' => getTimeTaken()
|
||||||
));
|
));
|
||||||
logToNginx("An upload was successful. original id: $originalFileName, final id: $finalName, size: $fileSize");
|
|
||||||
die();
|
die();
|
||||||
} catch (Exception $e) { // Handle any errors
|
} catch (Exception $e) { // Handle any errors
|
||||||
returnJson(array(
|
returnJson(array(
|
||||||
@ -212,6 +184,5 @@ try {
|
|||||||
'url' => $e->getMessage(),
|
'url' => $e->getMessage(),
|
||||||
'timeTaken' => getTimeTaken()
|
'timeTaken' => getTimeTaken()
|
||||||
));
|
));
|
||||||
logToNginx("An upload was attempted but an error occurred: " . $e->getMessage());
|
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user