Skip to content

Use Java 1.6+ MimeTypesFiletypeMap for SimpleStorageResource content type derivation #131

@chrisrhut

Description

@chrisrhut

Type: Feature

Is your feature request related to a problem? Please describe.
We would like to be able to take advantage of this library's WritableResource creation by just using a s3:// URL. Unfortunately there's not quite enough flexibility in determining the content type. The code in SimpleStorageNameUtils makes a call to URLConnection#guessContentTypeFromName() which is difficult to extend.

As a result, we have to manually create and upload using AmazonS3 (or TransferManager).

As of Java 1.6, there is a facility called MimeTypesFiletypeMap which is similar but somewhat easier to customize. Any META-INF/mime.types file in your app classpath can augment the default types, and provide a mapping from file extension to content type.

This proposal is to augment (if not replace) the URLConnection call to make it easier to use user-defined mappings.

Describe the solution you'd like
Something like:

static String getContentTypeFromLocation(String location) {
    String objectName = getObjectNameFromLocation(location);
    if (StringUtils.hasLength(objectName)) {
      String contentType = FileTypeMap.getDefaultFileTypeMap().getContentType(objectName);
      if (contentType == null) {
        contentType = URLConnection.guessContentTypeFromName(objectName);
      }

      return contentType;
    }
    return null;
}

Additional context
The FileTypeMap is in javax.activation API, which does seem to be included as a dependency for this project.

Assuming you agree with the general premise, I'd be happy to put together a PR for this.

Metadata

Metadata

Labels

component: s3S3 integration related issuetype: enhancementSmaller enhancement in existing integration

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions