package org.datavault.broker.services;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

/**
 * User: Robin Taylor
 * Date: 19/03/2015
 * Time: 13:34
 */
public class MacFilesService {

    private String activeDir;

    public void setactiveDir(String activeDir) {
        this.activeDir = activeDir;
    }
    
    public org.datavault.common.model.Files getFilesListing(String filePath) {

        // Join the requested path to the root of the filesystem.
        // In future this path handling should be part of a filesystem-specific driver.
        if (filePath.equals("")) {
            filePath = activeDir;
        } else {
            // A leading '/' will cause the path to be treated as absolute
            while (filePath.startsWith("/")) {
                filePath = filePath.replaceFirst("/", "");
            }
            
            java.nio.file.Path basePath = java.nio.file.Paths.get(activeDir);
            filePath = basePath.resolve(filePath).toString();
        }
        
        org.datavault.common.model.Files files = new org.datavault.common.model.Files();

        Map filesMap = new HashMap<String, String>();

        try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(filePath))) {
            for (Path entry : stream) {
                if (Files.isDirectory(entry)) {
                    filesMap.put(entry.toString(), "directory");
                } else {
                    filesMap.put(entry.toString(), "file");
                }

            }

        } catch (IOException e) {
            System.out.println(e.toString());
        }

        files.setFilesMap(filesMap);

        return files;


    }


}


@@ -689,16 +689,98 @@
 driver.%0A
+        Path basePath = Paths.get(activeDir);%0A        Path completePath;%0A        %0A
         
@@ -822,28 +822,31 @@
     
-filePath = activeDir
+completePath = basePath
 ;%0A  
@@ -1084,89 +1084,15 @@
     
-java.nio.file.Path basePath = java.nio.file.Paths.get(activeDir);%0A            fil
+complet
 ePat
@@ -1121,27 +1121,16 @@
 ilePath)
-.toString()
 ;%0A      
@@ -1360,29 +1360,22 @@
 eam(
-Paths.get(fil
+complet
 ePath))
-)
  %7B%0A 

Tidy up use of Path/Paths