From 79f4b8ee3a906a17c158eadf5ca30763af6111d4 Mon Sep 17 00:00:00 2001 From: Fascinated Date: Wed, 5 Jul 2023 01:49:59 +0100 Subject: [PATCH] add env var support --- docker-compose.yml | 6 ++++++ docker/start.sh | 11 ++++++++++- upload.php | 32 +++++++++++++++++++++----------- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index c50f6da..cb58185 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,12 @@ services: environment: - MAX_UPLOAD_SIZE=500M - UPLOAD_SECRETS=set me # You can add multiple secrets. Format: secret1,secret2,secret3 + - UPLOAD_DIR=./ # The directory to store the uploaded files + - USE_RANDOM_FILE_NAMES=true # If true, the uploaded files will be renamed to a random string + - SHOULD_CONVERT_TO_WEBP=true # If true, the uploaded files will be converted to webp + - WEBP_QUALITY=95 # The quality of the converted webp files + - WEBP_THREADHOLD=1048576 # The minimum file size to convert to webp (1MB in bytes) + - FILE_NAME_LENGTH=8 # The length of the random file name ports: - 80:80 volumes: diff --git a/docker/start.sh b/docker/start.sh index 3293b93..5ef1821 100644 --- a/docker/start.sh +++ b/docker/start.sh @@ -10,6 +10,15 @@ else echo "Upload script was not found, copying it." fi +# Letting php know that we are running in docker +echo "env[DOCKER] = true" >> /etc/php/8.1/fpm/pool.d/www.conf +echo "env[UPLOAD_DIR] = ${UPLOAD_DIR}" >> /etc/php/8.1/fpm/pool.d/www.conf +echo "env[USE_RANDOM_FILE_NAMES] = ${USE_RANDOM_FILE_NAMES}" >> /etc/php/8.1/fpm/pool.d/www.conf +echo "env[SHOULD_CONVERT_TO_WEBP] = ${SHOULD_CONVERT_TO_WEBP}" >> /etc/php/8.1/fpm/pool.d/www.conf +echo "env[WEBP_QUALITY] = ${WEBP_QUALITY}" >> /etc/php/8.1/fpm/pool.d/www.conf +echo "env[WEBP_THREADHOLD] = ${WEBP_THREADHOLD}" >> /etc/php/8.1/fpm/pool.d/www.conf +echo "env[FILE_NAME_LENGTH] = ${FILE_NAME_LENGTH}" >> /etc/php/8.1/fpm/pool.d/www.conf + echo "Setting permissions for upload script" chmod 777 /var/www/html/upload.php @@ -26,7 +35,7 @@ sed -i "s/^post_max_size = .*/post_max_size = ${MAX_UPLOAD_SIZE}/" /etc/php/8.1/ # 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 -# Setting env variable in php-fpm +# Setting env variable in php-fpm (holy shit this is dumb.. why is php so shit?) echo "env[UPLOAD_SECRETS] = ${UPLOAD_SECRETS}" >> /etc/php/8.1/fpm/pool.d/www.conf # Restart php to apply changes diff --git a/upload.php b/upload.php index 5f1a1c4..ddd4519 100644 --- a/upload.php +++ b/upload.php @@ -10,19 +10,29 @@ header('Content-type:application/json;charset=utf-8'); // Set the response conte /** * Configuration */ -$uploadSecrets; -// Check if the environment variable UPLOAD_SECRETS is set -if (getenv('UPLOAD_SECRETS')) { - $uploadSecrets = explode(",", getenv('UPLOAD_SECRETS')); + +if (getenv('DOCKER')) { // If the script is running in a Docker container + $uploadSecrets = explode(",", getenv('UPLOAD_SECRETS')); // The upload secrets + $uploadDir = getenv('UPLOAD_DIR'); // The upload directory + $useRandomFileNames = getenv('USE_RANDOM_FILE_NAMES'); // Use random file names instead of the original file name + $shouldConvertToWebp = getenv('SHOULD_CONVERT_TO_WEBP'); // Should the script convert images to webp? + $webpQuality = getenv('WEBP_QUALITY'); // The quality of the webp image (0-100) + $webpThreadhold = getenv('WEBP_THREADHOLD'); // The minimum file size for converting to webp (in bytes) + $fileNameLength = getenv('FILE_NAME_LENGTH'); // The length of the random file name } else { - $uploadSecrets = array("set me"); // Use this if you are not using environment variables (eg: Not using Docker) + /** + * !!! + * USE THIS IF YOU ARE NOT USING DOCKER + * !!! + */ + $uploadSecrets = array("set me"); // The upload secrets + $uploadDir = "./"; // The upload directory + $useRandomFileNames = false; // Use random file names instead of the original file name + $shouldConvertToWebp = true; // Should the script convert images to webp? + $webpQuality = 95; // The quality of the webp image (0-100) + $webpThreadhold = 1048576; // 1MB - The minimum file size for converting to webp (in bytes) + $fileNameLength = 8; // The length of the random file name } -$uploadDir = "./"; // The upload directory -$useRandomFileNames = false; // Use random file names instead of the original file name -$shouldConvertToWebp = true; // Should the script convert images to webp? -$webpQuality = 95; // The quality of the webp image (0-100) -$fileNameLength = 8; // The length of the random file name -$webpThreadhold = 1048576; // 1MB - The minimum file size for converting to webp (in bytes) /** * Check if the given secret is valid