def initiate_multipart(context, data_dict):
    """Initiate new Multipart Upload.

    :param context:
    :param data_dict: dict with required keys:
        id: resource's id
        name: filename
        size: filesize

    :returns: MultipartUpload info
    :rtype: dict

    """

    h.check_access('cloudstorage_initiate_multipart', data_dict)
    id, name, size = toolkit.get_or_bust(data_dict, ['id', 'name', 'size'])
    user_id = None
    if context['auth_user_obj']:
        user_id = context['auth_user_obj'].id

    res_dict = toolkit.get_action('resource_show')(context.copy(), {
        'id': data_dict.get('id')
    })
    res_dict['upload_in_progress'] = True
    toolkit.get_action('resource_patch')(context.copy(), res_dict)

    uploader = ResourceCloudStorage({'multipart_name': name})
    res_name = uploader.path_from_filename(id, name)

    upload_object = MultipartUpload.by_name(res_name)

    if upload_object is not None:
        _delete_multipart(upload_object, uploader)
        upload_object = None

    if upload_object is None:
        for old_upload in model.Session.query(MultipartUpload).filter_by(
                resource_id=id):
            _delete_multipart(old_upload, uploader)

        _rindex = res_name.rfind('/')
        if ~_rindex:
            try:
                name_prefix = res_name[:_rindex]
                for cloud_object in uploader.container.iterate_objects():
                    if cloud_object.name.startswith(name_prefix):
                        log.info('Removing cloud object: %s' % cloud_object)
                        cloud_object.delete()
            except Exception as e:
                log.exception('[delete from cloud] %s' % e)

        upload_object = MultipartUpload(
            uploader.driver._initiate_multipart(container=uploader.container,
                                                object_name=res_name), id,
            res_name, size, name, user_id)
        upload_object.save()
    return upload_object.as_dict()
Example #2
0
def initiate_multipart(context, data_dict):
    """Initiate new Multipart Upload.

    :param context:
    :param data_dict: dict with required keys:
        id: resource's id
        name: filename
        size: filesize

    :returns: MultipartUpload info
    :rtype: dict

    """

    toolkit.check_access('cloudstorage_initiate_multipart', context, data_dict)
    id, name, size = toolkit.get_or_bust(data_dict, ['id', 'name', 'size'])
    user_id = None
    if context['auth_user_obj']:
        user_id = context['auth_user_obj'].id

    uploader = ResourceCloudStorage({'multipart_name': name})
    res_name = uploader.path_from_filename(id, name)

    upload_object = MultipartUpload.by_name(res_name)

    if upload_object is not None:
        _delete_multipart(upload_object, uploader)
        upload_object = None

    if upload_object is None:
        for old_upload in model.Session.query(MultipartUpload).filter_by(
                resource_id=id):
            _delete_multipart(old_upload, uploader)

        _rindex = res_name.rfind('/')
        if ~_rindex:
            try:
                name_prefix = res_name[:_rindex]
                for cloud_object in uploader.container.iterate_objects():
                    if cloud_object.name.startswith(name_prefix):
                        log.info('Removing cloud object: %s' % cloud_object)
                        cloud_object.delete()
            except Exception as e:
                log.exception('[delete from cloud] %s' % e)

        resp = uploader.driver.connection.request(
            _get_object_url(uploader, res_name) + '?uploads', method='POST')
        if not resp.success():
            raise toolkit.ValidationError(resp.error)
        try:
            upload_id = resp.object.find('{%s}UploadId' %
                                         resp.object.nsmap[None]).text
        except AttributeError:
            upload_id_list = filter(lambda e: e.tag.endswith('UploadId'),
                                    resp.object.getchildren())
            upload_id = upload_id_list[0].text
        upload_object = MultipartUpload(upload_id, id, res_name, size, name,
                                        user_id)

        upload_object.save()
    return upload_object.as_dict()