Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 4.2.x] Attachments API use filename from Content-Disposition before url #8554

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading