Example #1
0
def delete_file(name, filename):
    wh = get_warehouse()
    parcel = get_or_404(wh.get_parcel, name, _exc=KeyError)

    if not authorize_for_parcel(parcel):
        flask.abort(403)
    if not parcel.uploading:
        flask.abort(403)

    if flask.request.method == 'POST':
        filename = secure_filename(filename)
        file_path = flask.safe_join(parcel.get_path(), filename)
        wh.logger.info("Delete file %r for parcel %r (user %s)", filename,
                       parcel.name, _current_user())
        try:
            os.unlink(file_path)
            parcel_file_deleted.send(parcel)
            flask.flash("File %s was deleted." % name, 'system')
        except OSError:
            flask.flash("File %s was not deleted." % name, 'system')
        return flask.redirect(flask.url_for('parcel.view', name=name))
    else:
        return flask.render_template('parcel_file_delete.html',
                                     parcel=parcel,
                                     filename=filename)
Example #2
0
def upload(name):
    wh = get_warehouse()
    parcel = get_or_404(wh.get_parcel, name, _exc=KeyError)
    form = flask.request.form.to_dict()

    if not authorize_for_upload(parcel):
        flask.abort(403)

    identifier = form['resumableIdentifier']
    chunk_number = int(form['resumableChunkNumber'])

    parcel_path = parcel.get_path()
    temp = parcel_path.joinpath(identifier)
    if not os.path.isdir(temp):
        os.makedirs(temp)
    filename = secure_filename(form['resumableFilename'])
    if parcel_path.joinpath(filename).exists():
        return "File already exists", 415

    posted_file = flask.request.files['file']
    tmp_file = tempfile.NamedTemporaryFile(dir=temp, delete=False)
    tmp = path(tmp_file.name)
    tmp_file.close()
    posted_file.save(tmp)

    with exclusive_lock():
        chunk_path = temp.joinpath('%s_%s' % (chunk_number, identifier))
        wh.logger.info("Begin chunked upload file %r for parcel %r (user %s)",
                       filename, parcel.name, _current_user())
        tmp.rename(chunk_path)

    return flask.jsonify({'status': 'success'})
Example #3
0
def delete_file(name, filename):
    wh = get_warehouse()
    parcel = get_or_404(wh.get_parcel, name, _exc=KeyError)

    if not authorize_for_parcel(parcel):
        flask.abort(403)
    if not parcel.uploading:
        flask.abort(403)

    if flask.request.method == 'POST':
        filename = secure_filename(filename)
        file_path = flask.safe_join(parcel.get_path(), filename)
        wh.logger.info("Delete file %r for parcel %r (user %s)",
                       filename, parcel.name, _current_user())
        try:
            os.unlink(file_path)
            parcel_file_deleted.send(parcel)
            flask.flash("File %s was deleted." % name, 'system')
        except OSError:
            flask.flash("File %s was not deleted." % name, 'system')
        return flask.redirect(flask.url_for('parcel.view', name=name))
    else:
        return flask.render_template('parcel_file_delete.html',
                                     parcel=parcel,
                                     filename=filename)
Example #4
0
def upload(name):
    wh = get_warehouse()
    parcel = get_or_404(wh.get_parcel, name, _exc=KeyError)
    form = flask.request.form.to_dict()

    if not authorize_for_upload(parcel):
        flask.abort(403)

    identifier = form['resumableIdentifier']
    chunk_number = int(form['resumableChunkNumber'])

    parcel_path = parcel.get_path()
    temp = parcel_path.joinpath(identifier)
    if not os.path.isdir(temp):
        os.makedirs(temp)
    filename = secure_filename(form['resumableFilename'])
    if parcel_path.joinpath(filename).exists():
        return "File already exists", 415

    posted_file = flask.request.files['file']
    tmp_file = tempfile.NamedTemporaryFile(dir=temp, delete=False)
    tmp = path(tmp_file.name)
    tmp_file.close()
    posted_file.save(tmp)

    with exclusive_lock():
        chunk_path = temp.joinpath('%s_%s' % (chunk_number, identifier))
        wh.logger.info("Begin chunked upload file %r for parcel %r (user %s)",
                       filename, parcel.name, _current_user())
        tmp.rename(chunk_path)

    return flask.jsonify({'status': 'success'})
Example #5
0
def finalize_upload(name):
    wh = get_warehouse()
    parcel = get_or_404(wh.get_parcel, name, _exc=KeyError)
    response = {'status': 'success'}

    form = flask.request.form.to_dict()
    filename = secure_filename(form['resumableFilename'])
    identifier = form['resumableIdentifier']
    total_size = int(form['resumableTotalSize'])

    temp = parcel.get_path().joinpath(identifier)
    if all_chunks_uploaded(temp, total_size):
        create_file_from_chunks(parcel, temp, filename)
        wh.logger.info("Finished chunked upload %r for parcel %r (user %s)",
                       filename, parcel.name, _current_user())
    else:
        response['status'] = 'error'
        response['message'] = "Upload didn't finalize. an error occurred"
    return flask.jsonify(response)
Example #6
0
def finalize_upload(name):
    wh = get_warehouse()
    parcel = get_or_404(wh.get_parcel, name, _exc=KeyError)
    response = {'status': 'success'}

    form = flask.request.form.to_dict()
    filename = secure_filename(form['resumableFilename'])
    identifier = form['resumableIdentifier']
    total_size = int(form['resumableTotalSize'])

    temp = parcel.get_path().joinpath(identifier)
    if all_chunks_uploaded(temp, total_size):
        create_file_from_chunks(parcel, temp, filename)
        wh.logger.info("Finished chunked upload %r for parcel %r (user %s)",
                       filename, parcel.name, _current_user())
    else:
        response['status'] = 'error'
        response['message'] = "Upload didn't finalize. an error occurred"
    return flask.jsonify(response)
Example #7
0
def upload_single_file(name):
    wh = get_warehouse()
    parcel = get_or_404(wh.get_parcel, name, _exc=KeyError)

    if not authorize_for_upload(parcel):
        flask.abort(403)

    posted_file = flask.request.files['file']

    filename = secure_filename(posted_file.filename)
    file_path = parcel.get_path().joinpath(filename)

    if file_path.exists():
        flask.flash("File %s already exists." % filename, 'system')
    else:
        if posted_file:
            posted_file.save(file_path)
            file_uploaded.send(parcel, filename=filename)
            wh.logger.info("Finished upload %r for parcel %r (user %s)",
                           filename, parcel.name, _current_user())
        else:
            flask.flash("Please upload a valid file", 'system')
    return flask.redirect(flask.url_for('parcel.view', name=name))
Example #8
0
def upload_single_file(name):
    wh = get_warehouse()
    parcel = get_or_404(wh.get_parcel, name, _exc=KeyError)

    if not authorize_for_upload(parcel):
        flask.abort(403)

    posted_file = flask.request.files['file']

    filename = secure_filename(posted_file.filename)
    file_path = parcel.get_path().joinpath(filename)

    if file_path.exists():
        flask.flash("File %s already exists." % filename, 'system')
    else:
        if posted_file:
            posted_file.save(file_path)
            file_uploaded.send(parcel, filename=filename)
            wh.logger.info("Finished upload %r for parcel %r (user %s)",
                           filename, parcel.name, _current_user())
        else:
            flask.flash("Please upload a valid file", 'system')
    return flask.redirect(flask.url_for('parcel.view', name=name))