Skip to content

Commit

Permalink
[Backport 4.2.x] Attachments API use filename from Content-Dispositio…
Browse files Browse the repository at this point in the history
…n before url (#8470)

* Update spring doc consumes and produces

* Update putResource logic to get filename from contentDisposition header before using url

* retrigger checks

* retrigger checks

* Remove incorrect consumes

* Update getFilenameFromHeader to return null on exception

* Update getFilenameFromHeader to return null on exception (including fileUrl.openConnection)
  • Loading branch information
tylerjmchugh authored and ianwallen committed Dec 10, 2024
1 parent b243377 commit d20cf5b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@
import org.fao.geonet.kernel.AccessManager;
import org.fao.geonet.kernel.datamanager.IMetadataUtils;
import org.fao.geonet.repository.MetadataRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -53,6 +57,7 @@
public abstract class AbstractStore implements Store {
protected static final String RESOURCE_MANAGEMENT_EXTERNAL_PROPERTIES_SEPARATOR = ":";
protected static final String RESOURCE_MANAGEMENT_EXTERNAL_PROPERTIES_ESCAPED_SEPARATOR = "\\:";
private static final Logger log = LoggerFactory.getLogger(AbstractStore.class);

@Override
public final List<MetadataResource> getResources(final ServiceContext context, final String metadataUuid, final Sort sort,
Expand Down Expand Up @@ -152,6 +157,29 @@ protected int canDownload(ServiceContext context, String metadataUuid, MetadataR
return metadataId;
}

protected String getFilenameFromHeader(final URL fileUrl) throws IOException {
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) fileUrl.openConnection();
connection.setRequestMethod("HEAD");
connection.connect();
String contentDisposition = connection.getHeaderField("Content-Disposition");

if (contentDisposition != null && contentDisposition.contains("filename=")) {
String filename = contentDisposition.split("filename=")[1].replace("\"", "").trim();
return filename.isEmpty() ? null : filename;
}
return null;
} catch (Exception e) {
log.error("Error retrieving resource filename from header", e);
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}

protected String getFilenameFromUrl(final URL fileUrl) {
String fileName = FilenameUtils.getName(fileUrl.getPath());
if (fileName.contains("?")) {
Expand Down Expand Up @@ -198,7 +226,11 @@ public final MetadataResource putResource(ServiceContext context, String metadat
@Override
public final MetadataResource putResource(ServiceContext context, String metadataUuid, URL fileUrl,
MetadataResourceVisibility visibility, Boolean approved) throws Exception {
return putResource(context, metadataUuid, getFilenameFromUrl(fileUrl), fileUrl.openStream(), null, visibility, approved);
String filename = getFilenameFromHeader(fileUrl);
if (filename == null) {
filename = getFilenameFromUrl(fileUrl);
}
return putResource(context, metadataUuid, filename, fileUrl.openStream(), null, visibility, approved);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public void delResources(
@io.swagger.v3.oas.annotations.Operation(summary = "Create a new resource for a given metadata")
@PreAuthorize("hasAuthority('Editor')")
@RequestMapping(method = RequestMethod.POST,
consumes = MediaType.ALL_VALUE,
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(value = HttpStatus.CREATED)
@ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Attachment uploaded."),
Expand Down Expand Up @@ -228,7 +228,8 @@ public MetadataResource putResource(

@io.swagger.v3.oas.annotations.Operation(summary = "Create a new resource from a URL for a given metadata")
@PreAuthorize("hasAuthority('Editor')")
@RequestMapping(method = RequestMethod.PUT)
@RequestMapping(method = RequestMethod.PUT,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(value = HttpStatus.CREATED)
@ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Attachment added."),
@ApiResponse(responseCode = "403", description = ApiParams.API_RESPONSE_NOT_ALLOWED_CAN_EDIT)})
Expand Down

0 comments on commit d20cf5b

Please sign in to comment.