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

Set Content-Type metadata in S3 from an Entity field annotated with @MimeType #889

Merged
merged 1 commit into from
May 6, 2022
Merged
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
Expand Up @@ -121,4 +121,26 @@ public OutputStream getOutputStream() throws IOException {
public void setRange(String range) {
((RangeableResource)delegate).setRange(range);
}

/**
* Set the Content-Type value that will be specified as object metadata when saving resource to the object storage.
* @param contentType Content-Type value or null
*/
public void setContentType(String contentType) {
if (delegate instanceof SimpleStorageResource) {
((SimpleStorageResource) delegate).setContentType(contentType);
}
}

/**
* Determine the Content-Type value of the resource as saved in object storage.
* @return Content-Type value of the resource
* @throws IOException
*/
public String contentType() throws IOException {
if (delegate instanceof SimpleStorageResource) {
return ((SimpleStorageResource) delegate).contentType();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,27 @@ public class SimpleStorageResource extends AbstractResource implements WritableR

private String range;

private String contentType;

public SimpleStorageResource(S3Client amazonS3, String bucketName, String objectName,
TaskExecutor taskExecutor) {
this(amazonS3, bucketName, objectName, taskExecutor, null);
this(amazonS3, bucketName, objectName, taskExecutor, null, null);
}

public SimpleStorageResource(S3Client amazonS3, String bucketName, String objectName,
TaskExecutor taskExecutor, String versionId) {
TaskExecutor taskExecutor, String versionId) {
this(amazonS3, bucketName, objectName, taskExecutor, versionId, null);
}

public SimpleStorageResource(S3Client amazonS3, String bucketName, String objectName,
TaskExecutor taskExecutor, String versionId, String contentType) {
// this.amazonS3 = AmazonS3ProxyFactory.createProxy(amazonS3);
this.amazonS3 = amazonS3;
this.bucketName = bucketName;
this.objectName = objectName;
this.taskExecutor = taskExecutor;
this.versionId = versionId;
this.contentType = contentType;
}

@Override
Expand All @@ -96,6 +104,23 @@ public String getDescription() {
return builder.toString();
}

/**
* Set the Content-Type value that will be specified as object metadata when saving resource to the object storage.
* @param contentType Content-Type value or null
*/
public void setContentType(String contentType) {
this.contentType = contentType;
}

/**
* Determine the Content-Type value of the resource as saved in object storage.
* @return Content-Type value of the resource
* @throws IOException
*/
public String contentType() throws IOException {
return getRequiredObjectMetadata().contentType();
}

@Override
public void setRange(String range) {
this.range = range;
Expand Down Expand Up @@ -281,12 +306,16 @@ private void finishSimpleUpload() {
e);
}

SimpleStorageResource.this.amazonS3.putObject(
PutObjectRequest.builder()
.bucket(SimpleStorageResource.this.bucketName)
.key(SimpleStorageResource.this.objectName)
.contentMD5(md5Digest).build(),
RequestBody.fromBytes(content));
PutObjectRequest.Builder requestBuilder = PutObjectRequest.builder()
.bucket(SimpleStorageResource.this.bucketName)
.key(SimpleStorageResource.this.objectName)
.contentMD5(md5Digest);

if (SimpleStorageResource.this.contentType != null) {
requestBuilder.contentType(SimpleStorageResource.this.contentType);
}

SimpleStorageResource.this.amazonS3.putObject(requestBuilder.build(), RequestBody.fromBytes(content));

// Release the memory early
this.currentOutputStream = null;
Expand Down Expand Up @@ -325,10 +354,16 @@ private void finishMultiPartUpload() throws IOException {

private void initiateMultiPartIfNeeded() {
if (this.multiPartUploadResult == null) {
CreateMultipartUploadRequest.Builder requestBuilder = CreateMultipartUploadRequest.builder()
.bucket(SimpleStorageResource.this.bucketName)
.key(SimpleStorageResource.this.objectName);

if (SimpleStorageResource.this.contentType != null) {
requestBuilder.contentType(SimpleStorageResource.this.contentType);
}

this.multiPartUploadResult = SimpleStorageResource.this.amazonS3
.createMultipartUpload(CreateMultipartUploadRequest.builder()
.bucket(SimpleStorageResource.this.bucketName)
.key(SimpleStorageResource.this.objectName).build());
.createMultipartUpload(requestBuilder.build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,21 @@ public Mono<S> setContent(S entity, PropertyPath path, long contentLen, Flux<Byt
}
final S3ObjectId s3ObjectId = placementService.convert(contentId, S3ObjectId.class);

CompletableFuture<PutObjectResponse> future = asyncClient.putObject(PutObjectRequest.builder()
.bucket(s3ObjectId.getBucket())
.contentLength(contentLen)
.key(contentId.toString())
.build(),
PutObjectRequest.Builder requestBuilder = PutObjectRequest.builder()
.bucket(s3ObjectId.getBucket())
.contentLength(contentLen)
.key(contentId.toString());

Object mimeType = property.getMimeType(entity);
if (mimeType != null) {
String strMimeType = mimeType.toString();
requestBuilder.contentType(strMimeType);
}

AsyncRequestBody.fromPublisher(buffer));
CompletableFuture<PutObjectResponse> future = asyncClient.putObject(
requestBuilder.build(),
AsyncRequestBody.fromPublisher(buffer)
);

return Mono.fromFuture(future)
.map((response) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.apache.commons.logging.LogFactory;
import org.springframework.content.commons.annotations.ContentId;
import org.springframework.content.commons.annotations.ContentLength;
import org.springframework.content.commons.annotations.MimeType;
import org.springframework.content.commons.io.DeletableResource;
import org.springframework.content.commons.mappingcontext.ContentProperty;
import org.springframework.content.commons.mappingcontext.MappingContext;
Expand Down Expand Up @@ -221,6 +222,24 @@ public boolean matches(TypeDescriptor descriptor) {
});
}

private void setResourceContentTypeFromMimeType(S entity, ContentProperty property, Resource resource) {
if (resource instanceof S3StoreResource) {
Object mimeType;
if (property == null) {
mimeType = BeanUtils.getFieldWithAnnotation(entity, MimeType.class);
}
else {
mimeType = property.getMimeType(entity);
}

if (mimeType != null) {
String strMimeType = mimeType.toString();
S3StoreResource s3StoreResource = (S3StoreResource) resource;
s3StoreResource.setContentType(strMimeType);
}
}
}

@Transactional
@Override
public S setContent(S entity, InputStream content) {
Expand All @@ -243,7 +262,9 @@ public S setContent(S entity, InputStream content) {
return entity;
}

if (resource instanceof WritableResource) {
setResourceContentTypeFromMimeType(entity, null, resource);

if (resource instanceof WritableResource) {
try (OutputStream os = ((WritableResource) resource).getOutputStream()) {
IOUtils.copy(content, os);
}
Expand Down Expand Up @@ -289,6 +310,8 @@ public S setContent(S entity, PropertyPath propertyPath, InputStream content) {
return entity;
}

setResourceContentTypeFromMimeType(entity, property, resource);

if (resource instanceof WritableResource) {
try (OutputStream os = ((WritableResource) resource).getOutputStream()) {
IOUtils.copy(content, os);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
import javax.persistence.Table;
import javax.sql.DataSource;

import internal.org.springframework.content.s3.io.S3StoreResource;
import org.apache.commons.io.IOUtils;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.content.commons.annotations.ContentId;
import org.springframework.content.commons.annotations.ContentLength;
import org.springframework.content.commons.annotations.MimeType;
import org.springframework.content.commons.io.DeletableResource;
import org.springframework.content.commons.io.RangeableResource;
import org.springframework.content.commons.property.PropertyPath;
Expand Down Expand Up @@ -325,6 +327,8 @@ public class S3StoreIT {

BeforeEach(() -> {
entity = new TestEntity();
entity.setContentType("text/plain");
entity.setRenditionContentType("text/html");
entity = repo.save(entity);

store.setContent(entity, new ByteArrayInputStream("Hello Spring Content World!".getBytes()));
Expand Down Expand Up @@ -355,6 +359,19 @@ public class S3StoreIT {
Assert.assertEquals(entity.getRenditionLen(), 40L);
});


It("should set Content-Type of stored content to value from field annotated with @MimeType", () -> {
// content
S3StoreResource resource = (S3StoreResource) store.getResource(entity);
assertThat(resource.contentType(), is(notNullValue()));
assertThat(resource.contentType(), is(entity.getContentType()));

//rendition
S3StoreResource renditionResource = (S3StoreResource) store.getResource(entity, PropertyPath.from("rendition"));
assertThat(renditionResource.contentType(), is(notNullValue()));
assertThat(renditionResource.contentType(), is(entity.getRenditionContentType()));
});

Context("when content is updated", () -> {
BeforeEach(() ->{
store.setContent(entity, new ByteArrayInputStream("Hello Updated Spring Content World!".getBytes()));
Expand Down Expand Up @@ -566,12 +583,18 @@ public static class TestEntity {
@ContentLength
private long contentLen;

@MimeType
private String contentType;

@ContentId
private String renditionId;

@ContentLength
private long renditionLen;

@MimeType
private String renditionContentType;

public TestEntity(String contentId) {
this.contentId = new String(contentId);
}
Expand Down