Skip to content

Commit

Permalink
Improved extraction of the date -> time of day. v1.2.0
Browse files Browse the repository at this point in the history
The algorithm now takes the LastModifiedTime as date if it matches the
date in the filename because it might contain the actual time of day.
  • Loading branch information
Sandro committed Jan 7, 2019
1 parent 71422d0 commit 2a11cd4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
84 changes: 58 additions & 26 deletions src/main/java/com/cookedapps/warestore/DateRestore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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 {
Expand All @@ -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<String> getDateStringFromFileName(String fileName) {
Expand All @@ -106,15 +94,43 @@ private void printInlineError(String message) {
System.err.print(" Error: " + message + "\n");
}

private Optional<Date> getDate(Path source, String dateString) {
Optional<Date> lastModDate = getDateFromLastMod(source, dateString);
if(lastModDate.isPresent()) {
return lastModDate;
} else {
return getDateFromString(dateString);
}
}

private Optional<Date> getDateFromLastMod(Path source, String dateString) {
Optional<FileTime> 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<FileTime> getLastModTime(Path source) {
try {
return Optional.of(Files.getLastModifiedTime(source, LinkOption.NOFOLLOW_LINKS));
} catch (IOException e) {
return Optional.empty();
}
}

private Optional<Date> 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();
}
Expand All @@ -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);
Expand Down

0 comments on commit 2a11cd4

Please sign in to comment.