Skip to content

Commit

Permalink
refactor: Determine content-type dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
PolinaPolupan committed Mar 4, 2025
1 parent ce1b562 commit 2bd039b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

import java.io.IOException;
import java.net.URI;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

import com.example.mypixel.exception.InvalidImageFormat;
import com.example.mypixel.storage.StorageService;
import jakarta.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
Expand All @@ -23,6 +27,9 @@ public class ImageUploadController {

private final StorageService storageService;

@Autowired
private ServletContext servletContext;

@Autowired
public ImageUploadController(StorageService storageService) {
this.storageService = storageService;
Expand All @@ -46,13 +53,19 @@ public ResponseEntity<Resource> serveFile(@PathVariable String filename) throws
if (file == null)
return ResponseEntity.notFound().build();

String location = ServletUriComponentsBuilder
.fromCurrentRequest()
.toUriString();
String contentType = null;
try {
contentType = Files.probeContentType(Paths.get(file.getFile().getAbsolutePath()));
} catch (IOException e) {
contentType = URLConnection.guessContentTypeFromName(file.getFilename());
}

if (contentType == null) {
contentType = "application/octet-stream";
}

return ResponseEntity.ok()
.contentType(MediaType.IMAGE_JPEG)
.header(HttpHeaders.LOCATION, location)
.contentType(MediaType.valueOf(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + file.getFilename() + "\"").body(file);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.mypixel;

import java.io.File;
import java.nio.file.Paths;
import java.util.stream.Stream;

Expand Down Expand Up @@ -66,6 +67,7 @@ public void shouldServeImage() throws Exception {
String filename = "test.jpg";
Resource mockResource = mock(Resource.class);
given(mockResource.getFilename()).willReturn(filename);
given(mockResource.getFile()).willReturn(new File(filename));
given(mockResource.exists()).willReturn(true);
given(mockResource.isReadable()).willReturn(true);

Expand All @@ -74,6 +76,7 @@ public void shouldServeImage() throws Exception {
mockMvc.perform(get("/images/{filename}", filename))
.andExpect(status().isOk())
.andExpect(header().string("Content-Disposition", containsString("attachment; filename=\"test.jpg\"")))
.andExpect(header().string("Content-Type", containsString("image/jpeg")))
.andExpect(content().contentType(MediaType.IMAGE_JPEG));
}

Expand Down

0 comments on commit 2bd039b

Please sign in to comment.