Search in sources :

Example 11 with GridFSFile

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

the class GridFsTemplateIntegrationTests method writesMetadataCorrectly.

// @Test // DATAMONGO-2392
// public void storesAndFindsByUUID() throws IOException {
// 
// UUID uuid = UUID.randomUUID();
// 
// GridFSFile fs = new GridFSFile(new BsonObjectId(new ObjectId(uuid.to))
// GridFSInputFile in = fs.createFile(resource.getInputStream(), "gridfs.xml");
// 
// in.put("_id", uuid);
// in.put("contentType", "application/octet-stream");
// in.save();
// 
// GridFSFile file = operations.findOne(query(where("_id").is(uuid)));
// GridFsResource resource = operations.getResource(file);
// 
// assertThat(resource.exists()).isTrue();
// }
// DATAMONGO-6
@Test
public void writesMetadataCorrectly() throws IOException {
    Document metadata = new Document("key", "value");
    ObjectId reference = operations.store(resource.getInputStream(), "foo.xml", metadata);
    List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<>();
    GridFSFindIterable result = operations.find(query(whereMetaData("key").is("value")));
    result.into(files);
    assertThat(files).hasSize(1);
    assertThat(((BsonObjectId) files.get(0).getId()).getValue()).isEqualTo(reference);
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) ArrayList(java.util.ArrayList) GridFSFindIterable(com.mongodb.client.gridfs.GridFSFindIterable) Document(org.bson.Document) BsonObjectId(org.bson.BsonObjectId) Test(org.junit.Test)

Example 12 with GridFSFile

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

the class GridFSDownloadStreamImpl method getGridFSFile.

@Override
public void getGridFSFile(final SingleResultCallback<GridFSFile> callback) {
    notNull("callback", callback);
    final SingleResultCallback<GridFSFile> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
    if (hasFileInfo()) {
        errHandlingCallback.onResult(fileInfo, null);
        return;
    }
    if (!tryGetReadingLock(errHandlingCallback)) {
        return;
    }
    fileInfoIterable.first(new SingleResultCallback<GridFSFile>() {

        @Override
        public void onResult(final GridFSFile result, final Throwable t) {
            releaseReadingLock();
            if (t != null) {
                errHandlingCallback.onResult(null, t);
            } else if (result == null) {
                errHandlingCallback.onResult(null, new MongoGridFSException("File not found"));
            } else {
                fileInfo = result;
                numberOfChunks = (int) Math.ceil((double) fileInfo.getLength() / fileInfo.getChunkSize());
                errHandlingCallback.onResult(result, null);
            }
        }
    });
}
Also used : GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) MongoGridFSException(com.mongodb.MongoGridFSException)

Example 13 with GridFSFile

use of com.mongodb.client.gridfs.model.GridFSFile in project georocket by georocket.

the class MongoDBStoreTest method validateAfterStoreAdd.

@Override
protected void validateAfterStoreAdd(TestContext context, Vertx vertx, String path, Handler<AsyncResult<Void>> handler) {
    vertx.executeBlocking(f -> {
        try (MongoClient client = new MongoClient(mongoConnector.serverAddress)) {
            MongoDatabase db = client.getDatabase(MongoDBTestConnector.MONGODB_DBNAME);
            GridFSBucket gridFS = GridFSBuckets.create(db);
            GridFSFindIterable files = gridFS.find();
            GridFSFile file = files.first();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            gridFS.downloadToStream(file.getFilename(), baos);
            String contents = new String(baos.toByteArray(), StandardCharsets.UTF_8);
            context.assertEquals(CHUNK_CONTENT, contents);
        }
        f.complete();
    }, handler);
}
Also used : GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) MongoClient(com.mongodb.MongoClient) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) GridFSFindIterable(com.mongodb.client.gridfs.GridFSFindIterable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 14 with GridFSFile

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

the class GridFSUploadStreamImpl method close.

@Override
public void close() {
    synchronized (closeLock) {
        if (closed) {
            return;
        }
        closed = true;
    }
    writeChunk();
    GridFSFile gridFSFile = new GridFSFile(fileId, filename, lengthInBytes, chunkSizeBytes, new Date(), metadata);
    if (clientSession != null) {
        filesCollection.insertOne(clientSession, gridFSFile);
    } else {
        filesCollection.insertOne(gridFSFile);
    }
    buffer = null;
}
Also used : GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) Date(java.util.Date)

Example 15 with GridFSFile

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

the class GridFSBucketImpl method getFileByName.

private GridFSFile getFileByName(@Nullable final ClientSession clientSession, final String filename, final GridFSDownloadOptions options) {
    int revision = options.getRevision();
    int skip;
    int sort;
    if (revision >= 0) {
        skip = revision;
        sort = 1;
    } else {
        skip = (-revision) - 1;
        sort = -1;
    }
    GridFSFile fileInfo = createGridFSFindIterable(clientSession, new Document("filename", filename)).skip(skip).sort(new Document("uploadDate", sort)).first();
    if (fileInfo == null) {
        throw new MongoGridFSException(format("No file found with the filename: %s and revision: %s", filename, revision));
    }
    return fileInfo;
}
Also used : GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) MongoGridFSException(com.mongodb.MongoGridFSException) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument)

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