Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Implemented FileTypeDetector to return contentType from Metadata #91

Open
wants to merge 1 commit into
base: master
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
21 changes: 21 additions & 0 deletions src/main/java/com/upplication/s3fs/S3FileTypeDetector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.upplication.s3fs;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.spi.FileTypeDetector;

import com.amazonaws.services.s3.model.ObjectMetadata;
import com.upplication.s3fs.util.S3Utils;

public class S3FileTypeDetector extends FileTypeDetector {
@Override
public String probeContentType(Path path) throws IOException {
if (path instanceof S3Path) {
S3Path s3Path = (S3Path) path;
ObjectMetadata metadata = S3Utils.getObjectMetadata(s3Path);
if (metadata != null)
return metadata.getContentType();
}
return null;
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/upplication/s3fs/util/S3Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@
*/
public class S3Utils {

public static ObjectMetadata getObjectMetadata(S3Path s3Path) {
String key = s3Path.getKey();
String bucketName = s3Path.getFileStore().name();
AmazonS3 client = s3Path.getFileSystem().getClient();
// try to find the element with the current key (maybe with end slash or maybe not.)
try {
return client.getObjectMetadata(bucketName, key);
} catch (AmazonS3Exception e) {
if (e.getStatusCode() != 404)
throw e;
}
return null;
}

/**
* Get the {@link S3ObjectSummary} that represent this Path or her first child if this path not exists
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.upplication.s3fs.S3FileTypeDetector