Search in sources :

Example 1 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(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);
                    }
                });
            }
        }
    });
}
Also used : GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) SingleResultCallback(com.mongodb.async.SingleResultCallback) Date(java.util.Date)

Example 2 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(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;
}
Also used : GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) MongoGridFSException(com.mongodb.MongoGridFSException) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument)

Example 3 with GridFSFile

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");
}
Also used : GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) Document(org.bson.Document) BsonObjectId(org.bson.BsonObjectId) Date(java.util.Date) Test(org.junit.Test)

Example 4 with GridFSFile

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);
}
Also used : GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) BsonObjectId(org.bson.BsonObjectId) Date(java.util.Date) Test(org.junit.Test)

Example 5 with GridFSFile

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) };
}
Also used : GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) ArrayList(java.util.ArrayList) GridFSFindIterable(com.mongodb.client.gridfs.GridFSFindIterable)

Aggregations

GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)17 Document (org.bson.Document)11 MongoGridFSException (com.mongodb.MongoGridFSException)6 Date (java.util.Date)6 BsonDocument (org.bson.BsonDocument)5 BsonObjectId (org.bson.BsonObjectId)4 Test (org.junit.Test)4 GridFSDownloadOptions (com.mongodb.client.gridfs.model.GridFSDownloadOptions)3 GridFSUploadOptions (com.mongodb.client.gridfs.model.GridFSUploadOptions)3 ByteBuffer (java.nio.ByteBuffer)3 ObjectId (org.bson.types.ObjectId)3 Publisher (org.reactivestreams.Publisher)3 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)2 MongoDatabase (com.mongodb.client.MongoDatabase)2 GridFSBucket (com.mongodb.client.gridfs.GridFSBucket)2 GridFSFindIterable (com.mongodb.client.gridfs.GridFSFindIterable)2 Nullable (com.mongodb.lang.Nullable)2 ClientSession (com.mongodb.reactivestreams.client.ClientSession)2 FindPublisher (com.mongodb.reactivestreams.client.FindPublisher)2 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)2