Esempio n. 1
0
    def _getFirstFileByHash(self, algo, hash, user=None):
        """
        Return the first file that the user has access to given its hash and its
        associated hashsum algorithm name.

        :param algo: Algorithm the given hash is encoded with.
        :param hash: Hash of the file to find.
        :param user: User to test access against.
         Default (none) is the current user.
        :return: A file document.
        """
        self._validateAlgo(algo)

        query = {algo: hash}
        fileModel = FileModel()
        cursor = fileModel.find(query)

        if not user:
            user = self.getCurrentUser()

        for file in cursor:
            if fileModel.hasAccess(file, user, AccessType.READ):
                return file

        return None
Esempio n. 2
0
def _computeHash(file, progress=noProgress):
    """
    Computes all supported checksums on a given file. Downloads the
    file data and stream-computes all required hashes on it, saving
    the results in the file document.

    In the case of assetstore impls that already compute the sha512,
    and when sha512 is the only supported algorithm, we will not download
    the file to the server.
    """
    toCompute = SUPPORTED_ALGORITHMS - set(file)
    toCompute = {alg: getattr(hashlib, alg)() for alg in toCompute}

    if not toCompute:
        return

    fileModel = FileModel()
    with fileModel.open(file) as fh:
        while True:
            chunk = fh.read(_CHUNK_LEN)
            if not chunk:
                break
            for digest in six.viewvalues(toCompute):
                digest.update(chunk)
            progress.update(increment=len(chunk))

    digests = {
        alg: digest.hexdigest()
        for alg, digest in six.viewitems(toCompute)
    }
    fileModel.update({'_id': file['_id']},
                     update={'$set': digests},
                     multi=False)

    return digests
Esempio n. 3
0
    def delete(self, id, user):
        stem_image = self.load(id, user=user, level=AccessType.WRITE)

        if not stem_image:
            raise RestException('StemImage not found.', 404)

        public = stem_image.get('public', False)

        # Try to load the file and check if it was imported.
        # If it was imported, delete the item containing the file.
        # If loading/removing the file fails, remove the stem image anyways
        try:
            f = FileModel().load(stem_image['fileId'],
                                 level=AccessType.WRITE,
                                 user=user)
            if f.get('imported', False) is True:
                item = ItemModel().load(f['itemId'],
                                        level=AccessType.WRITE,
                                        user=user)
                if item['folderId'] == self._get_import_folder(user,
                                                               public)['_id']:
                    ItemModel().remove(item)
        except:
            pass

        return self.remove(stem_image)
Esempio n. 4
0
    def getByHash(self, algo, hash):
        self._validateAlgo(algo)

        model = FileModel()
        user = self.getCurrentUser()
        cursor = model.find({algo: hash})
        return [file for file in cursor if model.hasAccess(file, user, AccessType.READ)]
Esempio n. 5
0
    def validate(self, doc):
        # Ensure the file exists
        if 'fileId' in doc:
            FileModel().load(doc['fileId'], level=AccessType.READ, force=True)
        else:
            raise RestException('stem image must contain `fileId`')

        return doc
Esempio n. 6
0
    def _open_h5py_file(self, stem_image_id, user):
        """Get an h5py file object of the stem image

        This should be used as a context manager as such:

        with self._open_h5py_file(id, user) as f:
            do_stuff_with_file(f)
        """
        stem_image = self.load(stem_image_id, user=user, level=AccessType.READ)

        if not stem_image:
            raise RestException('StemImage not found.', 404)

        girder_file = FileModel().load(stem_image['fileId'],
                                       level=AccessType.READ,
                                       user=user)
        with FileModel().open(girder_file) as rf:
            yield h5py.File(rf, 'r')
Esempio n. 7
0
 def file_path(self, id, user):
     stem_image = self.load(id, user=user, level=AccessType.READ)
     file = FileModel().load(stem_image['fileId'],
                             user=user,
                             level=AccessType.READ)
     assetstore = AssetstoreModel().load(file['assetstoreId'])
     if assetstore['type'] is not AssetstoreType.FILESYSTEM:
         raise RestException('The file assetstore is not a file system!')
     path = os.path.join(assetstore['root'], file['path'])
     return {'path': path}
Esempio n. 8
0
def _getFolderReadme(folder):
    query = {
        'folderId': folder['_id'],
        'name': {
            '$regex': re.compile(r'^README(\..+)?$')
        },
    }
    item = ItemModel().findOne(query)
    if item:
        files = list(ItemModel().childFiles(item=item, limit=1))
        if len(files) >= 1:
            return FileModel().download(files[0])
    cherrypy.response.status = 204
    return ''
Esempio n. 9
0
    def __init__(self):
        super(File, self).__init__()
        self._model = FileModel()

        self.resourceName = 'file'
        self.route('DELETE', (':id',), self.deleteFile)
        self.route('DELETE', ('upload', ':id'), self.cancelUpload)
        self.route('GET', ('offset',), self.requestOffset)
        self.route('GET', (':id',), self.getFile)
        self.route('GET', (':id', 'download'), self.download)
        self.route('GET', (':id', 'download', ':name'), self.downloadWithName)
        self.route('POST', (), self.initUpload)
        self.route('POST', ('chunk',), self.readChunk)
        self.route('POST', ('completion',), self.finalizeUpload)
        self.route('POST', (':id', 'copy'), self.copy)
        self.route('PUT', (':id',), self.updateFile)
        self.route('PUT', (':id', 'contents'), self.updateFileContents)
        self.route('PUT', (':id', 'move'), self.moveFileToAssetstore)
Esempio n. 10
0
    def load(self, info):
        HashedFile(info['apiRoot'].file)
        FileModel().exposeFields(level=AccessType.READ,
                                 fields=SUPPORTED_ALGORITHMS)

        events.bind('data.process', 'hashsum_download', _computeHashHook)