Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: improved working directory setting #32

Merged
merged 11 commits into from
Apr 2, 2024
Prev Previous commit
Next Next commit
fixed restore
SteRiccio committed Nov 28, 2023
commit 28155ec70ea6dead12f8868839923059f64533b4
Original file line number Diff line number Diff line change
@@ -69,7 +69,8 @@ protected Boolean runTask() throws Exception {
}
File infoFile = new File(unzippedDir, SurveyBackupJob.INFO_FILE_NAME);
infoFile.delete();
FileUtils.moveDirectory(unzippedDir, surveysDir);
File unzippedSurveysDir = new File(unzippedDir, AppDirs.SURVEYS_DIR_NAME);
FileUtils.moveDirectory(unzippedSurveysDir, surveysDir);
AndroidFiles.makeDiscoverable(surveysDir, context);
return true;
} else {
Original file line number Diff line number Diff line change
@@ -18,6 +18,8 @@
import java.util.List;

public abstract class AppDirs {

public static final String SURVEYS_DIR_NAME = "surveys";
public static final String PREFERENCE_KEY = "workingDir";
private static final String ENV_SECONDARY_STORAGE = "SECONDARY_STORAGE";

@@ -46,7 +48,7 @@ public static File surveyDatabasesDir(String surveyName, Context context) throws
}

public static File surveysDir(Context context) throws WorkingDirNotAccessible {
return new File(root(context), "surveys");
return new File(root(context), SURVEYS_DIR_NAME);
}

public static File surveyImagesDir(String surveyName, Context context) throws WorkingDirNotAccessible {
Original file line number Diff line number Diff line change
@@ -16,13 +16,13 @@
public class BackupGenerator {
private static final Logger LOG = Logger.getLogger(BackupGenerator.class.getName());

private File sourceDir;
private File surveysDir;
private String appVersion;
private File destFile;
private ZipOutputStream zipOutputStream;

public BackupGenerator(File sourceDir, String appVersion, File destFile) {
this.sourceDir = sourceDir;
public BackupGenerator(File surveysDir, String appVersion, File destFile) {
this.surveysDir = surveysDir;
this.appVersion = appVersion;
this.destFile = destFile;
}
@@ -38,10 +38,11 @@ public void generate() throws IOException {
}

private void addSourceFiles() throws IOException {
List<File> files = FileUtils.listFilesRecursively(sourceDir);
List<File> files = FileUtils.listFilesRecursively(surveysDir);
for (File file: files) {
String filePath = file.getAbsolutePath();
writeFile(file, filePath.substring(sourceDir.getAbsolutePath().length() + 1, filePath.length()));
String entryName = "surveys/" + filePath.substring(surveysDir.getAbsolutePath().length() + 1, filePath.length());
writeFile(file, entryName);
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.openforis.collect.android.util;

import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.Arrays;
import java.util.HashSet;
@@ -45,8 +47,9 @@ public void unzipAll() throws IOException {
try {
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
String entryName = entryName(zipEntry);
extractEntry(zipInputStream, entryName);
// keep folders hierarchy
String entryOutputFileName = zipEntry.getName().replace("/", File.separator);
extractEntry(zipInputStream, entryOutputFileName);
zipEntry = zipInputStream.getNextEntry();
}
} finally {
@@ -60,8 +63,9 @@ private String entryName(ZipEntry zipEntry) {
return i == -1 ? name : name.substring(i + 1);
}

private void extractEntry(ZipInputStream zipInputStream, String entryName) throws IOException {
File newFile = new File(outputFolder + File.separator + entryName);
private void extractEntry(ZipInputStream zipInputStream, String outputFileName) throws IOException {
File newFile = new File(outputFolder + File.separator + outputFileName);
FileUtils.forceMkdirParent(newFile);
OutputStream fos = new FileOutputStream(newFile);
write(zipInputStream, fos);
}