Skip to content

Commit

Permalink
#169: Experiment to trace file rename
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Sep 28, 2020
1 parent 1c6f767 commit 1f51e1c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
package de.k3b.android.androFotoFinder.media;

import android.content.ContentValues;
import android.util.Log;

import java.io.IOException;

import de.k3b.android.androFotoFinder.Global;
import de.k3b.android.androFotoFinder.queries.FotoSql;
import de.k3b.io.FileUtils;
import de.k3b.io.VISIBILITY;
import de.k3b.io.collections.SelectedFiles;
import de.k3b.io.filefacade.IFile;
import de.k3b.media.ExifInterfaceEx;

public class AndroidExifInterfaceEx extends ExifInterfaceEx {
public static boolean DBG_ENABLED = true;

private boolean overwriteOriginal;
private String inPath;
private String outPath;

public static void init() {
setFactory(new Factory() {
@Override
Expand All @@ -11,4 +28,62 @@ public ExifInterfaceEx create() {
}
});
}

@Override
protected IFile renameSouraceFileBeforeReplaceOrThrow(IFile oldSourcefile, String newName) throws IOException {
debugIdPaths("renameSouraceFileBeforeReplaceOrThrow begin", oldSourcefile.getAbsolutePath(), newName);
this.overwriteOriginal = true;
this.inPath = oldSourcefile.getAbsolutePath();
this.outPath = this.inPath + TMP_FILE_SUFFIX;

if (!renameInDatabase(":renameSouraceFileBeforeReplaceOrThrow", this.inPath, this.outPath)) {
this.outPath = null; // failed
}

final IFile result = super.renameSouraceFileBeforeReplaceOrThrow(oldSourcefile, newName);
debugIdPaths("renameSouraceFileBeforeReplaceOrThrow end", oldSourcefile.getAbsolutePath(), newName);
return result;
}

@Override
protected void beforeCloseSaveOutputStream() {
if (this.outPath != null) {
renameInDatabase(":beforeCloseSaveOutputStream", this.outPath, this.inPath);
this.outPath = null;
}
super.beforeCloseSaveOutputStream();
}

private boolean renameInDatabase(String dbgContext, String fromPath, String toPath) {
debugIdPaths(dbgContext + " renameInDatabase begin", fromPath, toPath);
ContentValues values = new ContentValues();
values.put(FotoSql.SQL_COL_PATH, toPath);
final int execResultCount = FotoSql.getMediaDBApi().
execUpdate(this.getClass().getSimpleName() + dbgContext, fromPath, values, null);

debugIdPaths(dbgContext + " renameInDatabase end " + execResultCount, fromPath, toPath);
if ((execResultCount != 1) && DBG_ENABLED) {
// !!!! debug ausgabe path+ id failed
}
return 1 == execResultCount;
}

private void debugIdPaths(String dbgContext, String... paths) {
StringBuilder sqlWhere = new StringBuilder();
for (String path : paths) {
if (sqlWhere.length() > 0) {
sqlWhere.append(" OR ");
}
sqlWhere.append("(").append(FotoSql.SQL_COL_PATH).append(" like '")
.append(FileUtils.replaceExtension(path, "")).append("%')");
}

// to prevent adding visibility
sqlWhere.append(" and " +
FotoSql.SQL_COL_EXT_MEDIA_TYPE +
" is not null");
final SelectedFiles selectedfiles = FotoSql.getSelectedfiles(sqlWhere.toString(), VISIBILITY.PRIVATE_PUBLIC);
Log.d(Global.LOG_CONTEXT, dbgContext + ": "
+ selectedfiles.toIdString() + " -> " + selectedfiles.toPathListString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1127,9 +1127,9 @@ public static String getUriString(long imageID) {
return SQL_TABLE_EXTERNAL_CONTENT_URI_FILE_NAME + "/" + imageID;
}

public static SelectedFiles getSelectedfiles(String sqlWhere, VISIBILITY visibility) {
public static SelectedFiles getSelectedfiles(String sqlWhere, VISIBILITY visibility, String... parameters) {
QueryParameter query = new QueryParameter(FotoSql.queryChangePath);
query.addWhere(sqlWhere);
query.addWhere(sqlWhere, parameters);
query.addOrderBy(FotoSql.SQL_COL_PATH);

return getSelectedfiles(query, FotoSql.SQL_COL_PATH, visibility);
Expand Down
8 changes: 5 additions & 3 deletions fotolib2/src/main/java/de/k3b/database/QueryParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,11 @@ public String toString() {
/************************** local helpers *********************/

private QueryParameter addToList(final List<String> list, boolean allowNull, final String[] parameters) {
for (String parameter : parameters) {
if ((allowNull) || (parameter != null)) {
list.add(parameter);
if (parameters != null) {
for (String parameter : parameters) {
if ((allowNull) || (parameter != null)) {
list.add(parameter);
}
}
}
return this;
Expand Down
39 changes: 27 additions & 12 deletions fotolib2/src/main/java/de/k3b/media/ExifInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,25 @@ public class ExifInterface {
// false for unittests because UserComment = null is not implemented for COM - Marker
protected static boolean fixDateOnSave = true;

// Used when overwriting original file
protected static final String TMP_FILE_SUFFIX = ".tmp";

// The Exif tag names
/** Type is String. */
/**
* Type is String.
*/
public static final String TAG_ARTIST = "Artist";
/** Type is int. @hide */
/**
* Type is int. @hide
*/
public static final String TAG_BITS_PER_SAMPLE = "BitsPerSample";
/** Type is int. @hide */
/**
* Type is int. @hide
*/
public static final String TAG_COMPRESSION = "Compression";
/** Type is String. */
/**
* Type is String.
*/
public static final String TAG_COPYRIGHT = "Copyright";
/** Type is String. @hide */
public static final String TAG_DATETIME = "DateTime";
Expand Down Expand Up @@ -1188,9 +1199,8 @@ public void saveAttributes(IFile inFile, IFile outFile, boolean deleteInFileOnFi

if (overwriteOriginal) {
final String name = inFile.getName();
final String tempName = name + ".tmp";
renameOrThrow(inFile, tempName);
inFile = inFile.getParentFile().create(tempName);
final String tempName = name + TMP_FILE_SUFFIX;
inFile = renameSouraceFileBeforeReplaceOrThrow(inFile, tempName);

currentOutFile = outFile.getParentFile().create(name);
}
Expand All @@ -1207,13 +1217,14 @@ public void saveAttributes(IFile inFile, IFile outFile, boolean deleteInFileOnFi
mThumbnailBytes = null;
}

private void renameOrThrow(IFile file, String newName) throws IOException {
logDebug(String.format("rename %s to %s", file, newName));
protected IFile renameSouraceFileBeforeReplaceOrThrow(IFile oldSourcefile, String newName) throws IOException {
logDebug(String.format("rename %s to %s", oldSourcefile, newName));

if (!file.renameTo(newName)) {
throw new IOException("Could'nt rename sourcefile from " + file +
if (!oldSourcefile.renameTo(newName)) {
throw new IOException("Could'nt rename sourcefile from " + oldSourcefile +
" to " + newName);
}
return oldSourcefile.getParentFile().create(newName);
}

/** repairs wrong/missing attributes */
Expand Down Expand Up @@ -1719,11 +1730,15 @@ public void saveJpegAttributes(InputStream inputStream, OutputStream outputStrea
}
}
} finally {
closeSilently(dataOutputStream, "ExifInterface saveJpegAttributes out " + outputStream);
closeSilently(dataInputStream, "ExifInterface saveJpegAttributes in " + dataInputStream);
beforeCloseSaveOutputStream();
closeSilently(dataOutputStream, "ExifInterface saveJpegAttributes out " + outputStream);
}
}

protected void beforeCloseSaveOutputStream() {
}

// Reads the given EXIF byte area and save its tag data into attributes.
private void readExifSegment(byte[] exifBytes, int exifOffsetFromBeginning) throws IOException {
// Parse TIFF Headers. See JEITA CP-3451C Table 1. page 10.
Expand Down

0 comments on commit 1f51e1c

Please sign in to comment.