Ejemplo n.º 1
0
def upload_to_s3(file_obj, filename=None):
    """Uploads the file_obj to an Amazon S3 bucket under filename if specified."""
    if not filename:
        filename = werkzeug.secure_filename(file_obj.filename)
    randstr = None
    if doc_has_file_twin(make_s3_path(filename)):
        randstr = gen_rand_string(8)
        filename = insert_in_filename(filename, randstr)
    mimetype = mimetypes.guess_type(filename)[0]
    bucket = get_bucket()
    key = bucket.new_key(filename)
    key.set_metadata('Content-Type', mimetype)
    file_obj.seek(0)
    key.set_contents_from_file(file_obj)
    key.set_acl('public-read')
    return randstr
Ejemplo n.º 2
0
def update_doc(doc_id):
    """Upload a new version of a document."""
    doc = Document.query.get(doc_id)
    form = UpdateDocumentForm()
    if form.validate_on_submit():

        file_obj = form.uploaded.data
        filename = insert_in_filename(werkzeug.secure_filename(file_obj.filename), "-{0}-rev.{1}".format(
            current_user.username, len(doc.versions)))
        upload_to_s3(file_obj, filename)
        s3path = make_s3_path(filename)

        version = Version(form.comment.data, s3path, croc.add_doc_to_croc(s3path), doc)

        add_model(version)
        flash("Added new version of {0}".format(doc.name), 'success')
        return redirect(url_for('docs.view_doc', doc_id=doc.uid))
    else:
        return redirect(url_for('docs.view_doc', doc_id=doc.uid))
Ejemplo n.º 3
0
def upload():
    """Display upload template, and upload document if form submitted."""
    form = DocumentForm()
    if form.validate_on_submit():
        file_obj = form.uploaded.data
        filename = werkzeug.secure_filename(file_obj.filename)
        randstr = upload_to_s3(file_obj)
        if randstr:
            filename = insert_in_filename(filename, randstr)
        doc = Document(form.name.data, make_s3_path(filename),
                       request.form['format'], current_user)
        doc.uuid = croc.add_doc_to_croc(doc.s3path)

        db.session.add(doc)
        db.session.commit()

        flash("Successfully uploaded {0}".format(filename), 'success')
        return redirect(url_for('home'))
    return render_template('upload.html', form=form)