Skip to content

Commit bb8af9e

Browse files
committed
refine getMimetype
1 parent b607016 commit bb8af9e

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

src/main/java/com/aliyun/oss/internal/Mimetypes.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,11 @@ public void loadMimetypes(InputStream is) throws IOException {
8888
}
8989
}
9090
}
91-
91+
9292
public String getMimetype(String fileName) {
93-
int lastPeriodIndex = fileName.lastIndexOf(".");
94-
if (lastPeriodIndex > 0 && lastPeriodIndex + 1 < fileName.length()) {
95-
String ext = fileName.substring(lastPeriodIndex + 1).toLowerCase();
96-
if (extensionToMimetypeMap.keySet().contains(ext)) {
97-
String mimetype = (String) extensionToMimetypeMap.get(ext);
98-
return mimetype;
99-
}
93+
String mimeType = getMimetypeByExt(fileName);
94+
if (mimeType != null) {
95+
return mimeType;
10096
}
10197
return DEFAULT_MIMETYPE;
10298
}
@@ -110,26 +106,28 @@ public String getMimetype(File file, String key) {
110106
}
111107

112108
public String getMimetype(String primaryObject, String secondaryObject) {
113-
// primary
114-
int lastPeriodIndex = primaryObject.lastIndexOf(".");
115-
if (lastPeriodIndex > 0 && lastPeriodIndex + 1 < primaryObject.length()) {
116-
String ext = primaryObject.substring(lastPeriodIndex + 1).toLowerCase();
117-
if (extensionToMimetypeMap.keySet().contains(ext)) {
118-
String mimetype = (String) extensionToMimetypeMap.get(ext);
119-
return mimetype;
120-
}
109+
String mimeType = getMimetypeByExt(primaryObject);
110+
if (mimeType != null) {
111+
return mimeType;
112+
}
113+
114+
mimeType = getMimetypeByExt(secondaryObject);
115+
if (mimeType != null) {
116+
return mimeType;
121117
}
122-
// secondary
123-
lastPeriodIndex = secondaryObject.lastIndexOf(".");
124-
if (lastPeriodIndex > 0 && lastPeriodIndex + 1 < secondaryObject.length()) {
125-
String ext = secondaryObject.substring(lastPeriodIndex + 1).toLowerCase();
118+
119+
return DEFAULT_MIMETYPE;
120+
}
121+
122+
private String getMimetypeByExt(String fileName) {
123+
int lastPeriodIndex = fileName.lastIndexOf(".");
124+
if (lastPeriodIndex > 0 && lastPeriodIndex + 1 < fileName.length()) {
125+
String ext = fileName.substring(lastPeriodIndex + 1).toLowerCase();
126126
if (extensionToMimetypeMap.keySet().contains(ext)) {
127127
String mimetype = (String) extensionToMimetypeMap.get(ext);
128128
return mimetype;
129129
}
130130
}
131-
// default
132-
return DEFAULT_MIMETYPE;
131+
return null;
133132
}
134-
135133
}

0 commit comments

Comments
 (0)