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);
}
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);
}
}
});
}
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);
}
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;
}
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;
}
Aggregations