use of com.mongodb.client.gridfs.model.GridFSFile in project mongo-java-driver by mongodb.
the class GridFSUploadStreamImpl method close.
@Override
public void close(final SingleResultCallback<Void> callback) {
notNull("callback", callback);
final SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
boolean alreadyClosed = false;
synchronized (closeAndWritingLock) {
alreadyClosed = closed;
closed = true;
}
if (alreadyClosed) {
errHandlingCallback.onResult(null, null);
return;
} else if (!getAndSetWritingLock()) {
callbackIsWritingException(errHandlingCallback);
return;
}
writeChunk(new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
if (t != null) {
releaseWritingLock();
errHandlingCallback.onResult(null, t);
} else {
GridFSFile gridFSFile = new GridFSFile(fileId, filename, lengthInBytes, chunkSizeBytes, new Date(), toHex(md5.digest()), metadata);
filesCollection.insertOne(gridFSFile, new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
buffer = null;
releaseWritingLock();
errHandlingCallback.onResult(result, t);
}
});
}
}
});
}
use of com.mongodb.client.gridfs.model.GridFSFile in project mongo-java-driver by mongodb.
the class GridFSBucketImpl method getFileByName.
private GridFSFile getFileByName(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 = find(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;
}
use of com.mongodb.client.gridfs.model.GridFSFile in project spring-data-mongodb by spring-projects.
the class GridFsResourceUnitTests method shouldReadContentTypeCorrectly.
// DATAMONGO-1850
@Test
public void shouldReadContentTypeCorrectly() {
Document metadata = new Document(GridFsResource.CONTENT_TYPE_FIELD, "text/plain");
GridFSFile file = new GridFSFile(new BsonObjectId(), "foo", 0, 0, new Date(), "foo", metadata);
GridFsResource resource = new GridFsResource(file);
assertThat(resource.getContentType()).isEqualTo("text/plain");
}
use of com.mongodb.client.gridfs.model.GridFSFile in project spring-data-mongodb by spring-projects.
the class GridFsResourceUnitTests method shouldThrowExceptionOnEmptyContentType.
// DATAMONGO-1850
@Test
public void shouldThrowExceptionOnEmptyContentType() {
GridFSFile file = new GridFSFile(new BsonObjectId(), "foo", 0, 0, new Date(), "foo", null);
GridFsResource resource = new GridFsResource(file);
assertThatThrownBy(resource::getContentType).isInstanceOf(MongoGridFSException.class);
}
use of com.mongodb.client.gridfs.model.GridFSFile in project spring-data-mongodb by spring-projects.
the class GridFsTemplate method getResources.
/*
* (non-Javadoc)
* @see org.springframework.core.io.support.ResourcePatternResolver#getResources(java.lang.String)
*/
public GridFsResource[] getResources(String locationPattern) {
if (!StringUtils.hasText(locationPattern)) {
return new GridFsResource[0];
}
AntPath path = new AntPath(locationPattern);
if (path.isPattern()) {
GridFSFindIterable files = find(query(whereFilename().regex(path.toRegex())));
List<GridFsResource> resources = new ArrayList<>();
for (GridFSFile file : files) {
resources.add(new GridFsResource(file, getGridFs().openDownloadStream(file.getFilename())));
}
return resources.toArray(new GridFsResource[0]);
}
return new GridFsResource[] { getResource(locationPattern) };
}
Aggregations