Skip to content

Commit

Permalink
#169: lib-test
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Apr 25, 2020
1 parent af35237 commit d245461
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 183 deletions.
7 changes: 3 additions & 4 deletions fotolib2/src/main/java/de/k3b/io/FileCommandLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package de.k3b.io;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
Expand All @@ -44,11 +43,11 @@ public void openLogfile() {
if (mLogFilePath != null) {
OutputStream stream = null;
try {
File logFile = new File(mLogFilePath);
IFile logFile = FileFacade.convert(new File(mLogFilePath));
if (logFile.exists()) {
// open existing in append mode
long ageInHours = (new Date().getTime() - logFile.lastModified()) / (1000 * 60 * 60);
stream = new FileOutputStream(logFile, true);
stream = logFile.openOutputStream();
mLogFile = new PrintWriter(stream, true);

if (ageInHours > 15) {
Expand All @@ -57,7 +56,7 @@ public void openLogfile() {
}
} else {
// create new
mLogFile = new PrintWriter(logFile, "UTF-8");
mLogFile = new PrintWriter(logFile.openOutputStream());
log("rem " , new Date());
}
} catch (Throwable e) {
Expand Down
22 changes: 11 additions & 11 deletions fotolib2/src/main/java/de/k3b/io/FileCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public int applyExifChanges(boolean move, PhotoPropertiesDiffCopy exifChanges, S
* @param selectedFiles
* @param destDirFolder where files are moved/copied to
* @param progessListener */
public int moveOrCopyFilesTo(boolean move, SelectedFiles selectedFiles, File destDirFolder, IProgessListener progessListener) {
public int moveOrCopyFilesTo(boolean move, SelectedFiles selectedFiles, IFile destDirFolder, IProgessListener progessListener) {
PhotoAutoprocessingDto autoProccessData = (!LibGlobal.apmEnabled) ? null : getPhotoAutoprocessingDto(destDirFolder);

return moveOrCopyFilesTo(move, selectedFiles, destDirFolder, autoProccessData, progessListener);
Expand All @@ -187,7 +187,7 @@ public int moveOrCopyFilesTo(boolean move, SelectedFiles selectedFiles, File des
* @param destDirFolder where files are moved/copied to
* @param autoProccessData null or data for auto rename/exif data
* @param progessListener */
public int moveOrCopyFilesTo(boolean move, SelectedFiles selectedFiles, File destDirFolder,
public int moveOrCopyFilesTo(boolean move, SelectedFiles selectedFiles, IFile destDirFolder,
PhotoAutoprocessingDto autoProccessData, IProgessListener progessListener) {
boolean doNotRenameIfSourceInDestFolder = false;
IFileNameProcessor renameProcessor = null;
Expand Down Expand Up @@ -218,11 +218,11 @@ public int moveOrCopyFilesTo(boolean move, SelectedFiles selectedFiles, File des
*/
int moveOrCopyFilesTo(boolean move,
PhotoPropertiesDiffCopy exifChanges, SelectedFiles selectedFiles, IFileNameProcessor renameProcessor,
File destDirFolder, IProgessListener progessListener) {
IFile destDirFolder, IProgessListener progessListener) {
int result = 0;
if (canProcessFile(move ? OP_MOVE : OP_COPY)) {
if (osCreateDirIfNeccessary(destDirFolder)) {
File[] destFiles = createDestFiles(renameProcessor, destDirFolder, selectedFiles.getDatesPhotoTaken() , selectedFiles.getFiles());
IFile[] destFiles = createDestFiles(renameProcessor, destDirFolder, selectedFiles.getDatesPhotoTaken(), selectedFiles.getIFiles());

result = moveOrCopyFiles(move, (move ? "mov" : "copy"), exifChanges, selectedFiles, destFiles, progessListener);

Expand Down Expand Up @@ -275,7 +275,7 @@ protected TransactionLoggerBase createTransactionLogger(long now) {
return new TransactionLoggerBase(this, now);
}

private PhotoAutoprocessingDto getPhotoAutoprocessingDto(File destDirFolder) {
private PhotoAutoprocessingDto getPhotoAutoprocessingDto(IFile destDirFolder) {
PhotoAutoprocessingDto autoProccessData = null;
try {
autoProccessData = new PhotoAutoprocessingDto().load(destDirFolder);
Expand All @@ -290,24 +290,24 @@ public PhotoPropertiesBulkUpdateService createWorkflow(TransactionLoggerBase log
return new PhotoPropertiesBulkUpdateService(logger);
}

private File[] createDestFiles(IFileNameProcessor renameProcessor, File destDirFolder, Date[] datesLastModified, File... sourceFiles) {
File[] result = new File[sourceFiles.length];
private IFile[] createDestFiles(IFileNameProcessor renameProcessor, IFile destDirFolder, Date[] datesLastModified, IFile... sourceFiles) {
IFile[] result = new IFile[sourceFiles.length];

int pos = 0;
File destFile;
for (File srcFile : sourceFiles) {
IFile destFile;
for (IFile srcFile : sourceFiles) {
if (renameProcessor != null) {
destFile = renameProcessor.getNextFile(srcFile, getRenameSourceFileDate(srcFile, datesLastModified, pos), -1);
} else {
destFile = new File(destDirFolder, srcFile.getName());
destFile = destDirFolder.create(srcFile.getName(), srcFile.getMime());
}
result[pos++] = destFile;
}

return result;
}

private Date getRenameSourceFileDate(File srcFile, Date[] datesLastModified, int pos) {
private Date getRenameSourceFileDate(IFile srcFile, Date[] datesLastModified, int pos) {
if ((datesLastModified != null) && (pos >= 0) && (pos < datesLastModified.length)) {
return datesLastModified[pos];
}
Expand Down
13 changes: 7 additions & 6 deletions fotolib2/src/main/java/de/k3b/io/FileProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/

public class FileProcessor extends FileCommandLogger implements IFileCommandLogger {
protected static final String UNKNOWN_MIME = null;
private static final String EXT_SIDECAR = ".xmp";

/// TODO what is mime for XMP
Expand Down Expand Up @@ -111,11 +112,6 @@ protected boolean fileOrSidecarExists(IFile file) {
|| FileCommands.getSidecar(parent, name, true).exists();
}

@Deprecated
public File renameDuplicate(File file) {
return FileFacade.convert(file).getFile();
}

public static XmpFile getExistingSidecarOrNull(String absolutePath) {
XmpFile result = null;
if (absolutePath != null) {
Expand All @@ -138,8 +134,13 @@ public static XmpFile getExistingSidecarOrNull(String absolutePath, boolean long
return result;
}

@Deprecated
public File renameDuplicate(File file) {
return FileFacade.convert(file).getFile();
}

/**
* @return file if rename is not neccessary else File with new name
* @return file if rename is not neccessary else IFile with new name
*/
public IFile renameDuplicate(IFile file) {
if (!fileOrSidecarExists(file)) {
Expand Down
3 changes: 1 addition & 2 deletions fotolib2/src/main/java/de/k3b/io/IFileNameProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package de.k3b.io;

import java.io.File;
import java.util.Date;

/**
Expand All @@ -33,5 +32,5 @@ public interface IFileNameProcessor {
* @param firstFileInstanceNumber number where numbering starts with. -1 : auto
* @return next absoulte renamed file.
*/
File getNextFile(File sourceFile, Date sourceFileDate, int firstFileInstanceNumber);
IFile getNextFile(IFile sourceFile, Date sourceFileDate, int firstFileInstanceNumber);
}
79 changes: 41 additions & 38 deletions fotolib2/src/main/java/de/k3b/io/PhotoAutoprocessingDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;

import de.k3b.LibGlobal;
Expand Down Expand Up @@ -53,29 +53,19 @@ public class PhotoAutoprocessingDto implements Serializable {
private static final String KEY_OUT_DIR = "outDir";

private final Properties properties;
private File outDir;
private IFile outDir;

public PhotoAutoprocessingDto() {
this(null, new Properties());
}

public PhotoAutoprocessingDto(File outDir, Properties properties) {
public PhotoAutoprocessingDto(IFile outDir, Properties properties) {
this.outDir = outDir;
this.properties = properties;
}

public PhotoAutoprocessingDto load(File outDir) throws IOException {
this.outDir = outDir;
File apm = getApmFile();
properties.clear();
if (apm.exists() && apm.isFile() && apm.canRead()) {
properties.load(apm);
if (LibGlobal.debugEnabled) {
logger.debug(this.getClass().getSimpleName() + ": loaded from " + apm + ":" + this);
}
return this;
}
return null;
public static IFile getApmFile(IFile outDir) {
return outDir.create(RuleFileNameProcessor.APM_FILE_NAME, null);
}

public void paste(PhotoAutoprocessingDto newData) {
Expand Down Expand Up @@ -105,18 +95,44 @@ public String getTranslateName(PhotoAutoprocessingDto newData) {
return null;
}

private File getApmFile() {
return getApmFile(this.outDir);
/**
* Android support: to persist state and to transfer activites via intent.
*/
public static PhotoAutoprocessingDto load(Serializable content) {
PhotoAutoprocessingDto photoAutoprocessingDto = null;
if (content instanceof Properties) {
Properties properties = (Properties) content;
String outDir = properties.getProperty(KEY_OUT_DIR);
photoAutoprocessingDto = new PhotoAutoprocessingDto((outDir != null) ? FileFacade.convert(new File(outDir)) : null, properties);
}
if (LibGlobal.debugEnabled) {
logger.debug(PhotoAutoprocessingDto.class.getSimpleName() + ": load De-Serialize:" + photoAutoprocessingDto);
}
return photoAutoprocessingDto;
}

public static File getApmFile(File outDir) {
return new File(outDir, RuleFileNameProcessor.APM_FILE_NAME);
public PhotoAutoprocessingDto load(IFile outDir) throws IOException {
this.outDir = outDir;
IFile apm = getApmFile();
properties.clear();
if (apm.exists() && apm.isFile() && apm.canRead()) {
properties.load(apm.openInputStream());
if (LibGlobal.debugEnabled) {
logger.debug(this.getClass().getSimpleName() + ": loaded from " + apm + ":" + this);
}
return this;
}
return null;
}

private IFile getApmFile() {
return getApmFile(this.outDir);
}

/** if has no data the file is deleted */
public void save() throws IOException {
File apm = getApmFile();
FileOutputStream stream = null;
IFile apm = getApmFile();
OutputStream stream = null;
if (isEmpty()) {
if (LibGlobal.debugEnabled) {
logger.debug(this.getClass().getSimpleName() + ": save delete empty " + apm + ":" + this);
Expand All @@ -127,28 +143,14 @@ public void save() throws IOException {
if (LibGlobal.debugEnabled) {
logger.debug(this.getClass().getSimpleName() + ": save to " + apm + ":" + this);
}
stream = new FileOutputStream(apm);
stream = apm.openOutputStream();
properties.store(stream, PhotoAutoprocessingDto.sFileComment);
} finally {
FileUtils.close(stream, "PhotoAutoprocessingDto.load(" + apm + ")");
}
}
}

/** Android support: to persist state and to transfer activites via intent. */
public static PhotoAutoprocessingDto load(Serializable content) {
PhotoAutoprocessingDto photoAutoprocessingDto = null;
if (content instanceof Properties ) {
Properties properties = (Properties) content;
String outDir = properties.getProperty(KEY_OUT_DIR);
photoAutoprocessingDto = new PhotoAutoprocessingDto((outDir != null) ? new File(outDir) : null, properties);
}
if (LibGlobal.debugEnabled) {
logger.debug(PhotoAutoprocessingDto.class.getSimpleName() + ": load De-Serialize:" + photoAutoprocessingDto);
}
return photoAutoprocessingDto;
}

/** DateFormat part for {@link RuleFileNameProcessor} */
public String getDateFormat() {
return getProperty(KEY_DATE_FORMAT);
Expand Down Expand Up @@ -182,10 +184,11 @@ public PhotoAutoprocessingDto setNumberFormat(String NumberFormat) {
return this;
}

public File getOutDir() {
public IFile getOutDir() {
return outDir;
}
public PhotoAutoprocessingDto setOutDir(File outDir) {

public PhotoAutoprocessingDto setOutDir(IFile outDir) {
this.outDir = outDir;
return this;
}
Expand Down
Loading

0 comments on commit d245461

Please sign in to comment.