diff --git a/.env.example b/.env.example index f07cc42..81a19ad 100644 --- a/.env.example +++ b/.env.example @@ -1,11 +1,17 @@ -SECRET_KEY=changeme -DEBUG=True -UPLOAD_FOLDER="/path/to/upload/folder" -PORT=25 -DOMAIN=127.0.0.1:5000 -SERVER_NAME=example.com -SMTP_SERVER=email.example.com -SENDER_EMAIL=admin@example.com -PASSWORD=changeme -receiver_email_1=admin@example.com -STRIPE_SECRET_KEY=changeme +SECRET_KEY="ASDSADASDASDASDadsf" +DEBUG="True" +APP_UPLOAD_FOLDER="/ecw-to-tiff/uploads" +DOCKER_SOCKET="/var/run/docker.sock" +DOCKER_LIB="/usr/bin/docker" +# change to absolute path where your upload folder inside the app folder is. +UPLOAD_FOLDER_MOUNT_PATH="/path/to/ecw-to-tiff-conversion/app/uploads" +SEND_EMAIL="False" + +# remove the comments below only if SEND_EMAIL="TRUE" +#DOMAIN=127.0.0.1:8888 +#PORT=25 +#SMTP_SERVER=email.example.com +#SENDER_EMAIL=test@example.com +#PASSWORD="changeme" +#receiver_email_1="admin1@example.com" +#receiver_email_2="admin2@example.com" diff --git a/.gitignore b/.gitignore index f312cd9..6b23f88 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__ venv/ .env src/uploads +app/uploads diff --git a/README.md b/README.md index 1018488..0cb59d2 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,7 @@ ## How to run it ``` -python3 -m venv venv -. venv/bin/activate -pip install -r requirements.txt +# change UPLOAD_FOLDER_MOUNT_PATH to the right setting cp .env.example .env -FLASK_DEBUG=1 flask run +docker-compose up --build ``` diff --git a/app/.dockerignore b/app/.dockerignore new file mode 100644 index 0000000..dc6e631 --- /dev/null +++ b/app/.dockerignore @@ -0,0 +1 @@ +uploads/* diff --git a/app/Dockerfile b/app/Dockerfile new file mode 100755 index 0000000..780f893 --- /dev/null +++ b/app/Dockerfile @@ -0,0 +1,19 @@ +# Use Ubuntu 20.04 as the base image +FROM ubuntu:20.04 + +# Update the package index and install necessary dependencies +RUN apt-get update && \ + apt-get install -y software-properties-common && \ + add-apt-repository ppa:deadsnakes/ppa && \ + apt-get update && \ + apt-get install -y python3.9 python3.9-dev python3.9-distutils python3-pip + +# Make sure pip is up to date +RUN python3.9 -m pip install --upgrade pip + +USER root +WORKDIR /ecw-to-tiff +COPY ./ /ecw-to-tiff +RUN pip3 install -r requirements.txt +ENTRYPOINT ["/ecw-to-tiff/entrypoint.sh"] + diff --git a/app.py b/app/app.py old mode 100644 new mode 100755 similarity index 91% rename from app.py rename to app/app.py index d4cceb2..da70443 --- a/app.py +++ b/app/app.py @@ -1,5 +1,6 @@ import os import hashlib +import stripe from flask import ( render_template, Flask, @@ -20,15 +21,16 @@ send_notification_upload_email, send_email_to_admin, ) # noqa: E501 -import stripe load_dotenv() SERVER_NAME = os.getenv("SERVER_NAME") -UPLOAD_FOLDER = os.getenv("UPLOAD_FOLDER") +UPLOAD_FOLDER = os.getenv("APP_UPLOAD_FOLDER") +UPLOAD_FOLDER_MOUNT_PATH = os.getenv("UPLOAD_FOLDER_MOUNT_PATH") STRIPE_SECRET_KEY = os.getenv("STRIPE_SECRET_KEY") ALLOWED_EXTENSIONS_TIFF = {".tiff", ".tif"} ALLOWED_EXTENSIONS_ECW = {".ecw"} +SEND_EMAIL = os.getenv("SEND_EMAIL") app = Flask(__name__) app.config["SECRET_KEY"] = os.getenv("SECRET_KEY") @@ -175,11 +177,12 @@ def convert_ecw_to_cog(app=None, filename=None, email=None): print(f"Running background ecw to cog on {filename}") with app.app_context(): subprocess.run( - f"./ecw-to-COG.sh {filename} {UPLOAD_FOLDER}", + f"./ecw-to-COG.sh {filename} '{UPLOAD_FOLDER_MOUNT_PATH}'", shell=True, ) - send_email(email, filename) - send_email_to_admin(email, filename) + if SEND_EMAIL == "True": + send_email(email, filename) + send_email_to_admin(email, filename) @background_task @@ -187,8 +190,9 @@ def convert_tif_to_cog(app=None, filename=None, email=None): print(f"Running background tif to cog on {filename}") with app.app_context(): subprocess.run( - f"./tif-to-COG.sh {filename} {UPLOAD_FOLDER}", + f"./tif-to-COG.sh {filename} '{UPLOAD_FOLDER_MOUNT_PATH}'", shell=True, ) - send_email(email, filename) - send_email_to_admin(email, filename) + if SEND_EMAIL == "True": + send_email(email, filename) + send_email_to_admin(email, filename) diff --git a/app/ecw-to-COG.sh b/app/ecw-to-COG.sh new file mode 100755 index 0000000..235851b --- /dev/null +++ b/app/ecw-to-COG.sh @@ -0,0 +1,10 @@ +#!/bin/bash +set -exu -o pipefail + +MAP_TO_CONVERT=$1 +UPLOAD_FOLDER=$2 + +# Taking out the extension name +COG_MAP=`echo $MAP_TO_CONVERT | cut -d '.' -f 1` + +docker run --rm -v $UPLOAD_FOLDER:/data geodata/gdal:latest /bin/bash -c "gdal_translate -of GTiff -co COMPRESS=DEFLATE -co PREDICTOR=2 -co ZLEVEL=9 -co BIGTIFF=YES $MAP_TO_CONVERT $COG_MAP'_converted.tiff'" diff --git a/emails.py b/app/emails.py old mode 100644 new mode 100755 similarity index 100% rename from emails.py rename to app/emails.py diff --git a/run.sh b/app/entrypoint.sh similarity index 86% rename from run.sh rename to app/entrypoint.sh index 8a9586a..a926888 100755 --- a/run.sh +++ b/app/entrypoint.sh @@ -1,3 +1,4 @@ #!/bin/bash +set -ex flask run --host 0.0.0.0 --port 8888 diff --git a/requirements.txt b/app/requirements.txt old mode 100644 new mode 100755 similarity index 72% rename from requirements.txt rename to app/requirements.txt index 7431be5..ea15f91 --- a/requirements.txt +++ b/app/requirements.txt @@ -1,3 +1,4 @@ Flask==2.2.2 python-dotenv==0.21.0 +Werkzeug==2.2.2 stripe diff --git a/tasks.py b/app/tasks.py old mode 100644 new mode 100755 similarity index 100% rename from tasks.py rename to app/tasks.py diff --git a/templates/homepage.html b/app/templates/homepage.html old mode 100644 new mode 100755 similarity index 100% rename from templates/homepage.html rename to app/templates/homepage.html diff --git a/templates/upload_complete.html b/app/templates/upload_complete.html old mode 100644 new mode 100755 similarity index 100% rename from templates/upload_complete.html rename to app/templates/upload_complete.html diff --git a/app/tif-to-COG.sh b/app/tif-to-COG.sh new file mode 100755 index 0000000..fed981d --- /dev/null +++ b/app/tif-to-COG.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -exu -o pipefail + +MAP_TO_CONVERT=$1 +UPLOAD_FOLDER=$2 + +# Taking out the extension name +COG_MAP=$(echo "$MAP_TO_CONVERT" | cut -d '.' -f 1) + +docker run --rm -v "$UPLOAD_FOLDER":/data osgeo/gdal:latest /bin/bash -c "cd data; gdal_translate -of COG -co NUM_THREADS=ALL_CPUS -co LEVEL=9 -co COMPRESS=DEFLATE -co BIGTIFF=YES -co PREDICTOR=2 $MAP_TO_CONVERT $COG_MAP'_converted.tiff'" + diff --git a/uploads/.gitkeep b/app/uploads/.gitkeep similarity index 100% rename from uploads/.gitkeep rename to app/uploads/.gitkeep diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..08cacf4 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +version: "3.9" +services: + ecw-to-tiff: + build: ./app + volumes: + - "${UPLOAD_FOLDER_MOUNT_PATH}:/ecw-to-tiff/uploads" + - "${DOCKER_SOCKET}:/var/run/docker.sock" + - "${DOCKER_BINARY}:/usr/local/bin/docker" + ports: + - 8888:8888 + env_file: + - .env diff --git a/ecw-to-COG.sh b/ecw-to-COG.sh deleted file mode 100755 index 540e2f1..0000000 --- a/ecw-to-COG.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash -set -exu -o pipefail - -MAP_TO_CONVERT=$1 -UPLOAD_FOLDER=$2 - -# Taking out the extension name -COG_MAP=`echo $MAP_TO_CONVERT | cut -d '.' -f 1` - -docker run --rm -it -v $UPLOAD_FOLDER:/data geodata/gdal:latest /bin/bash -c "gdal_translate -of GTiff -co COMPRESS=DEFLATE -co PREDICTOR=2 -co ZLEVEL=9 -co BIGTIFF=YES $MAP_TO_CONVERT $COG_MAP'_converted.tiff'" diff --git a/tif-to-COG.sh b/tif-to-COG.sh deleted file mode 100755 index e2b6a8a..0000000 --- a/tif-to-COG.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -set -exu -o pipefail - -MAP_TO_CONVERT=$1 -UPLOAD_FOLDER=$2 - -# Taking out the extension name -COG_MAP=$(echo "$MAP_TO_CONVERT" | cut -d '.' -f 1) - -docker run --rm -it -v "$UPLOAD_FOLDER":/data osgeo/gdal:latest /bin/bash -c "cd data; gdal_translate -of COG -co NUM_THREADS=ALL_CPUS -co LEVEL=9 -co COMPRESS=DEFLATE -co BIGTIFF=YES -co PREDICTOR=2 $MAP_TO_CONVERT $COG_MAP'_converted.tiff'" -