Search in sources :

Example 16 with GridFSFile

use of com.mongodb.client.gridfs.model.GridFSFile in project mongo-java-driver by mongodb.

the class GridFSDownloadPublisherImpl method getChunkPublisher.

private Flux<ByteBuffer> getChunkPublisher(final GridFSFile gridFSFile) {
    Document filter = new Document("files_id", gridFSFile.getId());
    FindPublisher<Document> chunkPublisher;
    if (clientSession != null) {
        chunkPublisher = chunksCollection.find(clientSession, filter);
    } else {
        chunkPublisher = chunksCollection.find(filter);
    }
    AtomicInteger chunkCounter = new AtomicInteger(0);
    int numberOfChunks = (int) Math.ceil((double) gridFSFile.getLength() / gridFSFile.getChunkSize());
    Flux<ByteBuffer> byteBufferFlux = Flux.from(chunkPublisher.sort(new Document("n", 1))).map(chunk -> {
        int expectedChunkIndex = chunkCounter.getAndAdd(1);
        if (chunk == null || chunk.getInteger("n") != expectedChunkIndex) {
            throw new MongoGridFSException(format("Could not find file chunk for files_id: %s at chunk index %s.", gridFSFile.getId(), expectedChunkIndex));
        } else if (!(chunk.get("data") instanceof Binary)) {
            throw new MongoGridFSException("Unexpected data format for the chunk");
        }
        byte[] data = chunk.get("data", Binary.class).getData();
        long expectedDataLength = 0;
        if (numberOfChunks > 0) {
            expectedDataLength = expectedChunkIndex + 1 == numberOfChunks ? gridFSFile.getLength() - (expectedChunkIndex * (long) gridFSFile.getChunkSize()) : gridFSFile.getChunkSize();
        }
        if (data.length != expectedDataLength) {
            throw new MongoGridFSException(format("Chunk size data length is not the expected size. " + "The size was %s for file_id: %s chunk index %s it should be " + "%s bytes.", data.length, gridFSFile.getId(), expectedChunkIndex, expectedDataLength));
        }
        return ByteBuffer.wrap(data);
    }).doOnComplete(() -> {
        if (chunkCounter.get() < numberOfChunks) {
            throw new MongoGridFSException(format("Could not find file chunk for files_id: %s at chunk index %s.", gridFSFile.getId(), chunkCounter.get()));
        }
    });
    return bufferSizeBytes == null ? byteBufferFlux : new ResizingByteBufferFlux(byteBufferFlux, bufferSizeBytes);
}
Also used : Document(org.bson.Document) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) Binary(org.bson.types.Binary) Publisher(org.reactivestreams.Publisher) MongoGridFSException(com.mongodb.MongoGridFSException) Mono(reactor.core.publisher.Mono) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) ClientSession(com.mongodb.reactivestreams.client.ClientSession) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) String.format(java.lang.String.format) GridFSDownloadPublisher(com.mongodb.reactivestreams.client.gridfs.GridFSDownloadPublisher) Flux(reactor.core.publisher.Flux) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FindPublisher(com.mongodb.reactivestreams.client.FindPublisher) Nullable(com.mongodb.lang.Nullable) Subscriber(org.reactivestreams.Subscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MongoGridFSException(com.mongodb.MongoGridFSException) Binary(org.bson.types.Binary) Document(org.bson.Document) ByteBuffer(java.nio.ByteBuffer)

Example 17 with GridFSFile

use of com.mongodb.client.gridfs.model.GridFSFile in project mongo-java-driver by mongodb.

the class GridFSPublisherCreator method createDeletePublisher.

public static Publisher<Void> createDeletePublisher(final MongoCollection<GridFSFile> filesCollection, final MongoCollection<Document> chunksCollection, @Nullable final ClientSession clientSession, final BsonValue id) {
    notNull("filesCollection", filesCollection);
    notNull("chunksCollection", chunksCollection);
    notNull("id", id);
    BsonDocument filter = new BsonDocument("_id", id);
    Publisher<DeleteResult> fileDeletePublisher;
    if (clientSession == null) {
        fileDeletePublisher = filesCollection.deleteOne(filter);
    } else {
        fileDeletePublisher = filesCollection.deleteOne(clientSession, filter);
    }
    return Mono.from(fileDeletePublisher).flatMap(deleteResult -> {
        if (deleteResult.wasAcknowledged() && deleteResult.getDeletedCount() == 0) {
            throw new MongoGridFSException(format("No file found with the ObjectId: %s", id));
        }
        if (clientSession == null) {
            return Mono.from(chunksCollection.deleteMany(new BsonDocument("files_id", id)));
        } else {
            return Mono.from(chunksCollection.deleteMany(clientSession, new BsonDocument("files_id", id)));
        }
    }).flatMap(i -> Mono.empty());
}
Also used : Document(org.bson.Document) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) Publisher(org.reactivestreams.Publisher) MongoGridFSException(com.mongodb.MongoGridFSException) Mono(reactor.core.publisher.Mono) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) ClientSession(com.mongodb.reactivestreams.client.ClientSession) BsonString(org.bson.BsonString) ByteBuffer(java.nio.ByteBuffer) String.format(java.lang.String.format) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) Bson(org.bson.conversions.Bson) GridFSUploadOptions(com.mongodb.client.gridfs.model.GridFSUploadOptions) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) UpdateResult(com.mongodb.client.result.UpdateResult) GridFSDownloadOptions(com.mongodb.client.gridfs.model.GridFSDownloadOptions) FindPublisher(com.mongodb.reactivestreams.client.FindPublisher) DeleteResult(com.mongodb.client.result.DeleteResult) GridFSFindPublisher(com.mongodb.reactivestreams.client.gridfs.GridFSFindPublisher) Nullable(com.mongodb.lang.Nullable) BsonDocument(org.bson.BsonDocument) MongoGridFSException(com.mongodb.MongoGridFSException) DeleteResult(com.mongodb.client.result.DeleteResult)

Example 18 with GridFSFile

use of com.mongodb.client.gridfs.model.GridFSFile in project spring-data-mongodb by spring-projects.

the class ReactiveGridFsTemplateTests method considersSkipLimitWhenQueryingFiles.

// DATAMONGO-765
@Test
public void considersSkipLimitWhenQueryingFiles() {
    DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
    DataBuffer buffer = bufferFactory.allocateBuffer(0);
    // 
    Flux.just(// 
    "a", // 
    "aa", // 
    "aaa", // 
    "b", // 
    "bb", // 
    "bbb", // 
    "c", // 
    "cc", // 
    "ccc", "d", "dd", // 
    "ddd").flatMap(// 
    fileName -> operations.store(Mono.just(buffer), fileName)).as(// 
    StepVerifier::create).expectNextCount(// 
    12).verifyComplete();
    PageRequest pageRequest = PageRequest.of(2, 3, Sort.Direction.ASC, "filename");
    // 
    operations.find(new Query().with(pageRequest)).map(// 
    GridFSFile::getFilename).as(// 
    StepVerifier::create).expectNext("c", "cc", // 
    "ccc").verifyComplete();
}
Also used : Document(org.bson.Document) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) RunWith(org.junit.runner.RunWith) ClassPathResource(org.springframework.core.io.ClassPathResource) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) Autowired(org.springframework.beans.factory.annotation.Autowired) BsonString(org.bson.BsonString) MongoConverter(org.springframework.data.mongodb.core.convert.MongoConverter) ByteBuffer(java.nio.ByteBuffer) Assertions(org.assertj.core.api.Assertions) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) Sort(org.springframework.data.domain.Sort) SpringRunner(org.springframework.test.context.junit4.SpringRunner) Before(org.junit.Before) Resource(org.springframework.core.io.Resource) StreamUtils(org.springframework.util.StreamUtils) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) BsonObjectId(org.bson.BsonObjectId) PageRequest(org.springframework.data.domain.PageRequest) Mono(reactor.core.publisher.Mono) IOException(java.io.IOException) Test(org.junit.Test) GridFsCriteria(org.springframework.data.mongodb.gridfs.GridFsCriteria) DataBuffer(org.springframework.core.io.buffer.DataBuffer) InputStreamReader(java.io.InputStreamReader) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) Flux(reactor.core.publisher.Flux) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) ContextConfiguration(org.springframework.test.context.ContextConfiguration) ObjectId(org.bson.types.ObjectId) ReactiveMongoDatabaseFactory(org.springframework.data.mongodb.ReactiveMongoDatabaseFactory) HexUtils(com.mongodb.internal.HexUtils) FileCopyUtils(org.springframework.util.FileCopyUtils) SimpleMongoClientDatabaseFactory(org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory) PageRequest(org.springframework.data.domain.PageRequest) Query(org.springframework.data.mongodb.core.query.Query) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 19 with GridFSFile

use of com.mongodb.client.gridfs.model.GridFSFile in project spring-data-mongodb by spring-projects.

the class GridFsTemplateIntegrationTests method convertFileToResource.

// DATAMONGO-1813
@Test
public void convertFileToResource() throws IOException {
    Document metadata = new Document("key", "value");
    ObjectId reference = operations.store(resource.getInputStream(), "foobar", metadata);
    GridFSFile file = operations.findOne(query(whereMetaData("key").is("value")));
    GridFsResource result = operations.getResource(file);
    assertThat(result.contentLength()).isEqualTo(resource.contentLength());
    assertThat(((BsonObjectId) result.getId()).getValue()).isEqualTo(reference);
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) Document(org.bson.Document) BsonObjectId(org.bson.BsonObjectId) Test(org.junit.Test)

Example 20 with GridFSFile

use of com.mongodb.client.gridfs.model.GridFSFile in project spring-data-mongodb by spring-projects.

the class GridFsResourceUnitTests method shouldThrowExceptionOnEmptyContentTypeInMetadata.

// DATAMONGO-1850
@Test
public void shouldThrowExceptionOnEmptyContentTypeInMetadata() {
    GridFSFile file = new GridFSFile(new BsonObjectId(), "foo", 0, 0, new Date(), new Document());
    GridFsResource resource = new GridFsResource(file);
    assertThatThrownBy(resource::getContentType).isInstanceOf(MongoGridFSException.class);
}
Also used : GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) Document(org.bson.Document) BsonObjectId(org.bson.BsonObjectId) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)20 Document (org.bson.Document)14 Date (java.util.Date)7 BsonObjectId (org.bson.BsonObjectId)7 MongoGridFSException (com.mongodb.MongoGridFSException)6 BsonDocument (org.bson.BsonDocument)5 ObjectId (org.bson.types.ObjectId)5 ByteBuffer (java.nio.ByteBuffer)4 GridFSFindIterable (com.mongodb.client.gridfs.GridFSFindIterable)3 GridFSDownloadOptions (com.mongodb.client.gridfs.model.GridFSDownloadOptions)3 GridFSUploadOptions (com.mongodb.client.gridfs.model.GridFSUploadOptions)3 BsonString (org.bson.BsonString)3 Test (org.junit.Test)3 Test (org.junit.jupiter.api.Test)3 Publisher (org.reactivestreams.Publisher)3 Mono (reactor.core.publisher.Mono)3 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)2 MongoDatabase (com.mongodb.client.MongoDatabase)2 GridFSBucket (com.mongodb.client.gridfs.GridFSBucket)2 Nullable (com.mongodb.lang.Nullable)2