Skip to content

Instantly share code, notes, and snippets.

@telenieko
Created January 18, 2019 08:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telenieko/2250095c12ccbf44dd6c14f9ae4c98a6 to your computer and use it in GitHub Desktop.
Save telenieko/2250095c12ccbf44dd6c14f9ae4c98a6 to your computer and use it in GitHub Desktop.
Sample of a CUBA Storage for Google Cloud Storage - very similar to the supplied AWS S3 one
package com.sample;
import com.google.cloud.storage.*;
import com.haulmont.bali.util.Preconditions;
import com.haulmont.cuba.core.app.FileStorageAPI;
import com.haulmont.cuba.core.entity.FileDescriptor;
import com.haulmont.cuba.core.global.Configuration;
import com.haulmont.cuba.core.global.FileStorageException;
import com.metanube.metai4.google.service.GoogleConfig;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import static com.haulmont.bali.util.Preconditions.checkNotNullArgument;
public class GoogleCloudFileStorage implements FileStorageAPI {
@Inject protected Configuration configuration;
GoogleConfig googleConfig;
Storage storage;
public GoogleCloudFileStorage() {
storage = StorageOptions.getDefaultInstance().getService();
}
@PostConstruct
public void init() {
googleConfig = configuration.getConfig(GoogleConfig.class);
}
@Override
public long saveStream(FileDescriptor fileDescr, InputStream inputStream) throws FileStorageException {
Preconditions.checkNotNullArgument(fileDescr.getSize());
BlobId blobId = getBlobId(fileDescr);
Blob blob = storage.create(BlobInfo.newBuilder(blobId).build(), inputStream);
return blob.getSize();
}
@Override
public void saveFile(FileDescriptor fileDescr, byte[] data) throws FileStorageException {
checkNotNullArgument(data, "File content is null");
BlobId blobId = getBlobId(fileDescr);
storage.create(BlobInfo.newBuilder(blobId).build(), data);
}
@Override
public void removeFile(FileDescriptor fileDescr) throws FileStorageException {
storage.delete(getBlobId(fileDescr));
}
@Override
public InputStream openStream(FileDescriptor fileDescr) throws FileStorageException {
return new ByteArrayInputStream(getBlob(fileDescr).getContent());
}
@Override
public byte[] loadFile(FileDescriptor fileDescr) throws FileStorageException {
Blob blob = getBlob(fileDescr);
return blob.getContent();
}
@Override
public boolean fileExists(FileDescriptor fileDescr) throws FileStorageException {
return getBlob(fileDescr) != null;
}
protected String resolveFileName(FileDescriptor fileDescr) {
return getStorageDir(fileDescr.getCreateDate()) + "/" + getFileName(fileDescr);
}
/**
* INTERNAL. Don't use in application code.
*/
protected Blob getBlob(FileDescriptor fileDescriptor) {
return getBlob(getBlobId(fileDescriptor));
}
protected Blob getBlob(BlobId blobId) {
return storage.get(blobId);
}
protected BlobId getBlobId(FileDescriptor fileDescr) {
return BlobId.of(googleConfig.getFileStorageBucket(), resolveFileName(fileDescr));
}
protected String getStorageDir(Date createDate) {
Calendar cal = Calendar.getInstance();
cal.setTime(createDate);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
return String.format("%d/%s/%s",
year, StringUtils.leftPad(String.valueOf(month), 2, '0'), StringUtils.leftPad(String.valueOf(day), 2, '0'));
}
protected String getFileName(FileDescriptor fileDescriptor) {
if (StringUtils.isNotBlank(fileDescriptor.getExtension())) {
return fileDescriptor.getId().toString() + "." + fileDescriptor.getExtension();
} else {
return fileDescriptor.getId().toString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment