Esempio n. 1
0
def upload(request):
    if request.method != "POST":
        return not_found()
    if "sessionid" not in request.args:
        return Response("You must submit a sessionid for your upload.")
    sessionid = request.args["sessionid"]

    filename = filehelper.get_filename(config.get_upload_path())
    session.set(sessionid, {"progress": 0, "upload_status": "uploading", "url": "", "filename": filename})
    request.environ["wsgi.input"] = StreamWrapper(
        request.environ["wsgi.input"], request.environ["CONTENT_LENGTH"], sessionid
    )
    # Save the file locally
    f = request.files["file"]
    path = config.get_upload_path() + filename
    f.save(path)
    f.close()

    filehelper.save_metadata(filename, {"title": f.filename, "description": ""})
    session.set(
        sessionid,
        {
            "progress": 100,
            "upload_status": "complete",
            "url": request.host_url + "file/" + filename,
            "filename": filename,
        },
    )
    return Response(
        "<!DOCTYPE html><html><head></head><body>\
                     <p>Upload OK</p></body>"
    )
Esempio n. 2
0
def save_description(request):
    if request.method != "POST":
        return not_found()
    if "sessionid" not in request.form or "description" not in request.form:
        return Response(
            json.dumps({"status": "error", "message": "Could not save description. Required parameters missing."})
        )

    description = request.form["description"]
    obj = session.get(request.form["sessionid"])
    if obj is None or "filename" not in obj:
        return Response(json.dumps({"status": "error", "message": "Could not save metadata file."}))

    filename = obj["filename"]
    metadata = filehelper.save_metadata(filename, {"description": description})
    try:
        title = metadata["title"]
        url = obj["url"]
    except KeyError:
        title = "(upload in progress...)"
        url = ""
    return Response(json.dumps({"status": "success", "title": title, "description": description, "url": url}))
    return Response(json.dumps({"status": "success", "title": title, "description": description, "url": obj["url"]}))