import logging
import os

from flask import Flask, request, url_for, flash, session, redirect
from gcloud import storage
#from PIL import Image as PILImage

app = Flask(__name__)
app.config['DEBUG'] = True

# Note: We don't need to call run() since our application is embedded within
# the App Engine WSGI application server.

# Configure this environment variable via app.yaml
RENDER_STATIC_FOLDER_NAME = "static/render-img/"
CLOUD_STORAGE_BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
# [end config]

@app.route('/')
#def hello():
#    """Return a friendly HTTP greeting."""
def index():
    #im=PILImage.new("RGB",(256,256),(102,153,255))
    #print(im)
    return """
    Upload Image <br>
<form method="POST" action="/upload" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit">
</form>
"""

@app.route('/upload', methods=['POST'])
def upload():
    """Process the uploaded file and upload it to Google Cloud Storage."""
    uploaded_file = request.files.get('file')

    if not uploaded_file:
        return 'No file uploaded.', 400

    # Create a Cloud Storage client.
    #gcs = storage.Client()

    # Get the bucket that the file will be uploaded to.
    #bucket = gcs.get_bucket(CLOUD_STORAGE_BUCKET)

    # Create a new blob and upload the file's content.
    #blob = bucket.blob(RENDER_STATIC_FOLDER_NAME + uploaded_file.filename)

    #blob.upload_from_string(
    #    uploaded_file.read(),
    #    content_type=uploaded_file.content_type
    #)

    # The public URL can be used to directly access the uploaded file via HTTP.
    #return blob.public_url
    #return redirect(url_for('/'))
    #flash('New entry was successfully posted')
    return redirect(url_for('index'))

@app.errorhandler(500)
def server_error(e):
    logging.exception('An error occurred during a request.')
    return """
    An internal error occurred: <pre>{}</pre>
    See logs for full stacktrace.
    """.format(e), 500

@app.errorhandler(404)
def page_not_found(e):
    """Return a custom 404 error."""
    return 'Sorry, nothing at this URL.', 404

import logging
import os

from flask import Flask, request, url_for, flash, session, redirect
from gcloud import storage
#from PIL import Image as PILImage

app = Flask(__name__)
app.config['DEBUG'] = True

# Note: We don't need to call run() since our application is embedded within
# the App Engine WSGI application server.

# Configure this environment variable via app.yaml
RENDER_STATIC_FOLDER_NAME = "static/render-img/"
CLOUD_STORAGE_BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
# [end config]

@app.route('/')
#def hello():
#    """Return a friendly HTTP greeting."""
def index():
    #im=PILImage.new("RGB",(256,256),(102,153,255))
    #print(im)
    return """
    Upload Image <br>
<form method="POST" action="/upload" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit">
</form>
"""

@app.route('/upload', methods=['POST'])
def upload():
    """Process the uploaded file and upload it to Google Cloud Storage."""
    uploaded_file = request.files.get('file')

    if not uploaded_file:
        return 'No file uploaded.', 400

    # Create a Cloud Storage client.
    #gcs = storage.Client()

    # Get the bucket that the file will be uploaded to.
    #bucket = gcs.get_bucket(CLOUD_STORAGE_BUCKET)

    # Create a new blob and upload the file's content.
    #blob = bucket.blob(RENDER_STATIC_FOLDER_NAME + uploaded_file.filename)

    #blob.upload_from_string(
    #    uploaded_file.read(),
    #    content_type=uploaded_file.content_type
    #)

    # The public URL can be used to directly access the uploaded file via HTTP.
    #return blob.public_url
    #return redirect(url_for('/'))
    #flash('New entry was successfully posted')
    return redirect(url_for('index'))

@app.errorhandler(500)
def server_error(e):
    logging.exception('An error occurred during a request.')
    return """
    An internal error occurred: <pre>{}</pre>
    See logs for full stacktrace.
    """.format(e), 500

@app.errorhandler(404)
def page_not_found(e):
    """Return a custom 404 error."""
    return 'Sorry, nothing at this URL.', 404

#from PIL import Image as PILImage # Note: We don't need to call run() since our application is embedded within # the App Engine WSGI application server. # Configure this environment variable via app.yaml # [end config] #def hello(): #    """Return a friendly HTTP greeting.""" #im=PILImage.new("RGB",(256,256),(102,153,255)) #print(im) Upload Image <br> <form method="POST" action="/upload" enctype="multipart/form-data">     <input type="file" name="file">     <input type="submit"> </form> Process the uploaded file and upload it to Google Cloud Storage. # Create a Cloud Storage client. #gcs = storage.Client() # Get the bucket that the file will be uploaded to. #bucket = gcs.get_bucket(CLOUD_STORAGE_BUCKET) # Create a new blob and upload the file's content. #blob = bucket.blob(RENDER_STATIC_FOLDER_NAME + uploaded_file.filename) #blob.upload_from_string( #    uploaded_file.read(), #    content_type=uploaded_file.content_type #) # The public URL can be used to directly access the uploaded file via HTTP. #return blob.public_url #return redirect(url_for('/')) #flash('New entry was successfully posted') An internal error occurred: <pre>{}</pre>     See logs for full stacktrace. Return a custom 404 error.