diff --git a/build.gradle b/build.gradle index 7f5b2be..94cfb2e 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ repositories { group = 'com.cookedapps' mainClassName = 'com.cookedapps.warestore.Main' -version = '1.0.1' +version = '1.2.0' sourceCompatibility = 1.8 targetCompatibility = 1.8 diff --git a/src/main/java/com/cookedapps/warestore/DateRestore.java b/src/main/java/com/cookedapps/warestore/DateRestore.java index 7a40970..054918b 100644 --- a/src/main/java/com/cookedapps/warestore/DateRestore.java +++ b/src/main/java/com/cookedapps/warestore/DateRestore.java @@ -12,10 +12,7 @@ import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet; import java.io.*; -import java.nio.file.DirectoryStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; +import java.nio.file.*; import java.nio.file.attribute.FileTime; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -31,10 +28,13 @@ */ class DateRestore { + static final String OUTPUT_DIR = "wa_date_restored"; + private static final String ERROR_PATH_PREFIX = "Invalid path provided: "; + private static final String DATE_PATTERN_FILENAME = "yyyyMMdd"; private static final SimpleDateFormat DATE_FORMAT_PRINT = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); private static final SimpleDateFormat DATE_FORMAT_EXIF = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss"); - public static final String OUTPUT_DIR = "wa_date_restored"; + private static final SimpleDateFormat DATE_FORMAT_FILENAME = new SimpleDateFormat(DATE_PATTERN_FILENAME); private String directoryPath; @@ -46,7 +46,7 @@ void restoreDates(String directoryPath, boolean lastModifiedDate, boolean exifDa if(Files.isDirectory(directory)) { getDirectoryStream(directory).ifPresent(paths -> paths.forEach(path -> { if(!Files.isDirectory(path)) { - process(path, lastModifiedDate, exifDateTimeOriginal); + extractAndWriteDate(path, lastModifiedDate, exifDateTimeOriginal); } })); } else { @@ -70,24 +70,12 @@ private void printDirectoryError(String msg) { System.err.println(ERROR_PATH_PREFIX + msg); } - private void process(Path source, boolean lastModifiedDate, boolean exifDate) { + private void extractAndWriteDate(Path source, boolean lastModifiedDate, boolean exifDate) { String fileName = source.getFileName().toString(); - getDateStringFromFileName(fileName).ifPresent(dateString -> getDateFromString(dateString).ifPresent(date -> { - System.out.print("Processing " + source.getFileName() + ":"); - if(!lastModifiedDate && !exifDate) { - System.out.print(" Nothing to process"); - } else { - Path dest = createDestinationFile(source); - if(exifDate) { - overwriteExifDate(source, dest, date); - } - if(lastModifiedDate) { - overwriteLastModifiedDate(dest, date); - } - } - System.out.println(); - })); + getDateStringFromFileName(fileName) + .ifPresent(dateString -> getDate(source, dateString) + .ifPresent(date -> processFile(source, lastModifiedDate, exifDate, date))); } private Optional getDateStringFromFileName(String fileName) { @@ -106,15 +94,43 @@ private void printInlineError(String message) { System.err.print(" Error: " + message + "\n"); } + private Optional getDate(Path source, String dateString) { + Optional lastModDate = getDateFromLastMod(source, dateString); + if(lastModDate.isPresent()) { + return lastModDate; + } else { + return getDateFromString(dateString); + } + } + + private Optional getDateFromLastMod(Path source, String dateString) { + Optional lastModTime = getLastModTime(source); + if(lastModTime.isPresent()) { + Date lastModDate = Date.from(lastModTime.get().toInstant()); + String lastModDateString = DATE_FORMAT_FILENAME.format(lastModDate); + + if(lastModDateString.equals(dateString)) { + return Optional.of(lastModDate); + } + } + return Optional.empty(); + } + + private Optional getLastModTime(Path source) { + try { + return Optional.of(Files.getLastModifiedTime(source, LinkOption.NOFOLLOW_LINKS)); + } catch (IOException e) { + return Optional.empty(); + } + } + private Optional getDateFromString(String dateString) { - String datePattern = "yyyyMMdd"; - SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern); try { - Date date = simpleDateFormat.parse(dateString); + Date date = DATE_FORMAT_FILENAME.parse(dateString); date = add12HoursToDate(date); return Optional.of(date); } catch (ParseException e) { - printInlineError("Could not parse date " + dateString + " - Not matching " + datePattern); + printInlineError("Could not parse date " + dateString + " - Not matching " + DATE_PATTERN_FILENAME); e.printStackTrace(); return Optional.empty(); } @@ -127,6 +143,22 @@ private Date add12HoursToDate(Date date) { return cal.getTime(); } + private void processFile(Path source, boolean lastModifiedDate, boolean exifDate, Date date) { + System.out.print("Processing " + source.getFileName() + ":"); + if(!lastModifiedDate && !exifDate) { + System.out.print(" Nothing to process"); + } else { + Path dest = createDestinationFile(source); + if(exifDate) { + overwriteExifDate(source, dest, date); + } + if(lastModifiedDate) { + overwriteLastModifiedDate(dest, date); + } + } + System.out.println(); + } + private Path createDestinationFile(Path source) { File dest = getOrCreateFile(source); copySourceFileToDest(source, dest);