|
1 | 1 | /* |
2 | 2 | * ============================================================================= |
3 | | - * === Copyright (C) 2001-2024 Food and Agriculture Organization of the |
| 3 | + * === Copyright (C) 2001-2025 Food and Agriculture Organization of the |
4 | 4 | * === United Nations (FAO-UN), United Nations World Food Programme (WFP) |
5 | 5 | * === and United Nations Environment Programme (UNEP) |
6 | 6 | * === |
|
46 | 46 | import org.fao.geonet.events.history.AttachmentDeletedEvent; |
47 | 47 | import org.fao.geonet.util.ImageUtil; |
48 | 48 | import org.springframework.context.ApplicationContext; |
| 49 | +import org.springframework.http.HttpHeaders; |
49 | 50 | import org.springframework.http.HttpStatus; |
50 | 51 | import org.springframework.http.MediaType; |
51 | 52 | import org.springframework.security.access.prepost.PreAuthorize; |
|
67 | 68 | import javax.servlet.http.HttpServletRequest; |
68 | 69 | import javax.servlet.http.HttpServletResponse; |
69 | 70 | import java.awt.image.BufferedImage; |
| 71 | +import java.io.ByteArrayOutputStream; |
70 | 72 | import java.io.IOException; |
71 | 73 | import java.io.InputStream; |
72 | 74 | import java.net.URL; |
@@ -275,22 +277,46 @@ public void getResource( |
275 | 277 |
|
276 | 278 | ApiUtils.canViewRecord(metadataUuid, request); |
277 | 279 |
|
278 | | - response.setHeader("Content-Disposition", "inline; filename=\"" + file.getMetadata().getFilename() + "\""); |
279 | | - response.setHeader("Cache-Control", "no-cache"); |
| 280 | + String originalFilename = file.getMetadata().getFilename(); |
280 | 281 | String contentType = getFileContentType(file.getPath()); |
281 | | - response.setHeader("Content-Type", contentType); |
| 282 | + String dispositionFilename = originalFilename; |
282 | 283 |
|
| 284 | + // If the image is being resized, always return as PNG and update |
| 285 | + // filename and content-type accordingly |
283 | 286 | if (contentType.startsWith("image/") && size != null) { |
284 | 287 | if (size >= MIN_IMAGE_SIZE && size <= MAX_IMAGE_SIZE) { |
| 288 | + // Set content type to PNG |
| 289 | + contentType = "image/png"; |
| 290 | + // Change file extension to .png |
| 291 | + int dotIdx = originalFilename.lastIndexOf('.'); |
| 292 | + if (dotIdx > 0) { |
| 293 | + dispositionFilename = originalFilename.substring(0, dotIdx) + ".png"; |
| 294 | + } else { |
| 295 | + dispositionFilename = originalFilename + ".png"; |
| 296 | + } |
| 297 | + response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + dispositionFilename + "\""); |
| 298 | + response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache"); |
| 299 | + response.setHeader(HttpHeaders.CONTENT_TYPE, contentType); |
| 300 | + |
| 301 | + // Read, resize, and write the image as PNG, and set Content-Length |
285 | 302 | BufferedImage image = ImageIO.read(file.getPath().toFile()); |
286 | 303 | BufferedImage resized = ImageUtil.resize(image, size); |
287 | | - ImageIO.write(resized, "png", response.getOutputStream()); |
| 304 | + // Write to a byte array first to get the length |
| 305 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 306 | + ImageIO.write(resized, "png", baos); |
| 307 | + byte[] pngBytes = baos.toByteArray(); |
| 308 | + response.setContentLengthLong(pngBytes.length); |
| 309 | + response.getOutputStream().write(pngBytes); |
288 | 310 | } else { |
289 | 311 | throw new IllegalArgumentException(String.format( |
290 | 312 | "Image can only be resized from %d to %d. You requested %d.", |
291 | 313 | MIN_IMAGE_SIZE, MAX_IMAGE_SIZE, size)); |
292 | 314 | } |
293 | 315 | } else { |
| 316 | + // For all other files, use the original content type and filename |
| 317 | + response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + dispositionFilename + "\""); |
| 318 | + response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache"); |
| 319 | + response.setHeader(HttpHeaders.CONTENT_TYPE, contentType); |
294 | 320 | response.setContentLengthLong(Files.size(file.getPath())); |
295 | 321 |
|
296 | 322 | try (InputStream inputStream = Files.newInputStream(file.getPath())) { |
|
0 commit comments