Forked from nmz787/pdf bookmarklet uploader bookmar
Last active
December 17, 2015 05:59
-
-
Save kanzure/5562043 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var xhr = new XMLHttpRequest(); | |
xhr.onreadystatechange = function() { | |
if (xhr.readyState == 4 && xhr.responseText != undefined && xhr.responseText != "") { | |
var xhr2 = new XMLHttpRequest(); | |
xhr2.onreadystatechange = function() { | |
switch (xhr2.readyState) { | |
case 0: // uninitialized | |
case 1: // loading | |
case 2: // loading | |
case 3: // interactive | |
break; | |
case 4: // completed | |
if (xhr2.status == 200) { | |
alert("done uploading") | |
} else if (xhr2.status != 200) { | |
alert("upload failure, status code " + xhr2.status); | |
} | |
break; | |
default: | |
alert("bad readyState value"); | |
} | |
} | |
xhr2.open("POST", 'http://localhost:5000/~nmz787/addpdf', false); | |
xhr2.setRequestHeader('crossDomain','true'); | |
var formData = new FormData(); | |
formData.append("content", xhr.responseText); | |
formData.append("url", window.location); | |
formData.append("contentType", xhr.getResponseHeader("content-type")); | |
xhr2.send(formData); | |
} | |
} | |
xhr.open("GET", window.location, false); | |
xhr.send(null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Flask==0.9 | |
Jinja2==2.6 | |
Werkzeug==0.8.3 | |
argparse==1.2.1 | |
stevedore==0.7.2 | |
virtualenv==1.8.4 | |
virtualenv-clone==0.2.4 | |
virtualenvwrapper==3.6 | |
wsgiref==0.1.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
def upload_file(): | |
if request.method == 'POST': | |
# original url of the document | |
url = request.form["url"] | |
# what sort of content type did the server claim for that file? | |
content_type = request.form["contentType"] | |
# actual file content | |
content = request.form["content"] | |
# 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='localhost') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment