import os
from flask import Flask, request, redirect, url_for
from werkzeug import secure_filename

from datetime import timedelta
from flask import make_response, request, current_app
from functools import update_wrapper

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

def crossdomain(origin=None, methods=None, headers=None,
                max_age=21600, attach_to_all=True,
                automatic_options=True):
    if methods is not None:
        methods = ', '.join(sorted(x.upper() for x in methods))
    if headers is not None and not isinstance(headers, basestring):
        headers = ', '.join(x.upper() for x in headers)
    if not isinstance(origin, basestring):
        origin = ', '.join(origin)
    if isinstance(max_age, timedelta):
        max_age = max_age.total_seconds()

    def get_methods():
        if methods is not None:
            return methods

        options_resp = current_app.make_default_options_response()
        return options_resp.headers['allow']

    def decorator(f):
        def wrapped_function(*args, **kwargs):
            if automatic_options and request.method == 'OPTIONS':
                resp = current_app.make_default_options_response()
            else:
                resp = make_response(f(*args, **kwargs))
            if not attach_to_all and request.method != 'OPTIONS':
                return resp

            h = resp.headers

            h['Access-Control-Allow-Origin'] = origin
            h['Access-Control-Allow-Methods'] = get_methods()
            h['Access-Control-Max-Age'] = str(max_age)
            if headers is not None:
                h['Access-Control-Allow-Headers'] = headers
            return resp

        f.provide_automatic_options = False
        f.required_methods = ['OPTIONS']
        return update_wrapper(wrapped_function, f)
    return decorator


UPLOAD_FOLDER = '/home/nmz787/public_html/pdf'
ALLOWED_EXTENSIONS = set(['pdf'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

# useful debugging tricks
app.config["TRAP_BAD_REQUEST_KEY_ERRORS"] = True
app.config["TRAP_HTTP_EXCEPTIONS"] = True
app.config["DEBUG"] = True

@app.route('/~nmz787/addpdf', methods=['POST', 'PUT', 'OPTIONS'])
@crossdomain(origin='*', headers='crossDomain, Content-Type')
def upload_file():
    if request.method == 'POST':
        # original url of the document
        #url = request.form["url"]
        print 'request:\n'
        print request
        print '***********'
        print request.files["file"]
        # what sort of content type did the server claim for that file?
        content_type = request.form["contentType"]
        print 'content type is: ' + content_type

        # actual file content
        content = request.files["file"]
        print 'content is:\n'
        print content
        filename = request.form["filename"]

        print 'filename: ' + filename
        file = content
        print allowed_file(filename)
        if file and allowed_file(filename):
            filename = secure_filename(filename)
            filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            print 'saving to filepath: ' + filepath
            file = open(filepath, "wb")
            file.write(content.read())
            file.close()
            return 'http://myserver.com/~nmz787/pdf/'+filename
        # TODO: save file
        return "hello world, the file contents are: " + content
    else:
        return "dunno what to do with your request"

if __name__ == "__main__":
    app.run(host='0.0.0.0')