Skip to content

Commit

Permalink
Harvesters / GeoNetwork harvester / Re-add exiting files when re-harv…
Browse files Browse the repository at this point in the history
…esting (#6416)

* Harvesters / GeoNetwork harvester / Re-add exiting files when re-harvesting

getResourceDescription must return null if the resource doesn't exist. This way the caller Aligner process
can save the file again as it checks for null to know if the resource exists or not.

* Add test cases
  • Loading branch information
juanluisrp committed Jun 21, 2022
1 parent 23640cc commit c3fe382
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 16 deletions.
@@ -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)
* ===
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
@@ -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);
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c3fe382

Please sign in to comment.