Development
Created on:
Thursday, December 1, 2016

Simple utility class for retrieving files from file system

Below is a simple utility class that we have used for our asset migration projects, to migrate assets from file system into AEM Assets (DAM). Enjoy!

import com.google.gson.JsonArray;
import com.google.gson.JsonPrimitive;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.activation.MimetypesFileTypeMap;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public final class FileSystemUtil {

    private static final Logger LOG = LoggerFactory.getLogger(FileSystemUtil.class);
    private static final MimetypesFileTypeMap mimetypesFileTypeMap = new MimetypesFileTypeMap();
   private static List<InputStream> isList = new ArrayList<>();

    private FileSystemUtil() {}

    public static void fetchFiles(String folderPath, List<File> files) {
       File dir = new File(folderPath);
       if (dir.isDirectory()) {
           File[] filesList = dir.listFiles();
           if (filesList != null && filesList.length > 0) {
               for (File file : filesList) {
                   if (file.isFile()) {
                       files.add(file);
                   } else if (file.isDirectory()) {
                       fetchFiles(file.getAbsolutePath(), files);
                   }
               }
           }
       }
   }

    public static InputStream getInputStream(File file) {
       InputStream is = null;
       try {
           is = new BufferedInputStream(new FileInputStream(file));
           isList.add(is);
       }
       catch (IOException e) {
           LOG.error(e.getMessage(), e);
       }
       return is;
   }

   public static void closeInputStream() {
       if (!isList.isEmpty()) {
           for (InputStream is : isList) {
               closeInputStream(is);
           }
           isList.clear();
       }
   }

   public static void closeInputStream(InputStream is) {
       try {
           if (is != null) {
               is.close();
           }
       }
       catch (IOException e) {
           LOG.error(e.getMessage(), e);
       }
   }

   public static String getMimeType(File file) {
       if (file != null) {
           return mimetypesFileTypeMap.getContentType(file);
       }
       return null;
   }

   public static File getFile(String path) {
       return new File(path);
   }

   public static String getFileContent(String path) {
       String content = StringUtils.EMPTY;
       try {
           File file = getFile(path);
           if (file.exists()) {
               byte[] bytes = IOUtils.toByteArray(getInputStream(file));
               content = new String(bytes);
           }
       }
       catch (IOException e) {
           LOG.error(e.getMessage(), e);
       }
       return content;
   }
}

Stop blending in with the rest of the crowd &
start leaving your mark on the web.
Your success is our success

Let's work together