import os
import json
import uuid

from flask import (abort, redirect, request, jsonify,
                   send_from_directory, url_for)
import requests

from ahye import app
from ahye.flaskext.mako import render_template as render
from ahye.lib import generate_filename
from ahye.settings import VDIR, LOCAL_UPLOADS_DIR


@app.route('/', methods=['GET'])
def home():
    return render('/home.mako')


@app.route('/upload', methods=['POST'])
def upload():
    data = request.files['imagedata']
    filename = generate_filename()
    data.save(os.path.join(LOCAL_UPLOADS_DIR, filename))
    return url_for('serve_upload', filename=filename, _external=True)


@app.route('/webupload', methods=['POST'])
def webupload():
    retval = []
    for newimg in request.files.getlist('files[]'):
        filename = generate_filename()
        newimg.save(os.path.join(LOCAL_UPLOADS_DIR, filename))
        retval.append({
            "name":filename,
            "size":0,
            "url":"/{0}/{1}".format(VDIR, filename),
            "thumbnail_url":"/blah.jpg",
            "delete_url":"/blah",
            "delete_type":"DELETE"
        })
    return json.dumps(retval)


@app.route('/%s/<filename>' % VDIR)
def serve_upload(filename):
    return send_from_directory(LOCAL_UPLOADS_DIR, filename)


@app.route('/<path:url>')
def crossload(url):
    if not url.endswith(('.jpg', '.png', '.jpeg', '.gif')):
        abort(400)

    # rewrite url as apache's mod_rewrite converts // to /
    if not url.startswith('http://') and url.startswith('http:/'):
        url = url.replace('http:/', 'http://')
    elif not url.startswith('https://') and url.startswith('https:/'):
        url = url.replace('https:/', 'https://')

    filename = '%s.png' % uuid.uuid3(uuid.NAMESPACE_DNS, str(url))
    if not os.path.exists(os.path.join(LOCAL_UPLOADS_DIR, filename)):
        conn = requests.get(url)
        if 200 <= conn.status_code <= 300:
            with open(os.path.join(LOCAL_UPLOADS_DIR, filename), 'w') as f:
                f.write(conn.content)
        else:
            abort(conn.status_code)
    return redirect(url_for('serve_upload', filename=filename, _external=True))


@app.route('/favicon.ico')
def serve_favicon():
    return redirect(url_for('static', filename='favicon.ico'))

@@ -1343,81 +1343,156 @@
     
-if not url.endswith(('.jpg', '.png', '.jpeg', '.gif')):%0A        abort(400
+# reconstruct url (query string has been stripped from url by flask)%0A    if request.query_string:%0A        url = '%25s?%25s' %25 (url, request.query_string
 )%0A%0A 
@@ -1556,20 +1556,16 @@
 %0A    if 
-not 
 url.star
@@ -1578,24 +1578,27 @@
 ('http:/
-/
 ') and 
+not 
 url.star
@@ -1611,16 +1611,17 @@
 ('http:/
+/
 '):%0A    
@@ -1672,20 +1672,16 @@
    elif 
-not 
 url.star
@@ -1695,24 +1695,27 @@
 'https:/
-/
 ') and 
+not 
 url.star
@@ -1729,16 +1729,17 @@
 'https:/
+/
 '):%0A    

Handle crossloading of urls with GET params in them