2525
2626package 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-
3128import io .swagger .v3 .oas .annotations .Parameter ;
3229import io .swagger .v3 .oas .annotations .media .Content ;
3330import io .swagger .v3 .oas .annotations .media .Schema ;
5047import org .springframework .core .io .ByteArrayResource ;
5148import org .springframework .core .io .PathResource ;
5249import 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 ;
5456import org .springframework .lang .NonNull ;
5557import org .springframework .security .access .prepost .PreAuthorize ;
5658import org .springframework .stereotype .Service ;
7072import javax .imageio .ImageIO ;
7173import javax .servlet .http .HttpServletRequest ;
7274import 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 ;
7479import java .net .URL ;
7580import java .nio .file .Files ;
7681import java .nio .file .Path ;
7782import 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