/*
 * Licensed to David Pilato (the "Author") under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Author licenses this
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package fr.pilato.elasticsearch.crawler.fs.fileabstractor;

import fr.pilato.elasticsearch.crawler.fs.meta.settings.FsSettings;
import fr.pilato.elasticsearch.crawler.fs.util.FsCrawlerUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;

public class FileAbstractorFile extends FileAbstractor<File> {
    public FileAbstractorFile(FsSettings fsSettings) {
        super(fsSettings);
    }

    @Override
    public FileAbstractModel toFileAbstractModel(String path, File file) {
        FileAbstractModel model = new FileAbstractModel();
        model.name = file.getName();
        model.file = file.isFile();
        model.directory = !model.file;
        model.lastModifiedDate = Instant.ofEpochMilli(file.lastModified());
        model.creationDate = FsCrawlerUtil.getCreationTime(file);
        model.path = path;
        model.fullpath = file.getAbsolutePath();
        model.size = file.length();
        model.owner = FsCrawlerUtil.getOwnerName(file);
        model.group = FsCrawlerUtil.getGroupName(file);

        return model;
    }

    @Override
    public InputStream getInputStream(FileAbstractModel file) throws Exception {
        return new FileInputStream(new File(file.fullpath));
    }

    @Override
    public Collection<FileAbstractModel> getFiles(String dir) {
        if (logger.isDebugEnabled()) logger.debug("Listing local files from {}", dir);
        File[] files = new File(dir).listFiles();

        Collection<FileAbstractModel> result = new ArrayList<>(files.length);

        // Iterate other files
        for (File file : files) {
            result.add(toFileAbstractModel(dir, file));
        }

        if (logger.isDebugEnabled()) logger.debug("{} local files found", result.size());
        return result;
    }

    @Override
    public boolean exists(String dir) throws Exception {
        return new File(dir).exists();
    }

    @Override
    public void open() throws Exception {

    }

    @Override
    public void close() throws Exception {

    }
}

@@ -1146,16 +1146,46 @@
 lection;
+%0Aimport java.util.Collections;
 %0A%0Apublic
@@ -2238,37 +2238,8 @@
     
- if (logger.isDebugEnabled())
  log
@@ -2335,17 +2335,16 @@
 iles();%0A
-%0A
         
@@ -2372,16 +2372,66 @@
 ctModel%3E
+ result;%0A%0A        if (files != null) %7B%0A           
  result 
@@ -2472,16 +2472,20 @@
         
+    
 // Itera
@@ -2499,16 +2499,20 @@
 r files%0A
+    
         
@@ -2545,24 +2545,28 @@
             
+    
 result.add(t
@@ -2593,24 +2593,28 @@
 ir, file));%0A
+    
         %7D%0A%0A 
@@ -2607,24 +2607,40 @@
           %7D%0A
+        %7D else %7B
 %0A        if 
@@ -2640,36 +2640,159 @@
     
-if (
+    
 logger.
-isD
+d
 ebug
-Enabled())
+(%22Symlink on windows gives null for listFiles(). Skipping %5B%7B%7D%5D%22, dir);%0A            result = Collections.EMPTY_LIST;%0A        %7D%0A%0A%0A       
  log

Fix NullPointerException when traversing certain directories when running on Windows