Ejemplo n.º 1
0
    async def intra_copy(self, dest_provider, src_path, dest_path):
        created = True
        if dest_path.identifier:
            created = False
            await dest_provider.delete(dest_path)

        async with self.signed_request(
                'POST',
                self.build_url('hooks', 'copy'),
                data=json.dumps({
                    'user': self.auth['id'],
                    'source': src_path.identifier,
                    'destination': {
                        'name': dest_path.name,
                        'node': dest_provider.nid,
                        'parent': dest_path.parent.identifier
                    }
                }),
                headers={'Content-Type': 'application/json'},
                expects=(200, 201)) as resp:
            data = await resp.json()

        if data['kind'] == 'file':
            return OsfStorageFileMetadata(
                data, str(dest_path)), dest_path.identifier is None

        folder_meta = OsfStorageFolderMetadata(data, str(dest_path))
        dest_path = await dest_provider.validate_v1_path(data['path'])
        folder_meta.children = await dest_provider._children_metadata(dest_path
                                                                      )

        return folder_meta, created
Ejemplo n.º 2
0
    def intra_copy(self, dest_provider, src_path, dest_path):
        if dest_path.identifier:
            yield from dest_provider.delete(dest_path)

        resp = yield from self.make_signed_request(
            'POST',
            self.build_url('hooks', 'copy'),
            data=json.dumps({
                'user': self.auth['id'],
                'source': src_path.identifier,
                'destination': {
                    'name': dest_path.name,
                    'node': dest_provider.nid,
                    'parent': dest_path.parent.identifier
                }
            }),
            headers={'Content-Type': 'application/json'},
            expects=(200, 201)
        )

        data = yield from resp.json()

        if data['kind'] == 'file':
            return OsfStorageFileMetadata(data, str(dest_path)), dest_path.identifier is None

        return OsfStorageFolderMetadata(data, str(dest_path)), dest_path.identifier is None
Ejemplo n.º 3
0
    async def _do_intra_move_or_copy(self, action: str, dest_provider,
                                     src_path, dest_path):
        """Update files and folders on osfstorage with a single request.

        If the data of the file or the folder's children doesn't need to be copied to another
        bucket, then doing an intra-move or intra-copy is just a matter of updating the entity
        metadata in the OSF.  If something already exists at ``dest_path``, it must be deleted
        before relocating the source to the new path.
        """

        created = True
        if dest_path.identifier:
            created = False
            await dest_provider.delete(dest_path)

        async with self.signed_request(
                'POST',
                self.build_url('hooks', action),
                data=json.dumps({
                    'user': self.auth['id'],
                    'source': src_path.identifier,
                    'destination': {
                        'name': dest_path.name,
                        'node': dest_provider.nid,
                        'parent': dest_path.parent.identifier
                    }
                }),
                headers={'Content-Type': 'application/json'},
                expects=(200, 201)) as resp:
            data = await resp.json()

        if data['kind'] == 'file':
            return OsfStorageFileMetadata(
                data, str(dest_path)), dest_path.identifier is None

        folder_meta = OsfStorageFolderMetadata(data, str(dest_path))
        dest_path = await dest_provider.validate_v1_path(data['path'])
        folder_meta.children = await dest_provider._children_metadata(dest_path
                                                                      )

        return folder_meta, created
Ejemplo n.º 4
0
    async def _do_intra_move_or_copy(self, action: str, dest_provider, src_path, dest_path):
        """Update files and folders on osfstorage with a single request.

        If the data of the file or the folder's children doesn't need to be copied to another
        bucket, then doing an intra-move or intra-copy is just a matter of updating the entity
        metadata in the OSF.  If something already exists at ``dest_path``, it must be deleted
        before relocating the source to the new path.
        """

        created = True
        if dest_path.identifier:
            created = False
            await dest_provider.delete(dest_path)

        async with self.signed_request(
            'POST',
            self.build_url('hooks', action),
            data=json.dumps({
                'user': self.auth['id'],
                'source': src_path.identifier,
                'destination': {
                    'name': dest_path.name,
                    'node': dest_provider.nid,
                    'parent': dest_path.parent.identifier
                }
            }),
            headers={'Content-Type': 'application/json'},
            expects=(200, 201)
        ) as resp:
            data = await resp.json()

        if data['kind'] == 'file':
            return OsfStorageFileMetadata(data, str(dest_path)), dest_path.identifier is None

        folder_meta = OsfStorageFolderMetadata(data, str(dest_path))
        dest_path = await dest_provider.validate_v1_path(data['path'])
        folder_meta.children = await dest_provider._children_metadata(dest_path)

        return folder_meta, created
Ejemplo n.º 5
0
    def create_folder(self, path, **kwargs):
        resp = yield from self.make_signed_request(
            'POST',
            self.build_url(path.parent.identifier, 'children'),
            data=json.dumps({
                'kind': 'folder',
                'name': path.name,
                'user': self.auth['id'],
            }),
            headers={'Content-Type': 'application/json'},
            expects=(201, ))

        return OsfStorageFolderMetadata((yield from resp.json())['data'],
                                        str(path)).serialized()
Ejemplo n.º 6
0
 async def create_folder(self, path, **kwargs):
     async with self.signed_request(
             'POST',
             self.build_url(path.parent.identifier, 'children'),
             data=json.dumps({
                 'kind': 'folder',
                 'name': path.name,
                 'user': self.auth['id'],
             }),
             headers={'Content-Type': 'application/json'},
             expects=(201, )) as resp:
         resp_json = await resp.json()
         # save new folder's id into the WaterButlerPath object. logs will need it later.
         path._parts[-1]._id = resp_json['data']['path'].strip('/')
         return OsfStorageFolderMetadata(resp_json['data'], str(path))
Ejemplo n.º 7
0
    def _children_metadata(self, path):
        resp = yield from self.make_signed_request(
            'GET',
            self.build_url(path.identifier, 'children'),
            expects=(200, )
        )
        resp_json = yield from resp.json()

        ret = []
        for item in resp_json:
            if item['kind'] == 'folder':
                ret.append(OsfStorageFolderMetadata(item, str(path.child(item['name']))))
            else:
                ret.append(OsfStorageFileMetadata(item, str(path.child(item['name']))))
        return ret
Ejemplo n.º 8
0
    async def _children_metadata(self, path):
        async with self.signed_request('GET',
                                       self.build_url(path.identifier,
                                                      'children'),
                                       expects=(200, )) as resp:
            resp_json = await resp.json()

        ret = []
        for item in resp_json:
            if item['kind'] == 'folder':
                ret.append(
                    OsfStorageFolderMetadata(
                        item, str(path.child(item['name'], folder=True))))
            else:
                ret.append(
                    OsfStorageFileMetadata(item,
                                           str(path.child(item['name']))))
        return ret
Ejemplo n.º 9
0
def folder_metadata_object(folder_metadata):
    path = WaterButlerPath('/' + folder_metadata['data']['name'], folder=True)
    return OsfStorageFolderMetadata(folder_metadata['data'], path)