Skip to content

Commit

Permalink
#108: Zip-file export/backup unfinished feature temporary suspended
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Feb 11, 2019
1 parent 198343f commit 3815dbf
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,25 @@

package de.k3b.android.androFotoFinder.backup;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

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

import de.k3b.android.androFotoFinder.media.MediaCursor;
import de.k3b.android.androFotoFinder.queries.FotoSql;
import de.k3b.android.androFotoFinder.tagDB.TagSql;
import de.k3b.database.QueryParameter;
import de.k3b.io.FileUtils;
import de.k3b.io.IItemSaver;
import de.k3b.media.IMetaApi;
import de.k3b.media.Media2ExistingFileSaver;
import de.k3b.media.MediaCsvStringSaver;
import de.k3b.zip.IZipConfig;
import de.k3b.zip.LibZipGlobal;
import de.k3b.zip.ZipConfigRepository;
Expand Down Expand Up @@ -55,10 +66,20 @@ public static IZipConfig loadZipConfig(Uri uri, Context context) {
return null;
}

public static IZipConfig execute(IZipConfig mFilter) {
ZipConfigRepository repo = new ZipConfigRepository(mFilter);
public static IZipConfig execute(IZipConfig zipConfig, ContentResolver contentResolver) {
ZipConfigRepository repo = new ZipConfigRepository(zipConfig);
final File zipConfigFile = repo.getZipConfigFile();
if (zipConfigFile != null) {
QueryParameter filter = getEffectiveQueryParameter(zipConfig);

final MediaCsvStringSaver csv = new MediaCsvStringSaver();

/// !!! todo go on here
final IItemSaver<File> file2Zip = null;
final Media2ExistingFileSaver media2file = new Media2ExistingFileSaver(file2Zip);

execQuery(filter, contentResolver, csv, media2file);

if (repo.save()) {
if (LibZipGlobal.debugEnabled) {
Log.d(LibZipGlobal.LOG_TAG, mDebugPrefix + " Saved as " + repo);
Expand All @@ -68,4 +89,58 @@ public static IZipConfig execute(IZipConfig mFilter) {
}
return null;
}

/**
* @return get query without filte-DateModified-min/max and with added zipConfig.getDateModifiedFrom
*/
private static QueryParameter getEffectiveQueryParameter(IZipConfig zipConfig) {
QueryParameter filter = QueryParameter.parse(zipConfig.getFilter());
FotoSql.parseDateModifiedMax(filter, true);
FotoSql.parseDateModifiedMin(filter, true);
final Date dateModifiedFrom = zipConfig.getDateModifiedFrom();
if (dateModifiedFrom != null) {
FotoSql.addWhereDateModifiedMinMax(filter, dateModifiedFrom.getTime(), 0);
}

filter.clearColumns().addColumn(TagSql.SQL_COL_PK
,TagSql.SQL_COL_PATH
,TagSql.SQL_COL_DATE_TAKEN
,TagSql.SQL_COL_EXT_TITLE
,TagSql.SQL_COL_EXT_DESCRIPTION
,TagSql.SQL_COL_EXT_TAGS
,TagSql.SQL_COL_LAT
,TagSql.SQL_COL_LON
,TagSql.SQL_COL_EXT_RATING
,TagSql.SQL_COL_EXT_MEDIA_TYPE);

return filter;
}

private static void execQuery(QueryParameter query, ContentResolver contentResolver,
IItemSaver<IMetaApi>... consumers) {
Cursor cursor = null;
try {
cursor = contentResolver.query(Uri.parse(query.toFrom()), query.toColumns(),
query.toAndroidWhere(), query.toAndroidParameters(), query.toOrderBy());

int itemCount = cursor.getCount();
final int expectedCount = itemCount + itemCount;

MediaCursor mediaItem = new MediaCursor(cursor);
while (cursor.moveToNext()) {
for (IItemSaver<IMetaApi> consumer : consumers){
if (consumer != null) consumer.save(mediaItem);
}
}
} catch (Exception ex){
Log.e(LibZipGlobal.LOG_TAG, mDebugPrefix + query, ex);
throw new RuntimeException(mDebugPrefix + query + "\n" + ex.getMessage(), ex);
} finally {
if (cursor != null) {
cursor.close();
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ private boolean onOk() {
Activity ctx = this;
saveGuiToData();
gui.mHistory.saveHistory();
IZipConfig newConfig = Backup2ZipService.execute(mFilter);
IZipConfig newConfig = Backup2ZipService.execute(mFilter, ctx.getContentResolver());
if (newConfig != null) {
Toast.makeText(BackupActivity.this, newConfig.toString(), Toast.LENGTH_LONG).show();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,14 +390,22 @@ public static IGalleryFilter parseQuery(QueryParameter query, boolean removeFrom
filter.setPath(getFilePath(query, removeFromSourceQuery));

// SQL_COL_LAST_MODIFIED in seconds since 1970; translate from MilliSecs
filter.setDateModified(getParamWithFix(query, FILTER_EXPR_DATE_MODIFIED_MIN, removeFromSourceQuery, LAST_MODIFIED_FACTOR),
getParamWithFix(query, FILTER_EXPR_DATE_MODIFIED_MAX, removeFromSourceQuery, LAST_MODIFIED_FACTOR));
filter.setDateModified(parseDateModifiedMin(query, removeFromSourceQuery),
parseDateModifiedMax(query, removeFromSourceQuery));

return filter;
}
return null;
}

public static long parseDateModifiedMax(QueryParameter query, boolean removeFromSourceQuery) {
return getParamWithFix(query, FILTER_EXPR_DATE_MODIFIED_MAX, removeFromSourceQuery, LAST_MODIFIED_FACTOR);
}

public static long parseDateModifiedMin(QueryParameter query, boolean removeFromSourceQuery) {
return getParamWithFix(query, FILTER_EXPR_DATE_MODIFIED_MIN, removeFromSourceQuery, LAST_MODIFIED_FACTOR);
}

/** extracts geo infos from srcQuery to destFilter */
public static GeoRectangle parseQueryGeo(QueryParameter srcQuery, GeoRectangle destFilter, boolean removeFromSourceQuery) {
if (null != getParams(srcQuery, FILTER_EXPR_NO_GPS, removeFromSourceQuery)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<!-- image commands always available -->
<item android:id="@+id/cmd_backup"
android:title="@string/backup_title"
android:visible="true"
android:visible="false"
android:orderInCategory="41"
android:showAsAction="never" />

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/menu/menu_gallery_non_selected_only.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<!-- image commands always available -->
<item android:id="@+id/cmd_backup"
android:title="@string/backup_title"
android:visible="true"
android:visible="false"
android:orderInCategory="41"
android:showAsAction="never" />

Expand Down
25 changes: 25 additions & 0 deletions fotolib2/src/main/java/de/k3b/io/IItemSaver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package de.k3b.io;

/*
* Copyright (c) 2019 by k3b.
*
* This file is part of AndroFotoFinder.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>
*/

/* abstract api to process an item */
public interface IItemSaver<T> {
boolean save(T item);
}
34 changes: 34 additions & 0 deletions fotolib2/src/main/java/de/k3b/media/Media2ExistingFileSaver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.k3b.media;

import java.io.File;

import de.k3b.io.FileProcessor;
import de.k3b.io.IItemSaver;

public class Media2ExistingFileSaver implements IItemSaver<IMetaApi> {
private final IItemSaver<File> fileSaver;
public Media2ExistingFileSaver(IItemSaver<File> fileSaver) {
this.fileSaver = fileSaver;
}

@Override
public boolean save(IMetaApi item) {
if (item != null) {
String path = item.getPath();
if (path != null) {
return saveFiles(new File(path),
FileProcessor.getExistingSidecarOrNull(path, true),
FileProcessor.getExistingSidecarOrNull(path, false)) > 0;
}
}
return false;
}

private int saveFiles(File... files) {
int processed = 0;
for (File f: files) {
if ((f != null) && (f.exists()) && f.canRead() && this.fileSaver.save(f)) processed++;
}
return processed;
}
}
19 changes: 14 additions & 5 deletions fotolib2/src/main/java/de/k3b/media/MediaCsvSaver.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2016 by k3b.
* Copyright (c) 2015-2019 by k3b.
*
* This file is part of AndroFotoFinder.
*
Expand All @@ -23,30 +23,39 @@
import java.util.Arrays;

import de.k3b.csv2db.csv.CsvItem;
import de.k3b.io.IItemSaver;

/**
* Created by k3b on 13.10.2016.
*/

public class MediaCsvSaver {
private final PrintWriter printer;
public class MediaCsvSaver implements IItemSaver<IMetaApi> {
private PrintWriter printer;
private final MediaCsvItem csvLine;

public MediaCsvSaver(PrintWriter printer) {
this.printer = printer;
setPrinter(printer);
csvLine = new MediaCsvItem();
defineHeader(MediaCsvItem.MEDIA_CSV_STANDARD_HEADER);
}

public void save(IMetaApi item) {
protected MediaCsvSaver setPrinter(PrintWriter printer) {
this.printer = printer;
return this;
}

@Override
public boolean save(IMetaApi item) {
if (item != null) {
csvLine.clear();
MediaUtil.copy(csvLine, item, true, true);
if (!csvLine.isEmpty()) {
this.printer.write(csvLine.toString());
this.printer.write(CsvItem.DEFAULT_CHAR_LINE_DELIMITER);
return true;
}
}
return false;
}

private void defineHeader(String header) {
Expand Down
18 changes: 18 additions & 0 deletions fotolib2/src/main/java/de/k3b/media/MediaCsvStringSaver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.k3b.media;

import java.io.PrintWriter;
import java.io.StringWriter;

public class MediaCsvStringSaver extends MediaCsvSaver{
private StringWriter result = new StringWriter();
private PrintWriter writer = new PrintWriter(result);
public MediaCsvStringSaver() {
super(null);
this.setPrinter(writer);
}

@Override
public String toString() {
return result.toString();
}
}

0 comments on commit 3815dbf

Please sign in to comment.