import os
from PIL import Image
from flask import request, render_template
from __main__ import app

@app.route("/upload", methods=['GET', 'POST'])
def upload():
    state="Nothing done..."
    success=False

    if request.method == 'POST':
        if 'file' not in request.files:
            state='No file'

        file = request.files['file']

        if file.filename == '':
            state="No File selected!"

        if file:
            filename = "uploadedfile"
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            state="File uploaded!"
            success=True

    imagedata = []
    if success:
        try:
            i = Image.open("uploads/uploadedfile")
            imagedata.append(i.format)
            imagedata.append(i.size)
        except Exception as e:
            imagedata.append(e)

        
    return render_template("upload.html", state=state, imagedata=imagedata)
