Commit da19a0d0 authored by Matyáš Latner's avatar Matyáš Latner

#14 Files order in assets checker fix

parent 5874d4cc
......@@ -14,6 +14,7 @@ import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import cz.nic.tablexia.TablexiaSettings;
import cz.nic.tablexia.checksum.Checksum;
import cz.nic.tablexia.loader.IApplicationLoader;
import cz.nic.tablexia.loader.TablexiaAbstractFileManager;
......@@ -35,18 +36,18 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic
private static class ZipAssetLoaderTask implements AsyncTask<Void> {
private Locale locale;
private Map<String, String> buildChecksum;
private Map<String, String> buildChecksums;
public ZipAssetLoaderTask(Locale locale, Map<String, String> buildChecksum) {
public ZipAssetLoaderTask(Locale locale, Map<String, String> buildChecksums) {
this.locale = locale;
this.buildChecksum = buildChecksum;
this.buildChecksums = buildChecksums;
}
private boolean checkAssets(String buildChecksum, String extractDestinationDirectory) {
File file = new File(extractDestinationDirectory);
if (file.exists()) {
if (file.exists() && file.isDirectory()) {
try {
String runtimeChecksum = Checksum.getMd5OfDir(file.getAbsolutePath());
String runtimeChecksum = Checksum.getMd5OfDir(file);
Log.debug(getClass(), "Comparing assets checksums: [BUILD: " + buildChecksum + "] - [RUNTIME: " + runtimeChecksum + "]");
return runtimeChecksum.equals(buildChecksum);
} catch (NoSuchAlgorithmException e) {
......@@ -61,25 +62,52 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic
@Override
public Void call() throws Exception {
String language = locale.getLanguage();
String commonZipFile = COMMON_ZIP_FILE_NAME + ZIP_FILE_EXTENSION;
String localisedZipFile = language + ZIP_FILE_EXTENSION;
String extractDestinationDirectory = TablexiaAbstractFileManager.getFileStoragePath(TablexiaAbstractFileManager.AssetsStorageType.EXTERNAL);
if (buildChecksum == null || !checkAssets(buildChecksum.get(language), extractDestinationDirectory)) {
File eddFile = new File(extractDestinationDirectory);
if (eddFile.exists()) {
Log.info(getClass(), "Assets check FAILED! --> Deleting content under: " + extractDestinationDirectory);
deleteDirectory(eddFile);
Long startTime = null;
if (TablexiaSettings.getInstance().isDebug()) {
startTime = System.nanoTime();
}
// check current content
if (buildChecksums != null) {
String buildChecksum = buildChecksums.get(language);
if (buildChecksum != null) {
boolean checksumResult = checkAssets((buildChecksum), extractDestinationDirectory);
if (startTime != null) {
Log.debug(getClass(), String.format("Checksum delay: %05f s", (System.nanoTime() - startTime) / 1000000000.0));
}
if (checksumResult) {
Log.info(getClass(), "Assets check OK! --> Continue to loading");
return null;
}
} else {
Log.err(getClass(), "BUILD CHECKSUM: no build checksum specified for language " + language);
}
} else {
Log.err(getClass(), "BUILD CHECKSUM: no build checksum specified");
}
Log.info(getClass(), "Assets check FAILED! --> Extracting new assets to: " + extractDestinationDirectory);
if (TablexiaSettings.getInstance().isDebug()) {
startTime = System.nanoTime();
}
unzip(ZIP_FILES_STORAGE_TYPE.getResolver().resolve(ZIP_FILES_STORAGE_TYPE.getStoragePath() + commonZipFile), extractDestinationDirectory);
unzip(ZIP_FILES_STORAGE_TYPE.getResolver().resolve(ZIP_FILES_STORAGE_TYPE.getStoragePath() + localisedZipFile), extractDestinationDirectory);
} else {
Log.info(getClass(), "Assets check OK! --> Continue to loading");
// delete modified or old content
File eddFile = new File(extractDestinationDirectory);
if (eddFile.exists()) {
Log.info(getClass(), "Assets check FAILED! --> Deleting content under: " + extractDestinationDirectory);
deleteDirectory(eddFile);
}
// extract new content
Log.info(getClass(), "Assets check FAILED! --> Extracting new assets to: " + extractDestinationDirectory);
String commonZipFile = COMMON_ZIP_FILE_NAME + ZIP_FILE_EXTENSION;
String localisedZipFile = language + ZIP_FILE_EXTENSION;
unzip(ZIP_FILES_STORAGE_TYPE.getResolver().resolve(ZIP_FILES_STORAGE_TYPE.getStoragePath() + commonZipFile), extractDestinationDirectory);
unzip(ZIP_FILES_STORAGE_TYPE.getResolver().resolve(ZIP_FILES_STORAGE_TYPE.getStoragePath() + localisedZipFile), extractDestinationDirectory);
if (startTime != null) {
Log.debug(getClass(), String.format("Unzip delay: %05f s", (System.nanoTime() - startTime) / 1000000000.0));
}
return null;
......
......@@ -6,33 +6,38 @@ import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Comparator;
public class Checksum {
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
System.out.println(getMd5OfDir(args[0]));
System.out.println(getMd5OfDir(new File(args[0])));
}
public static String getMd5OfDir(String dir) throws NoSuchAlgorithmException, IOException {
String md5 = "";
File folder = new File(dir);
File[] files = folder.listFiles();
public static String getMd5OfDir(File dir) throws NoSuchAlgorithmException, IOException {
StringBuilder checksumBuilder = new StringBuilder();
File[] files = dir.listFiles();
Arrays.sort(files,
new Comparator<File>() {
public int compare(File a, File b) {
return a.getName().compareTo(b.getName());
}
});
for (int i = 0; i < files.length; i++) {
if ("tablexia.db".equals(files[i].getName())) {
continue;
} else if (files[i].isDirectory()) {
md5 = md5 + getMd5OfDir(files[i].getAbsolutePath());
if (files[i].isDirectory()) {
checksumBuilder.append(getMd5OfDir(files[i]));
} else {
md5 = md5 + getMd5OfFile(files[i].toString());
checksumBuilder.append(getMd5OfFile(files[i]));
}
}
return getMD5OfString(md5);
return getMD5OfString(checksumBuilder.toString());
}
public static String getMd5OfFile(String filePath) throws IOException, NoSuchAlgorithmException {
String returnVal = "";
InputStream input = new FileInputStream(filePath);
public static String getMd5OfFile(File file) throws IOException, NoSuchAlgorithmException {
StringBuilder returnVal = new StringBuilder();
InputStream input = new FileInputStream(file);
byte[] buffer = new byte[1024];
MessageDigest md5Hash = MessageDigest.getInstance("MD5");
int numRead = 0;
......@@ -46,14 +51,14 @@ public class Checksum {
byte [] md5Bytes = md5Hash.digest();
for (int i=0; i < md5Bytes.length; i++) {
returnVal += Integer.toString((md5Bytes[i] & 0xff) + 0x100, 16).substring(1);
returnVal.append(Integer.toString((md5Bytes[i] & 0xff) + 0x100, 16).substring(1));
}
return returnVal;
return returnVal.toString();
}
public static String getMD5OfString(String str) throws NoSuchAlgorithmException {
MessageDigest md5;
StringBuffer hexString = new StringBuffer();
StringBuilder hexString = new StringBuilder();
md5 = MessageDigest.getInstance("md5");
md5.reset();
md5.update(str.getBytes());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment