예제 #1
0
def api_clone_container(session, container_id):
    containers = dict([(x.uuid, x) for x in session.query(Container).all()])

    if container_id not in containers:
        return abort(404)

    container = containers[container_id]

    cont_clone = Container()
    cont_clone.name = container.name
    cont_clone.create_ts = datetime.utcnow()

    session.add(cont_clone)
    session.commit()
    session.refresh(cont_clone)

    for obj in session.query(File).filter_by(container=container.id).all():
        file_clone = File()
        file_clone.name = obj.name
        file_clone.content = obj.content
        file_clone.container = cont_clone.id
        file_clone.meta = ""
        session.add(file_clone)
        session.commit()

    return jsonify({'uuid': cont_clone.uuid})
예제 #2
0
def api_create_file(session, container_id):
    """
    Create a new file resource with data taken from form upload.
    XXX: Streamed upload and hash calculation are required for efficient
    uploads.
    """
    try:
        os.stat(app.config['UPLOAD_FOLDER'])
    except OSError:
        os.makedirs(app.config['UPLOAD_FOLDER'])

    try:
        os.stat(app.config['CONTENT_FOLDER'])
    except OSError:
        os.makedirs(app.config['CONTENT_FOLDER'])

    if request.method == 'POST':
        container = get_container(container_id)
        if not container:
            abort(404)
        upload = request.files['file']
        if upload:
            filename = secure_filename(upload.filename)
            tmp_name = uuid.uuid1().hex
            upload_path = os.path.join(app.config['UPLOAD_FOLDER'], tmp_name)
            upload.save(upload_path)

            checksum = calculate_sha1(upload_path)
            content = get_content_by_checksum(checksum)

            if not content:
                content = Content()
                content.checksum = checksum
                content.size = upload.content_length
                session.add(content)
                session.commit()
                session.refresh(content)
                content_path = os.path.join(app.config['CONTENT_FOLDER'],
                                            checksum)
                shutil.move(upload_path, content_path)

            file = File()
            file.name = filename
            file.container = container.id
            file.content = content.id

            session.add(file)
            session.commit()

            return jsonify({'id': file.uuid})
        else:
            abort(400)