diff --git a/core/src/main/java/org/fao/geonet/api/records/attachments/FilesystemStore.java b/core/src/main/java/org/fao/geonet/api/records/attachments/FilesystemStore.java index 82da882a2e8..4f2c16ec739 100644 --- a/core/src/main/java/org/fao/geonet/api/records/attachments/FilesystemStore.java +++ b/core/src/main/java/org/fao/geonet/api/records/attachments/FilesystemStore.java @@ -1,6 +1,6 @@ /* * ============================================================================= - * === Copyright (C) 2001-2016 Food and Agriculture Organization of the + * === Copyright (C) 2001-2022 Food and Agriculture Organization of the * === United Nations (FAO-UN), United Nations World Food Programme (WFP) * === and United Nations Environment Programme (UNEP) * === @@ -39,6 +39,7 @@ import org.fao.geonet.utils.Log; import org.springframework.beans.factory.annotation.Autowired; +import javax.annotation.Nullable; import java.io.IOException; import java.io.InputStream; import java.nio.file.DirectoryStream; @@ -50,7 +51,6 @@ import java.util.Date; import java.util.List; import java.util.concurrent.TimeUnit; -import javax.annotation.Nullable; /** * A FileSystemStore store resources files in the catalog data directory. Each metadata record as a directory in the data directory @@ -148,25 +148,33 @@ public MetadataResource getResourceDescription(final ServiceContext context, Str return getResourceDescription(context, metadataUuid, visibility, path, approved); } + /** + * Get the resource description or null if the file doesn't exist. + * @param context the service context. + * @param metadataUuid the uuid of the owner metadata record. + * @param visibility is the resource is public or not. + * @param filePath the path to the resource. + * @param approved if the metadata draft has been approved or not + * @return the resource description or {@code null} if there is any problem accessing the file. + */ private MetadataResource getResourceDescription(final ServiceContext context, final String metadataUuid, - final MetadataResourceVisibility visibility, final Path filePath, Boolean approved) - throws IOException { - Integer metadataId = null; + final MetadataResourceVisibility visibility, final Path filePath, Boolean approved) { + FilesystemStoreResource result = null; try { - metadataId = getAndCheckMetadataId(metadataUuid, approved); - } catch (Exception e) { - Log.error(Geonet.RESOURCES, e.getMessage(), e); - } - - long fileSize = -1; - try { - fileSize = Files.size(filePath); + int metadataId = getAndCheckMetadataId(metadataUuid, approved); + long fileSize = Files.size(filePath); + result = new FilesystemStoreResource(metadataUuid, metadataId, filePath.getFileName().toString(), + settingManager.getNodeURL() + "api/records/", visibility, fileSize, + new Date(Files.getLastModifiedTime(filePath).toMillis()), approved); } catch (IOException e) { - Log.error(Geonet.RESOURCES, e.getMessage(), e); + Log.error(Geonet.RESOURCES, "Error getting size of file " + filePath + ": " + + e.getMessage(), e); + } catch (Exception e) { + Log.error(Geonet.RESOURCES, "Error in getResourceDescription: " + + e.getMessage(), e); } - return new FilesystemStoreResource(metadataUuid, metadataId, filePath.getFileName().toString(), settingManager.getNodeURL() + "api/records/", - visibility, fileSize, new Date(Files.getLastModifiedTime(filePath).toMillis()), approved); + return result; } @Override diff --git a/core/src/test/java/org/fao/geonet/api/records/attachments/FilesystemStoreTest.java b/core/src/test/java/org/fao/geonet/api/records/attachments/FilesystemStoreTest.java new file mode 100644 index 00000000000..34ea93823c6 --- /dev/null +++ b/core/src/test/java/org/fao/geonet/api/records/attachments/FilesystemStoreTest.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2001-2022 Food and Agriculture Organization of the + * United Nations (FAO-UN), United Nations World Food Programme (WFP) + * and United Nations Environment Programme (UNEP) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + * + * Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2, + * Rome - Italy. email: geonetwork@osgeo.org + */ +package org.fao.geonet.api.records.attachments; + +import jeeves.server.context.ServiceContext; +import org.fao.geonet.AbstractCoreIntegrationTest; +import org.fao.geonet.api.exception.ResourceNotFoundException; +import org.fao.geonet.domain.ISODate; +import org.fao.geonet.domain.MetadataResource; +import org.fao.geonet.domain.MetadataResourceVisibility; +import org.fao.geonet.domain.ReservedGroup; +import org.fao.geonet.kernel.datamanager.IMetadataManager; +import org.fao.geonet.kernel.setting.SettingManager; +import org.jdom.Element; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.io.InputStream; +import java.util.Date; + +import static org.junit.Assert.*; + +public class FilesystemStoreTest extends AbstractCoreIntegrationTest { + + @Autowired + protected IMetadataManager metadataManager; + @Autowired + protected SettingManager settingManager; + + @Test + public void getResourceDescription() throws Exception { + ServiceContext context = createServiceContext(); + loginAsAdmin(context); + + String mdId = metadataManager.insertMetadata( + context, + "iso19139", + new Element("MD_Metadata"), + "uuid", + context.getUserSession().getUserIdAsInt(), + "" + ReservedGroup.all.getId(), + "sourceid", + "n", + "doctype", + null, + new ISODate().getDateAndTime(), + new ISODate().getDateAndTime(), + false, + false); + + FilesystemStore filesystemStore = new FilesystemStore(); + filesystemStore.settingManager = this.settingManager; + + MetadataResource resource = filesystemStore.getResourceDescription(context, "uuid", MetadataResourceVisibility.PUBLIC, "test.jpg", true); + assertNull("Non exising resource must be null", resource); + + try (InputStream file = this.getClass().getResourceAsStream("existingResource.jpg")) { + filesystemStore.putResource(context, "uuid", "existingResource.jpg", file, new Date(), + MetadataResourceVisibility.PUBLIC, true); + resource = filesystemStore.getResourceDescription(context, "uuid", + MetadataResourceVisibility.PUBLIC, "existingResource.jpg", true); + assertNotNull("Existing resource must not return null", resource); + assertEquals("The file size doesn't match the expected one", 6416, resource.getSize()); + assertEquals("The visibility must be public", MetadataResourceVisibility.PUBLIC, resource.getVisibility()); + assertEquals("Filename is wrong", "existingResource.jpg", resource.getFilename()); + assertEquals("Metadata id is wrong", Integer.parseInt(mdId), resource.getMetadataId()); + } + } + + @Test(expected = ResourceNotFoundException.class) + public void testGetResourceDescriptionNonExistingUuid() throws Exception { + ServiceContext context = createServiceContext(); + loginAsAdmin(context); + FilesystemStore filesystemStore = new FilesystemStore(); + filesystemStore.settingManager = this.settingManager; + + + // context, metadataUuid, visibility, path, approved) + filesystemStore.getResourceDescription(context, "nonExistingUuid", + MetadataResourceVisibility.PUBLIC, "existingResource.jpg", true); + } +} diff --git a/core/src/test/resources/org/fao/geonet/api/records/attachments/existingResource.jpg b/core/src/test/resources/org/fao/geonet/api/records/attachments/existingResource.jpg new file mode 100644 index 00000000000..353a358d1ad Binary files /dev/null and b/core/src/test/resources/org/fao/geonet/api/records/attachments/existingResource.jpg differ