Example #1
0
def osfstorage_delete(file_node, payload, target, **kwargs):
    user = OSFUser.load(payload['user'])
    auth = Auth(user)

    #TODO Auth check?
    if not auth:
        raise HTTPError(httplib.BAD_REQUEST)
    if file_node == OsfStorageFolder.objects.get_root(target=target):
        raise HTTPError(httplib.BAD_REQUEST)

    try:
        file_node.delete(user=user)

    except exceptions.FileNodeCheckedOutError:
        raise HTTPError(httplib.FORBIDDEN)
    except exceptions.FileNodeIsPrimaryFile:
        raise HTTPError(
            httplib.FORBIDDEN,
            data={
                'message_long':
                'Cannot delete file as it is the primary file of preprint.'
            })

    update_storage_usage(file_node.target)
    return {'status': 'success'}
Example #2
0
    def perform_file_action(self, source, destination, name):
        dest_target = destination.target
        source_target = source.target
        ret = source.move_under(destination, name)

        if dest_target != source_target:
            update_storage_usage(source.target)
            update_storage_usage(destination.target)

        return ret
Example #3
0
def osfstorage_move_hook(source, destination, name=None, **kwargs):
    source_target = source.target

    try:
        ret = source.move_under(destination, name=name).serialize(), httplib.OK
    except exceptions.FileNodeCheckedOutError:
        raise HTTPError(httplib.METHOD_NOT_ALLOWED, data={
            'message_long': 'Cannot move file as it is checked out.'
        })
    except exceptions.FileNodeIsPrimaryFile:
        raise HTTPError(httplib.FORBIDDEN, data={
            'message_long': 'Cannot move file as it is the primary file of preprint.'
        })

    # once the move is complete recalculate storage for both targets if it's a inter-target move.
    if source_target != destination.target:
        update_storage_usage(destination.target)
        update_storage_usage(source_target)

    return ret
Example #4
0
def osfstorage_move_hook(source, destination, name=None, **kwargs):
    source_target = source.target

    try:
        ret = source.move_under(destination, name=name).serialize(), httplib.OK
    except exceptions.FileNodeCheckedOutError:
        raise HTTPError(httplib.METHOD_NOT_ALLOWED, data={
            'message_long': 'Cannot move file as it is checked out.'
        })
    except exceptions.FileNodeIsPrimaryFile:
        raise HTTPError(httplib.FORBIDDEN, data={
            'message_long': 'Cannot move file as it is the primary file of preprint.'
        })

    # once the move is complete recalculate storage for both targets if it's a inter-target move.
    if source_target != destination.target:
        update_storage_usage(destination.target)
        update_storage_usage(source_target)

    return ret
Example #5
0
def osfstorage_delete(file_node, payload, target, **kwargs):
    user = OSFUser.load(payload['user'])
    auth = Auth(user)

    #TODO Auth check?
    if not auth:
        raise HTTPError(httplib.BAD_REQUEST)
    if file_node == OsfStorageFolder.objects.get_root(target=target):
            raise HTTPError(httplib.BAD_REQUEST)

    try:
        file_node.delete(user=user)

    except exceptions.FileNodeCheckedOutError:
        raise HTTPError(httplib.FORBIDDEN)
    except exceptions.FileNodeIsPrimaryFile:
        raise HTTPError(httplib.FORBIDDEN, data={
            'message_long': 'Cannot delete file as it is the primary file of preprint.'
        })

    update_storage_usage(file_node.target)
    return {'status': 'success'}
Example #6
0
def osfstorage_create_child(file_node, payload, **kwargs):
    parent = file_node  # Just for clarity
    name = payload.get('name')
    user = OSFUser.load(payload.get('user'))
    is_folder = payload.get('kind') == 'folder'

    if getattr(file_node.target, 'is_registration', False) and not getattr(file_node.target, 'archiving', False):
        raise HTTPError(
            httplib.BAD_REQUEST,
            data={
                'message_short': 'Registered Nodes are immutable',
                'message_long': "The operation you're trying to do cannot be applied to registered Nodes, which are immutable",
            }
        )

    if not (name or user) or '/' in name:
        raise HTTPError(httplib.BAD_REQUEST)

    if getattr(file_node.target, 'is_quickfiles', False) and is_folder:
        raise HTTPError(httplib.BAD_REQUEST, data={'message_long': 'You may not create a folder for QuickFiles'})

    try:
        # Create a save point so that we can rollback and unlock
        # the parent record
        with transaction.atomic():
            if is_folder:
                created, file_node = True, parent.append_folder(name)
            else:
                created, file_node = True, parent.append_file(name)
    except (ValidationError, IntegrityError):
        created, file_node = False, parent.find_child_by_name(name, kind=int(not is_folder))

    if not created and is_folder:
        raise HTTPError(httplib.CONFLICT, data={
            'message_long': 'Cannot create folder "{name}" because a file or folder already exists at path "{path}"'.format(
                name=file_node.name,
                path=file_node.materialized_path,
            )
        })

    if file_node.checkout and file_node.checkout._id != user._id:
        raise HTTPError(httplib.FORBIDDEN, data={
            'message_long': 'File cannot be updated due to checkout status.'
        })

    if not is_folder:
        try:
            metadata = dict(payload['metadata'], **payload['hashes'])
            location = dict(payload['settings'], **dict(
                payload['worker'], **{
                    'object': payload['metadata']['name'],
                    'service': payload['metadata']['provider'],
                }
            ))
        except KeyError:
            raise HTTPError(httplib.BAD_REQUEST)
        current_version = file_node.get_version()
        new_version = file_node.create_version(user, location, metadata)

        if not current_version or not current_version.is_duplicate(new_version):
            update_storage_usage(file_node.target)

        version_id = new_version._id
        archive_exists = new_version.archive is not None
    else:
        version_id = None
        archive_exists = False

    return {
        'status': 'success',
        'archive': not archive_exists,  # Should waterbutler also archive this file
        'data': file_node.serialize(),
        'version': version_id,
    }, httplib.CREATED if created else httplib.OK
Example #7
0
def osfstorage_copy_hook(source, destination, name=None, **kwargs):
    ret = source.copy_under(destination, name=name).serialize(), httplib.CREATED
    update_storage_usage(destination.target)
    return ret
Example #8
0
 def perform_file_action(self, source, destination, name):
     ret = source.copy_under(destination, name)
     update_storage_usage(destination.target)
     return ret
Example #9
0
def osfstorage_create_child(file_node, payload, **kwargs):
    parent = file_node  # Just for clarity
    name = payload.get('name')
    user = OSFUser.load(payload.get('user'))
    is_folder = payload.get('kind') == 'folder'

    if getattr(file_node.target, 'is_registration', False) and not getattr(file_node.target, 'archiving', False):
        raise HTTPError(
            httplib.BAD_REQUEST,
            data={
                'message_short': 'Registered Nodes are immutable',
                'message_long': "The operation you're trying to do cannot be applied to registered Nodes, which are immutable",
            }
        )

    if not (name or user) or '/' in name:
        raise HTTPError(httplib.BAD_REQUEST)

    if getattr(file_node.target, 'is_quickfiles', False) and is_folder:
        raise HTTPError(httplib.BAD_REQUEST, data={'message_long': 'You may not create a folder for QuickFiles'})

    try:
        # Create a save point so that we can rollback and unlock
        # the parent record
        with transaction.atomic():
            if is_folder:
                created, file_node = True, parent.append_folder(name)
            else:
                created, file_node = True, parent.append_file(name)
    except (ValidationError, IntegrityError):
        created, file_node = False, parent.find_child_by_name(name, kind=int(not is_folder))

    if not created and is_folder:
        raise HTTPError(httplib.CONFLICT, data={
            'message_long': 'Cannot create folder "{name}" because a file or folder already exists at path "{path}"'.format(
                name=file_node.name,
                path=file_node.materialized_path,
            )
        })

    if file_node.checkout and file_node.checkout._id != user._id:
        raise HTTPError(httplib.FORBIDDEN, data={
            'message_long': 'File cannot be updated due to checkout status.'
        })

    if not is_folder:
        try:
            metadata = dict(payload['metadata'], **payload['hashes'])
            location = dict(payload['settings'], **dict(
                payload['worker'], **{
                    'object': payload['metadata']['name'],
                    'service': payload['metadata']['provider'],
                }
            ))
        except KeyError:
            raise HTTPError(httplib.BAD_REQUEST)
        current_version = file_node.get_version()
        new_version = file_node.create_version(user, location, metadata)

        if not current_version or not current_version.is_duplicate(new_version):
            update_storage_usage(file_node.target)

        version_id = new_version._id
        archive_exists = new_version.archive is not None
    else:
        version_id = None
        archive_exists = False

    return {
        'status': 'success',
        'archive': not archive_exists,  # Should waterbutler also archive this file
        'data': file_node.serialize(),
        'version': version_id,
    }, httplib.CREATED if created else httplib.OK
Example #10
0
def osfstorage_copy_hook(source, destination, name=None, **kwargs):
    ret = source.copy_under(destination, name=name).serialize(), httplib.CREATED
    update_storage_usage(destination.target)
    return ret