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)

[((6, 6, 6, 21), 'flask.Flask', 'Flask', ({(6, 12, 6, 20): '__name__'}, {}), '(__name__)', False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((14, 11, 14, 39), 'flask.render_template', 'render_template', ({(14, 27, 14, 38): '"""home.html"""'}, {}), "('home.html')", False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((21, 15, 21, 46), 'flask.render_template', 'render_template', ({(21, 31, 21, 45): '"""encrypt.html"""'}, {}), "('encrypt.html')", False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((26, 18, 26, 46), 'flask.request.values.get', 'request.values.get', ({(26, 37, 26, 45): '"""Secret"""'}, {}), "('Secret')", False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((27, 19, 27, 44), 'flask.request.values.get', 'request.values.get', ({(27, 38, 27, 43): '"""key"""'}, {}), "('key')", False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((29, 19, 29, 71), 'os.path.join', 'join', ({(29, 24, 29, 51): "app.config['UPLOAD_FOLDER']", (29, 53, 29, 70): '"""cover_image.png"""'}, {}), "(app.config['UPLOAD_FOLDER'], 'cover_image.png')", False, 'from os.path import join\n'), ((32, 13, 32, 78), 'stego.Steganography', 'Steganography', (), '', False, 'from stego import Steganography\n'), ((34, 15, 34, 97), 'flask.render_template', 'render_template', (), '', False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((36, 20, 36, 38), 'flask.url_for', 'url_for', ({(36, 28, 36, 37): '"""encrypt"""'}, {}), "('encrypt')", False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((43, 15, 43, 46), 'flask.render_template', 'render_template', ({(43, 31, 43, 45): '"""decrypt.html"""'}, {}), "('decrypt.html')", False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((46, 19, 46, 44), 'flask.request.values.get', 'request.values.get', ({(46, 38, 46, 43): '"""key"""'}, {}), "('key')", False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((47, 19, 47, 71), 'os.path.join', 'join', ({(47, 24, 47, 51): "app.config['UPLOAD_FOLDER']", (47, 53, 47, 70): '"""stego_image.png"""'}, {}), "(app.config['UPLOAD_FOLDER'], 'stego_image.png')", False, 'from os.path import join\n'), ((48, 13, 48, 78), 'stego.Steganography', 'Steganography', (), '', False, 'from stego import Steganography\n'), ((50, 15, 50, 79), 'flask.render_template', 'render_template', (), '', False, 'from flask import Flask, render_template, request, redirect, url_for\n'), ((54, 19, 54, 71), 'os.path.join', 'join', ({(54, 24, 54, 51): "app.config['UPLOAD_FOLDER']", (54, 53, 54, 70): '"""stego_image.png"""'}, {}), "(app.config['UPLOAD_FOLDER'], 'stego_image.png')", False, 'from os.path import join\n'), ((56, 15, 56, 62), 'flask.render_template', 'render_template', (), '', False, 'from flask import Flask, render_template, request, redirect, url_for\n')]