Added code to delte temp directories with uploaded temp files of

Geoportal
This commit is contained in:
Francesco Mangiacrapa 2022-10-20 16:58:29 +02:00
parent cc3595d4d9
commit d03d1a1477
1 changed files with 72 additions and 15 deletions

View File

@ -2,7 +2,10 @@ package org.gcube.portlets.user.geoportaldataentry.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -10,12 +13,15 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.bson.Document;
import org.gcube.application.geoportal.client.utils.Serialization;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation;
import org.gcube.application.geoportal.common.model.rest.TempFile;
import org.gcube.application.geoportal.common.model.useCaseDescriptor.RelationshipDefinition;
import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor;
import org.gcube.application.geoportal.common.utils.StorageUtils;
import org.gcube.application.geoportalcommon.ConvertToDataValueObjectModel;
import org.gcube.application.geoportalcommon.ConvertToDataViewModel;
import org.gcube.application.geoportalcommon.GeoportalCommon;
@ -47,6 +53,7 @@ import org.gcube.application.geoportalcommon.shared.products.ConcessioneDV;
import org.gcube.application.geoportalcommon.shared.products.content.WorkspaceContentDV;
import org.gcube.application.geoportalcommon.shared.products.paths.FileSetPathsDV;
import org.gcube.common.portal.PortalContext;
import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException;
import org.gcube.portlets.user.geoportaldataentry.client.GeoportalDataEntryService;
import org.gcube.portlets.user.geoportaldataentry.client.ProjectFormCard;
import org.gcube.portlets.user.geoportaldataentry.shared.CommitReport;
@ -82,6 +89,8 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen
public static final String GEONA_GENERIC_RESOURCE_SECONDARY_TYPE = "GEONA_GENERIC_RESOURCE_SECONDARY_TYPE";
private static final Logger LOG = LoggerFactory.getLogger(GeoportalDataEntryServiceImpl.class);
// private String tmpDirsLocation = System.getProperty("java.io.tmpdir");
/**
* Gets the GNA data entry config profile.
*
@ -146,11 +155,13 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen
+ e.getMessage());
}
List<File> listTempDirs = new ArrayList<File>();
try {
// Uploading files
// Uploading files into tempDirs in order to avoid clashing of names
LOG.debug("Going to upload the files");
recursiveUploadFileset(mongoService, profileID, theProject, tree_Node, null);
listTempDirs = recursiveUploadFileset(mongoService, profileID, theProject, tree_Node, null, listTempDirs);
} catch (Exception e) {
LOG.error("Error on uploading files: ", e);
@ -183,6 +194,22 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen
} catch (Exception e) {
throw new Exception("Error occurred on loading LifecycleInformation for the project: " + theProject.getId()
+ ". Error: " + e.getMessage());
} finally {
LOG.debug("List listTempDirs is: " + listTempDirs);
if (listTempDirs != null && listTempDirs.size() > 0) {
for (File file : listTempDirs) {
try {
String dirName = file.getName();
LOG.debug("Deleting directory directory: " + dirName);
FileUtils.deleteDirectory(file);
LOG.debug("Directory {} deleted!", dirName);
} catch (Exception e) {
LOG.debug("Error on deleting the directory: " + file);
}
}
}
}
}
@ -196,13 +223,14 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen
* @param sectionJSONPathIndexer the json path indexer
* @throws Exception the exception
*/
public void recursiveUploadFileset(MongoServiceUtil mongoService, String profileID, Project theProject,
Tree_Node<GeoNaFormDataObject> tree_Node, Map<String, Integer> sectionJSONPathIndexer) throws Exception {
public List<File> recursiveUploadFileset(MongoServiceUtil mongoService, String profileID, Project theProject,
Tree_Node<GeoNaFormDataObject> tree_Node, Map<String, Integer> sectionJSONPathIndexer, List<File> tempDirs)
throws Exception {
LOG.debug("recursiveUploadFileset called [tree_Node: " + tree_Node + "], [jsonPathIndexer: "
+ sectionJSONPathIndexer + "]");
if (tree_Node == null)
return;
return tempDirs;
if (sectionJSONPathIndexer == null) {
sectionJSONPathIndexer = new HashMap<String, Integer>();
@ -242,7 +270,6 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen
sectionJSONPathIndexer.put(sectionJSONPath, jpcV);
}
LOG.debug("sectionJSONPathIndexer is: " + sectionJSONPathIndexer);
LOG.info("The profile is: " + profile);
for (GenericDatasetBean gdb : listGDB) {
Map<String, FileSetDataObject> collectFilesetPerFieldDef = new HashMap<String, FileSetDataObject>();
@ -267,17 +294,25 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen
collFieldDef = new FileSetDataObject();
collFieldDef.setFilePathDV(filePath);
}
try {
File input = new File(file.getTempSystemPath());
File output = new File(file.getFileName());
LOG.debug("Temp file is: " + file.getTempSystemPath());
File tempDir = Files.createTempDirectory("GEOPORTAL_UPLOAD_").toFile();
String tmpDirPath = tempDir.getAbsolutePath();
File output = new File(tmpDirPath, file.getFileName());
// input.renameTo(output);
copyContent(input, output);
collFieldDef.addFile(output);
LOG.info("Temp file: " + file.getTempSystemPath() + ", copied to new file: " + file.getFileName());
}catch (Exception e) {
LOG.warn("Skipping file: "+file.getFileName()+ ". Error: "+e.getMessage());
tempDirs.add(tempDir);
tempDir.deleteOnExit();
LOG.info("Temp file: " + file.getTempSystemPath() + ", copied to new file: "
+ file.getFileName());
} catch (Exception e) {
LOG.warn("Skipping file: " + file.getFileName() + ". Error: " + e.getMessage());
}
collectFilesetPerFieldDef.put(filePath.getFieldDefinition(), collFieldDef);
}
@ -311,12 +346,34 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen
}
recursiveUploadFileset(mongoService, profileID, theProject, treeNodeChild_GNA_DO,
sectionJSONPathIndexer);
tempDirs = recursiveUploadFileset(mongoService, profileID, theProject, treeNodeChild_GNA_DO,
sectionJSONPathIndexer, tempDirs);
}
}
return tempDirs;
}
/**
* Creates the temp file on storage.
*
* @param is the is
* @param fileName the file name
* @return the temp file
*/
public TempFile createTempFileOnStorage(InputStream is, String fileName) {
LOG.debug("createTempFileOnStorage called");
StorageUtils storage = new StorageUtils();
TempFile toUpload = null;
try {
LOG.info("calling putOntoStorage the stream with the fileName: " + fileName);
toUpload = storage.putOntoStorage(is, fileName);
} catch (RemoteBackendException | FileNotFoundException e) {
LOG.error("Error when uploading stream on Storage: ", e);
}
return toUpload;
}
public static void copyContent(File a, File b) throws Exception {
@ -347,7 +404,7 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen
out.close();
}
}
System.out.println("File Copied");
LOG.debug("File Copied");
}
/**