2019-02-21 22:34:35 +01:00
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
}
2019-02-22 11:49:40 +01:00
static int filesindex ;
2019-02-21 22:34:35 +01:00
2019-02-22 11:49:40 +01:00
static int fileslenght ;
2019-02-21 22:34:35 +01:00
static File [ ] files = null ;
private static void sortImages ( )
{
2019-02-22 11:49:40 +01:00
for ( int i = currentYear ; i < years . length ; i + + ) {
if ( years [ i ] ! = null ) {
2019-02-21 22:34:35 +01:00
2019-02-22 11:49:40 +01:00
for ( int j = currentMonth ; j < month [ i ] . length ; j + + ) {
if ( month [ i ] [ j ] ) {
File dir = new File (
rootFolder + years [ i ] + " / " + monthNames [ j ] + " / " ) ;
2019-02-21 22:34:35 +01:00
2019-02-22 11:49:40 +01:00
files = dir . listFiles ( ) ;
fileslenght = files . length ;
2019-02-21 22:34:35 +01:00
2019-02-22 11:49:40 +01:00
// System.out.println("File lenght: " + fileslenght);
2019-02-21 22:34:35 +01:00
2019-02-22 11:49:40 +01:00
sortFilesByDateCreated ( files ) ;
2019-02-21 22:34:35 +01:00
2019-02-22 11:49:40 +01:00
filesindex = 0 ;
2019-02-21 22:34:35 +01:00
2019-02-22 11:49:40 +01:00
currentMonth = j + 1 ;
currentYear = i ;
return ;
}
}
currentMonth = 0 ;
}
}
2019-02-21 22:34:35 +01:00
}
public String getImagePath ( int imageCounter )
{
String path = null ;
2019-02-22 11:49:40 +01:00
// System.out.println("filesindex: " + filesindex);
// System.out.println("fileslenght: " + fileslenght);
2019-02-21 22:34:35 +01:00
2019-02-22 11:49:40 +01:00
if ( ( filesindex < fileslenght ) & & ( fileslenght ! = 0 ) ) {
path = files [ filesindex ] . getAbsolutePath ( ) ;
filesindex + + ;
2019-02-22 19:55:34 +01:00
return path . substring ( lengthRootFolder ) ;
2019-02-21 22:34:35 +01:00
} else {
2019-02-22 11:49:40 +01:00
// System.out.println("sort new Images");
sortImages ( ) ;
if ( filesindex > = fileslenght ) {
return " 404 " ;
}
path = files [ filesindex ] . getAbsolutePath ( ) ;
filesindex + + ;
2019-02-22 19:55:34 +01:00
return path . substring ( lengthRootFolder ) ;
2019-02-21 22:34:35 +01:00
}
}
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 ) ;
}
}
}