Skip to content

Commit

Permalink
Set file name on file download sme (#458)
Browse files Browse the repository at this point in the history
* fix: update filename on download

* fix: add filename check

* refactor: remove url as a separate variable
  • Loading branch information
ShehriyarShariq-Fraunhofer authored Sep 25, 2024
1 parent 4fc702e commit 827de16
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -214,7 +215,11 @@ public ResponseEntity<Submodel> getSubmodelByIdMetadata(Base64UrlEncodedIdentifi
public ResponseEntity<Resource> getFileByPath(Base64UrlEncodedIdentifier submodelIdentifier, String idShortPath) {
Resource resource = new FileSystemResource(repository.getFileByPathSubmodel(submodelIdentifier.getIdentifier(), idShortPath));

return new ResponseEntity<Resource>(resource, HttpStatus.OK);
String fileName = resource.getFilename();

return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
.body(resource);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -42,6 +43,7 @@
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.ProtocolException;
Expand Down Expand Up @@ -355,6 +357,7 @@ public void deleteFileFromNotExistElement() throws FileNotFoundException, Unsupp
@Test
public void getFile() throws FileNotFoundException, IOException, ParseException {
String fileName = DummySubmodelFactory.FILE_NAME;
String expectedFileName = Base64UrlEncodedIdentifier.encodeIdentifier(DummySubmodelFactory.SUBMODEL_FOR_FILE_TEST) + "-" + DummySubmodelFactory.SUBMODEL_ELEMENT_FILE_ID_SHORT + "-" + fileName;

byte[] expectedFile = readBytesFromClasspath(fileName);

Expand All @@ -363,6 +366,14 @@ public void getFile() throws FileNotFoundException, IOException, ParseException
CloseableHttpResponse response = BaSyxHttpTestUtils.executeGetOnURL(createSMEFileGetURL(DummySubmodelFactory.SUBMODEL_FOR_FILE_TEST, DummySubmodelFactory.SUBMODEL_ELEMENT_FILE_ID_SHORT));
assertEquals(HttpStatus.OK.value(), response.getCode());

Header contentDispositionHeader = response.getFirstHeader("Content-Disposition");
assertNotNull(contentDispositionHeader);

String contentDisposition = contentDispositionHeader.getValue();
String actualFileName = extractFileNameFromContentDisposition(contentDisposition);

assertEquals(expectedFileName, actualFileName);

byte[] actualFile = EntityUtils.toByteArray(response.getEntity());

response.close();
Expand All @@ -384,6 +395,16 @@ public void getFileFromNotExistElement() throws FileNotFoundException, Unsupport
assertEquals(HttpStatus.NOT_FOUND.value(), response.getCode());
}

private String extractFileNameFromContentDisposition(String contentDisposition) {
for (String part : contentDisposition.split(";")) {
part = part.trim();
if (part.startsWith("filename")) {
return part.split("=")[1].replace("\"", "").trim();
}
}
return null;
}

private String getJSONWithoutCursorInfo(String response) throws JsonMappingException, JsonProcessingException {
return BaSyxHttpTestUtils.removeCursorFromJSON(response);
}
Expand Down

0 comments on commit 827de16

Please sign in to comment.