Search in sources :

Example 1 with MongoGridFSException

use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.

the class GridFSBucketImpl method readAndWriteInputStream.

private void readAndWriteInputStream(final AsyncInputStream source, final GridFSUploadStream uploadStream, final ByteBuffer buffer, final SingleResultCallback<Void> callback) {
    buffer.clear();
    source.read(buffer, new SingleResultCallback<Integer>() {

        @Override
        public void onResult(final Integer result, final Throwable t) {
            if (t != null) {
                if (t instanceof IOException) {
                    uploadStream.abort(new SingleResultCallback<Void>() {

                        @Override
                        public void onResult(final Void result, final Throwable abortException) {
                            if (abortException != null) {
                                callback.onResult(null, abortException);
                            } else {
                                callback.onResult(null, new MongoGridFSException("IOException when reading from the InputStream", t));
                            }
                        }
                    });
                } else {
                    callback.onResult(null, t);
                }
            } else if (result > 0) {
                buffer.flip();
                uploadStream.write(buffer, new SingleResultCallback<Integer>() {

                    @Override
                    public void onResult(final Integer result, final Throwable t) {
                        if (t != null) {
                            callback.onResult(null, t);
                        } else {
                            readAndWriteInputStream(source, uploadStream, buffer, callback);
                        }
                    }
                });
            } else {
                uploadStream.close(callback);
            }
        }
    });
}
Also used : MongoGridFSException(com.mongodb.MongoGridFSException) SingleResultCallback(com.mongodb.async.SingleResultCallback) IOException(java.io.IOException)

Example 2 with MongoGridFSException

use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.

the class GridFSBucketImpl method rename.

@Override
public void rename(final BsonValue id, final String newFilename, final SingleResultCallback<Void> callback) {
    notNull("id", id);
    notNull("newFilename", newFilename);
    notNull("callback", callback);
    final SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
    filesCollection.updateOne(new BsonDocument("_id", id), new BsonDocument("$set", new BsonDocument("filename", new BsonString(newFilename))), new SingleResultCallback<UpdateResult>() {

        @Override
        public void onResult(final UpdateResult result, final Throwable t) {
            if (t != null) {
                errHandlingCallback.onResult(null, t);
            } else if (result.wasAcknowledged() && result.getMatchedCount() == 0) {
                errHandlingCallback.onResult(null, new MongoGridFSException(format("No file found with the ObjectId: %s", id)));
            } else {
                errHandlingCallback.onResult(null, null);
            }
        }
    });
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) MongoGridFSException(com.mongodb.MongoGridFSException) UpdateResult(com.mongodb.client.result.UpdateResult)

Example 3 with MongoGridFSException

use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.

the class GridFSUploadStreamImpl method writeChunk.

private void writeChunk(final SingleResultCallback<Void> callback) {
    if (md5 == null) {
        callback.onResult(null, new MongoGridFSException("No MD5 message digest available, cannot upload file"));
    } else if (bufferOffset > 0) {
        chunksCollection.insertOne(new Document("files_id", fileId).append("n", chunkIndex).append("data", getData()), new SingleResultCallback<Void>() {

            @Override
            public void onResult(final Void result, final Throwable t) {
                if (t != null) {
                    callback.onResult(null, t);
                } else {
                    md5.update(buffer);
                    chunkIndex++;
                    bufferOffset = 0;
                    callback.onResult(null, null);
                }
            }
        });
    } else {
        callback.onResult(null, null);
    }
}
Also used : MongoGridFSException(com.mongodb.MongoGridFSException) SingleResultCallback(com.mongodb.async.SingleResultCallback) Document(org.bson.Document)

Example 4 with MongoGridFSException

use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.

the class GridFSBucketImpl method delete.

@Override
public void delete(final BsonValue id) {
    DeleteResult result = filesCollection.deleteOne(new BsonDocument("_id", id));
    chunksCollection.deleteMany(new BsonDocument("files_id", id));
    if (result.wasAcknowledged() && result.getDeletedCount() == 0) {
        throw new MongoGridFSException(format("No file found with the id: %s", id));
    }
}
Also used : BsonDocument(org.bson.BsonDocument) MongoGridFSException(com.mongodb.MongoGridFSException) DeleteResult(com.mongodb.client.result.DeleteResult)

Example 5 with MongoGridFSException

use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.

the class GridFSBucketImpl method uploadFromStream.

@Override
public void uploadFromStream(final BsonValue id, final String filename, final InputStream source, final GridFSUploadOptions options) {
    GridFSUploadStream uploadStream = openUploadStream(id, filename, options);
    int chunkSize = options.getChunkSizeBytes() == null ? chunkSizeBytes : options.getChunkSizeBytes();
    byte[] buffer = new byte[chunkSize];
    int len;
    try {
        while ((len = source.read(buffer)) != -1) {
            uploadStream.write(buffer, 0, len);
        }
        uploadStream.close();
    } catch (IOException e) {
        uploadStream.abort();
        throw new MongoGridFSException("IOException when reading from the InputStream", e);
    }
}
Also used : MongoGridFSException(com.mongodb.MongoGridFSException) IOException(java.io.IOException)

Aggregations

MongoGridFSException (com.mongodb.MongoGridFSException)22 BsonDocument (org.bson.BsonDocument)11 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)6 IOException (java.io.IOException)6 Document (org.bson.Document)6 BsonString (org.bson.BsonString)5 DeleteResult (com.mongodb.client.result.DeleteResult)4 SingleResultCallback (com.mongodb.async.SingleResultCallback)3 UpdateResult (com.mongodb.client.result.UpdateResult)3 ByteBuffer (java.nio.ByteBuffer)3 BsonValue (org.bson.BsonValue)3 Binary (org.bson.types.Binary)3 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)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 String.format (java.lang.String.format)2 BsonArray (org.bson.BsonArray)2 BsonObjectId (org.bson.BsonObjectId)2