Skip to content

Commit 4cccdca

Browse files
authored
Merge pull request #348 from georchestra/fix-attachmentsapi-behaviour
fix: use application/octet-stream as default mimetype
2 parents 04e6f2c + 8f8a2ca commit 4cccdca

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

services/src/main/java/org/fao/geonet/api/records/attachments/AttachmentsApi.java

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525

2626
package org.fao.geonet.api.records.attachments;
2727

28-
import static org.fao.geonet.api.ApiParams.API_CLASS_RECORD_OPS;
29-
import static org.fao.geonet.api.ApiParams.API_CLASS_RECORD_TAG;
30-
3128
import io.swagger.v3.oas.annotations.Parameter;
3229
import io.swagger.v3.oas.annotations.media.Content;
3330
import io.swagger.v3.oas.annotations.media.Schema;
@@ -50,7 +47,12 @@
5047
import org.springframework.core.io.ByteArrayResource;
5148
import org.springframework.core.io.PathResource;
5249
import org.springframework.core.io.Resource;
53-
import org.springframework.http.*;
50+
import org.springframework.http.CacheControl;
51+
import org.springframework.http.ContentDisposition;
52+
import org.springframework.http.HttpHeaders;
53+
import org.springframework.http.HttpStatus;
54+
import org.springframework.http.MediaType;
55+
import org.springframework.http.ResponseEntity;
5456
import org.springframework.lang.NonNull;
5557
import org.springframework.security.access.prepost.PreAuthorize;
5658
import org.springframework.stereotype.Service;
@@ -70,19 +72,25 @@
7072
import javax.imageio.ImageIO;
7173
import javax.servlet.http.HttpServletRequest;
7274
import java.awt.image.BufferedImage;
73-
import java.io.*;
75+
import java.io.ByteArrayOutputStream;
76+
import java.io.FilterInputStream;
77+
import java.io.IOException;
78+
import java.io.InputStream;
7479
import java.net.URL;
7580
import java.nio.file.Files;
7681
import java.nio.file.Path;
7782
import java.util.List;
7883

84+
import static org.fao.geonet.api.ApiParams.API_CLASS_RECORD_OPS;
85+
import static org.fao.geonet.api.ApiParams.API_CLASS_RECORD_TAG;
86+
7987
/**
8088
* Metadata resource related operations.
8189
* <p>
8290
* Load the store with id 'resourceStore'.
8391
*/
84-
@EnableWebMvc
8592
@Service
93+
@EnableWebMvc
8694
@RequestMapping(value = {"/{portal}/api/records/{metadataUuid}/attachments"})
8795
@Tag(name = API_CLASS_RECORD_TAG,
8896
description = API_CLASS_RECORD_OPS)
@@ -103,36 +111,34 @@ public AttachmentsApi(Store store) {
103111
/**
104112
* Based on the file content or file extension return an appropiate mime type.
105113
*
106-
* @return The mime type or application/{{file_extension}} if none found.
114+
* @return The mime type or application/octet-stream if none found.
107115
*/
108116
public static String getFileContentType(Path file) throws IOException {
109-
String contentType = Files.probeContentType(file);
110-
if (contentType == null) {
111-
String ext = com.google.common.io.Files.getFileExtension(file.getFileName().toString()).toLowerCase();
112-
switch (ext) {
113-
case "png":
114-
case "gif":
115-
case "bmp":
116-
contentType = "image/" + ext;
117-
break;
118-
case "tif":
119-
case "tiff":
120-
contentType = "image/tiff";
121-
break;
122-
case "jpg":
123-
case "jpeg":
124-
contentType = "image/jpeg";
125-
break;
126-
case "txt":
127-
contentType = "text/plain";
128-
break;
129-
case "htm":
130-
case "html":
131-
contentType = "text/html";
132-
break;
133-
default:
134-
contentType = "application/" + ext;
135-
}
117+
String contentType;
118+
String ext = com.google.common.io.Files.getFileExtension(file.getFileName().toString()).toLowerCase();
119+
switch (ext) {
120+
case "png":
121+
case "gif":
122+
case "bmp":
123+
contentType = "image/" + ext;
124+
break;
125+
case "tif":
126+
case "tiff":
127+
contentType = "image/tiff";
128+
break;
129+
case "jpg":
130+
case "jpeg":
131+
contentType = "image/jpeg";
132+
break;
133+
case "txt":
134+
contentType = "text/plain";
135+
break;
136+
case "htm":
137+
case "html":
138+
contentType = "text/html";
139+
break;
140+
default:
141+
contentType = "application/octet-stream";
136142
}
137143
return contentType;
138144
}

0 commit comments

Comments
 (0)