from flask import Flask, render_template, request, redirect, url_for
from os.path import join

from stego import Steganography

app = Flask(__name__)
UPLOAD_FOLDER = 'static/files/'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}


@app.route("/")
def home():
    return render_template('home.html')


@app.route("/encrypt", methods=['GET', 'POST'])
def get_image():

    if request.method == 'GET':
        return render_template('encrypt.html')

    # Check if the user has entered the secret message
    if 'file' in request.files and 'Secret' in request.values:
        uploaded_image = request.files['file']
        message = request.values.get('Secret')
        password = request.values.get("key")

        filepath = join(app.config['UPLOAD_FOLDER'], "cover_image.png")
        uploaded_image.save(filepath)

        im = Steganography(filepath=app.config['UPLOAD_FOLDER'], key=password)
        im.encode(message=message)
        return render_template('encrypt.html', value=filepath, image_flag=True, secret_flag=True)

    return redirect(url_for('encrypt'))


@app.route("/decrypt", methods=['GET', 'POST'])
def get_image_to_decrypt():

    if request.method == 'GET':
        return render_template('decrypt.html')

    if 'key' in request.values:
        password = request.values.get('key')
        filepath = join(app.config['UPLOAD_FOLDER'], "stego_image.png")
        im = Steganography(filepath=app.config['UPLOAD_FOLDER'], key=password)
        message = im.decode()
        return render_template('decrypt.html', value=filepath, message=message)

    if 'file' in request.files:
        uploaded_image = request.files['file']
        filepath = join(app.config['UPLOAD_FOLDER'], "stego_image.png")
        uploaded_image.save(filepath)
        return render_template('decrypt.html', value=filepath)


if __name__ == '__main__':
    app.run(debug=True)

[((134, 149), 'flask.Flask', 'Flask', (['__name__'], {}), '(__name__)\n', (139, 149), False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((311, 339), 'flask.render_template', 'render_template', (['"""home.html"""'], {}), "('home.html')\n", (326, 339), False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((455, 486), 'flask.render_template', 'render_template', (['"""encrypt.html"""'], {}), "('encrypt.html')\n", (470, 486), False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((671, 699), 'flask.request.values.get', 'request.values.get', (['"""Secret"""'], {}), "('Secret')\n", (689, 699), False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((719, 744), 'flask.request.values.get', 'request.values.get', (['"""key"""'], {}), "('key')\n", (737, 744), False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((765, 817), 'os.path.join', 'join', (["app.config['UPLOAD_FOLDER']", '"""cover_image.png"""'], {}), "(app.config['UPLOAD_FOLDER'], 'cover_image.png')\n", (769, 817), False, 'from os.path import join\n'), ((870, 935), 'stego.Steganography', 'Steganography', ([], {'filepath': "app.config['UPLOAD_FOLDER']", 'key': 'password'}), "(filepath=app.config['UPLOAD_FOLDER'], key=password)\n", (883, 935), False, 'from stego import Steganography\n'), ((986, 1072), 'flask.render_template', 'render_template', (['"""encrypt.html"""'], {'value': 'filepath', 'image_flag': '(True)', 'secret_flag': '(True)'}), "('encrypt.html', value=filepath, image_flag=True,\n    secret_flag=True)\n", (1001, 1072), False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((1090, 1108), 'flask.url_for', 'url_for', (['"""encrypt"""'], {}), "('encrypt')\n", (1097, 1108), False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((1236, 1267), 'flask.render_template', 'render_template', (['"""decrypt.html"""'], {}), "('decrypt.html')\n", (1251, 1267), False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((1320, 1345), 'flask.request.values.get', 'request.values.get', (['"""key"""'], {}), "('key')\n", (1338, 1345), False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((1365, 1417), 'os.path.join', 'join', (["app.config['UPLOAD_FOLDER']", '"""stego_image.png"""'], {}), "(app.config['UPLOAD_FOLDER'], 'stego_image.png')\n", (1369, 1417), False, 'from os.path import join\n'), ((1431, 1496), 'stego.Steganography', 'Steganography', ([], {'filepath': "app.config['UPLOAD_FOLDER']", 'key': 'password'}), "(filepath=app.config['UPLOAD_FOLDER'], key=password)\n", (1444, 1496), False, 'from stego import Steganography\n'), ((1542, 1606), 'flask.render_template', 'render_template', (['"""decrypt.html"""'], {'value': 'filepath', 'message': 'message'}), "('decrypt.html', value=filepath, message=message)\n", (1557, 1606), False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((1706, 1758), 'os.path.join', 'join', (["app.config['UPLOAD_FOLDER']", '"""stego_image.png"""'], {}), "(app.config['UPLOAD_FOLDER'], 'stego_image.png')\n", (1710, 1758), False, 'from os.path import join\n'), ((1812, 1859), 'flask.render_template', 'render_template', (['"""decrypt.html"""'], {'value': 'filepath'}), "('decrypt.html', value=filepath)\n", (1827, 1859), False, 'from flask import Flask, render_template, request, redirect, url_for\n')]