Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding basic GridFS support #1962

Open
wants to merge 1 commit into
base: 4.7.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2017-2023 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.data.mongodb.client;

import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.mongodb.client.gridfs.model.GridFSUploadOptions;
import com.mongodb.client.model.Filters;
import io.micronaut.context.annotation.Bean;
import io.micronaut.data.exceptions.DataAccessException;
import org.bson.Document;
import org.bson.types.ObjectId;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;

/**
* Client for a given (or default) GridFS bucket.
*/
@Bean
public class GridFSClient {

/** Bucket backing the client instance. */
private final GridFSBucket bucket;

public GridFSClient(GridFSBucket bucket) {
this.bucket = bucket;
}

/**
* Upload a file to GridFS bucket.
*
* @param path Path to the temporary file
* @param fileName Filename for the uploaded file
* @param metadata Metadata for the uploaded file
* @return ObjectId for the uploaded file
*/
public ObjectId uploadFile(Path path, String fileName, Map<String, String> metadata) {

Document document = new Document();
if (metadata != null) {
document.putAll(metadata);
}

try (InputStream streamToUploadFrom = new FileInputStream(path.toFile())) {
GridFSUploadOptions options = new GridFSUploadOptions()
.metadata(document);
return bucket.uploadFromStream(fileName, streamToUploadFrom, options);
} catch (IOException e) {
throw new DataAccessException(e.getMessage(), e);
}
}

/**
* Download a file for given id.
*
* @param id ObjectId of the given file
* @return Optional Path for the downloaded file
*/
public Optional<Path> downloadFile(ObjectId id) {
try {
GridFSFile file = bucket.find(Filters.eq(id)).first();
if (file != null) {
Path tempFile = Files.createTempFile("gridfs-" + id + "-", ".tmp");
FileOutputStream destination = new FileOutputStream(tempFile.toFile());
bucket.downloadToStream(id, destination);
destination.flush();
return Optional.of(tempFile);
} else {
return Optional.empty();
}
} catch (IOException e) {
throw new DataAccessException(e.getMessage(), e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2017-2023 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.data.mongodb.conf;

import com.mongodb.ReadConcern;
import com.mongodb.ReadConcernLevel;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import io.micronaut.context.annotation.ConfigurationProperties;

/**
* Top level configuration for GridFS.
*/
@ConfigurationProperties(MongoDataConfiguration.PREFIX + GridFSConfiguration.GRID_FS)
public final class GridFSConfiguration {

public static final String GRID_FS = ".gridfs";
public static final String DEFAULT_BUCKET_NAME = "files";
public static final int DEFAULT_CHUNK_SIZE = 1048576;

/**
* Chunk size for the bucket. Default is 1 MB.
*/
private int chunkSize = DEFAULT_CHUNK_SIZE;

/**
* Database for the bucket. If not specified, the value of micronaut.data.mongodb.gridfs.database is used.
*/
private String database;

/** Bucket Name. */
private String bucketName;

private ReadConcernLevel readConcern;
private WriteConcernOptions writeConcern;
private ReadPreferenceOptions readPreference;

public void setReadConcern(ReadConcernLevel readConcern) {
this.readConcern = readConcern;
}

public void setWriteConcern(WriteConcernOptions writeConcern) {
this.writeConcern = writeConcern;
}

public void setReadPreference(ReadPreferenceOptions readPreference) {
this.readPreference = readPreference;
}

public ReadConcern getReadConcern() {
return (this.readConcern != null) ? new ReadConcern(this.readConcern) : null;
}

public WriteConcern getWriteConcern() {
return (this.writeConcern != null) ? this.writeConcern.getValue() : null;
}

public ReadPreference getReadPreference() {
return (this.readPreference != null) ? this.readPreference.getValue() : null;
}

public int getChunkSize() {
return chunkSize;
}

public void setChunkSize(int chunkSize) {
this.chunkSize = chunkSize;
}

public String getDatabase() {
return database;
}

public void setDatabase(String database) {
this.database = database;
}

public String getBucketName() {
return bucketName;
}

public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2017-2023 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.data.mongodb.conf;

import com.mongodb.ReadConcern;
import com.mongodb.ReadConcernLevel;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import io.micronaut.context.annotation.EachProperty;
import io.micronaut.context.annotation.Parameter;

import javax.validation.constraints.NotBlank;

/**
* Configuration for class GriDFS bucket.
*/
@EachProperty(value = MongoDataConfiguration.PREFIX + GridFSConfiguration.GRID_FS + NamedBucketConfiguration.BUCKETS)
public final class NamedBucketConfiguration {

public static final String BUCKETS = ".buckets";

/**
* Required. Name of the bucket
*/
@NotBlank
private String name;

/**
* Database for the bucket. If not specified, the value of micronaut.data.mongodb.gridfs.database is used.
*/
private String database;

/**
* Chunk size for the bucket. Default is 1 MB
*/
private int chunkSize;

private ReadConcernLevel readConcern;
private WriteConcernOptions writeConcern;
private ReadPreferenceOptions readPreference;

public NamedBucketConfiguration(@Parameter String name) {
this.name = name;
}

public String getName() {
return name;
}

public int getChunkSize() {
return chunkSize;
}

public void setChunkSize(int chunkSize) {
this.chunkSize = chunkSize;
}

public String getDatabase() {
return database;
}

public void setReadConcern(ReadConcernLevel readConcern) {
this.readConcern = readConcern;
}

public void setWriteConcern(WriteConcernOptions writeConcern) {
this.writeConcern = writeConcern;
}

public void setReadPreference(ReadPreferenceOptions readPreference) {
this.readPreference = readPreference;
}

public ReadConcern getReadConcern() {
return (this.readConcern != null) ? new ReadConcern(this.readConcern) : null;
}

public WriteConcern getWriteConcern() {
return (this.writeConcern != null) ? this.writeConcern.getValue() : null;
}

public ReadPreference getReadPreference() {
return (this.readPreference != null) ? this.readPreference.getValue() : null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2017-2023 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.data.mongodb.conf;

import com.mongodb.ReadPreference;

/** Enum for ReadPreference value. */
public enum ReadPreferenceOptions {
primary(ReadPreference.primary()),
secondary(ReadPreference.secondary()),
secondary_preferred(ReadPreference.secondaryPreferred()),
primary_preferred(ReadPreference.primaryPreferred()),
nearest(ReadPreference.nearest());

private final ReadPreference value;

ReadPreferenceOptions(ReadPreference value) {
this.value = value;
}

public ReadPreference getValue() {
return this.value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2017-2023 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.data.mongodb.conf;

import com.mongodb.WriteConcern;

/**
* Enum for WriteConcern value.
*/
public enum WriteConcernOptions {
w1(WriteConcern.W1),
w2(WriteConcern.W2),
w3(WriteConcern.W3),
acknowledged(WriteConcern.ACKNOWLEDGED),
unacknowledged(WriteConcern.UNACKNOWLEDGED),
journaled(WriteConcern.JOURNALED),
majority(WriteConcern.MAJORITY);

private final WriteConcern value;

WriteConcernOptions(WriteConcern value) {
this.value = value;
}

public WriteConcern getValue() {
return this.value;
}
}
Loading