예제 #1
0
파일: file.py 프로젝트: Anhmike/canopsis
    def _connect(self, **kwargs):

        result = super(FileStorage, self)._connect(**kwargs)

        if result:

            self.gridfs = GridFs(
                database=self._database, collection=self.get_table())

        return result
예제 #2
0
파일: file.py 프로젝트: Anhmike/canopsis
class MongoFileStorage(MongoStorage, FileStorage):

    FILENAME = 'filename'

    def _connect(self, **kwargs):

        result = super(FileStorage, self)._connect(**kwargs)

        if result:

            self.gridfs = GridFs(
                database=self._database, collection=self.get_table())

        return result

    def get(self, names, version=-1):

        names = ensure_iterable(names)
        result = []
        for name in names:
            gridout = self.gridfs.get_version(filename=name, version=version)
            fs = MongoFileStream(gridout)
            result.append(fs)

        return result

    def exists(self, name):

        result = self.gridfs.exists(name)

        return result

    def find(self, names=None, meta=None, sort=None, limit=-1, skip=0):

        request = {}

        if names is not None:
            if isinstance(names, basestring):
                request[MongoFileStorage.FILENAME] = names
            else:
                request[MongoFileStorage.FILENAME] = {'$in': names}

        if meta is not None:
            request.update(meta)

        cursor = self.gridfs.find(request)

        if sort is not None:
            cursor.sort(sort)
        if limit > 0:
            cursor.limit(limit)
        if skip > 0:
            cursor.skip(skip)

        result = (MongoFileStream(gridout) for gridout in cursor)

        return result

    def new_file(self, name=None, meta=None, data=None):

        kwargs = {}

        if name is None:
            name = str(uuid())

        kwargs['_id'] = name
        kwargs['filename'] = name

        if meta is not None:
            kwargs['metadata'] = meta

        gridout = self.gridfs.new_file(**kwargs)

        result = MongoFileStream(gridout)

        if data is not None:
            result.write(data)

        return result

    def delete(self, names):

        names = ensure_iterable(names)
        for name in names:
            self.gridfs.delete(file_id=name)