TLC_Post_Server/src/ImageFinderTimestamp.java

98 lines
2.1 KiB
Java

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.Comparator;
import org.apache.commons.io.filefilter.WildcardFileFilter;
public class ImageFinderTimestamp extends ImageFinder
{
public ImageFinderTimestamp(String rootFolder)
{
super(rootFolder);
// TODO Auto-generated constructor stub
}
static int sortedimagecounter = 0;
static int fileslenght = 0;
static File[] files = null;
private static void sortImages()
{
// System.out.println("Path: " + rootFolder + years[i] + "/"
// + monthNames[j] + "/" );
/*
* File dir = new File( rootFolder + years[i] + "/" + monthNames[j] +
* "/");
*/
File dir = new File(rootFolder + "/");
files = dir.listFiles();
fileslenght = files.length;
System.out.println("File lenght: " + fileslenght);
System.exit(0);
sortFilesByDateCreated(files);
sortedimagecounter = 0;
/*
* for (int k = 0; k < files.length; k++) {
* System.out.println(files[k].getName()); }
*/
}
public String getImagePath(int imageCounter)
{
String path = null;
if (sortedimagecounter != (fileslenght - 1)) {
System.out.println("sort new Images");
sortImages();
path = files[sortedimagecounter].getAbsolutePath();
sortedimagecounter++;
return path;
} else {
path = files[sortedimagecounter].getAbsolutePath();
sortedimagecounter++;
return path;
}
}
public static void sortFilesByDateCreated(File[] files)
{
Arrays.sort(files, new Comparator<File>() {
public int compare(File f1, File f2)
{
long l1 = getFileCreationEpoch(f1);
long l2 = getFileCreationEpoch(f2);
return Long.valueOf(l1).compareTo(l2);
}
});
}
public static long getFileCreationEpoch(File file)
{
try {
BasicFileAttributes attr = Files.readAttributes(file.toPath(),
BasicFileAttributes.class);
return attr.creationTime().toInstant().toEpochMilli();
} catch (IOException e) {
throw new RuntimeException(file.getAbsolutePath(), e);
}
}
}