Leonardo Vannucci
2018-08-01 5554f5ebced8adb7b7982481120d0ca00767c7f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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.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());
       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());
       }
   }
}