package it.digione.dg1cloud.service;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.net.MalformedURLException;
|
import java.net.URL;
|
|
import org.apache.commons.io.FileUtils;
|
import org.apache.logging.log4j.util.Strings;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.web.util.UriBuilder;
|
import org.springframework.web.util.UriComponentsBuilder;
|
|
import it.digione.dg1cloud.config.AppConfig;
|
import it.digione.dg1cloud.model.RegDocument;
|
|
@Service("utils")
|
public class Utils {
|
|
@Autowired private AppConfig appConfig;
|
|
private static final Logger logger = LoggerFactory.getLogger(CloudService.class);
|
|
public URL generateUrl(RegDocument regDocument) throws MalformedURLException {
|
logger.debug("Genero l'url per il record file {} con id {}", regDocument.getFileName(), regDocument.getDocumentId());
|
UriBuilder uriBuilder = UriComponentsBuilder.fromUriString(appConfig.getExternalBaseUrl());
|
uriBuilder.path("/downloadFile");
|
uriBuilder.queryParam("fileName", regDocument.getFileName());
|
uriBuilder.queryParam("id", regDocument.getDocumentId());
|
if ( Strings.isEmpty(regDocument.getSecretKey()) == false ) {
|
uriBuilder.queryParam("secretKey", regDocument.getSecretKey());
|
}
|
return uriBuilder.build().toURL();
|
}
|
|
public void deleteDir( File directory ) throws IOException {
|
logger.debug("Rimozione directory {}", directory.getAbsolutePath());
|
FileUtils.deleteDirectory(directory);
|
}
|
|
public void deleteEmptyDirectoriesRecursive( File directory) throws IOException {
|
for (File subDirectory : directory.listFiles(File::isDirectory) ) {
|
logger.debug("Analizzo sottodirectory {}", subDirectory.getAbsolutePath());
|
deleteEmptyDirectoriesRecursive(subDirectory);
|
}
|
if ( directory.listFiles().length == 0 ) {
|
logger.debug("La directory {} e' vuota", directory.getAbsolutePath());
|
deleteDir(directory);
|
} else {
|
logger.debug("La directory {} non e' vuota", directory.getAbsolutePath());
|
}
|
}
|
}
|