add env var support
All checks were successful
/ docker (push) Successful in 1m45s

This commit is contained in:
Lee 2023-07-05 01:49:59 +01:00
parent e18239d5bf
commit 79f4b8ee3a
3 changed files with 37 additions and 12 deletions

@ -9,6 +9,12 @@ services:
environment: environment:
- MAX_UPLOAD_SIZE=500M - MAX_UPLOAD_SIZE=500M
- UPLOAD_SECRETS=set me # You can add multiple secrets. Format: secret1,secret2,secret3 - 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: ports:
- 80:80 - 80:80
volumes: volumes:

@ -10,6 +10,15 @@ else
echo "Upload script was not found, copying it." echo "Upload script was not found, copying it."
fi 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" echo "Setting permissions for upload script"
chmod 777 /var/www/html/upload.php 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 # 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
# 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 echo "env[UPLOAD_SECRETS] = ${UPLOAD_SECRETS}" >> /etc/php/8.1/fpm/pool.d/www.conf
# Restart php to apply changes # Restart php to apply changes

@ -10,19 +10,29 @@ header('Content-type:application/json;charset=utf-8'); // Set the response conte
/** /**
* Configuration * Configuration
*/ */
$uploadSecrets;
// Check if the environment variable UPLOAD_SECRETS is set if (getenv('DOCKER')) { // If the script is running in a Docker container
if (getenv('UPLOAD_SECRETS')) { $uploadSecrets = explode(",", getenv('UPLOAD_SECRETS')); // The upload secrets
$uploadSecrets = explode(",", getenv('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 { } 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 $uploadDir = "./"; // The upload directory
$useRandomFileNames = false; // Use random file names instead of the original file name $useRandomFileNames = false; // Use random file names instead of the original file name
$shouldConvertToWebp = true; // Should the script convert images to webp? $shouldConvertToWebp = true; // Should the script convert images to webp?
$webpQuality = 95; // The quality of the webp image (0-100) $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) $webpThreadhold = 1048576; // 1MB - The minimum file size for converting to webp (in bytes)
$fileNameLength = 8; // The length of the random file name
}
/** /**
* Check if the given secret is valid * Check if the given secret is valid