Remove partial download of different versions of the file
CHANGELOG.md
| 16 | 16 | ||
| 17 | 17 | * When a file is already downloaded, updating it will append to it instead | |
| 18 | 18 | of overriding it. | |
| 19 | + | * When a file is partially downloaded but updated on server, remove the old | |
| 20 | + | partial file as continuing download would cause a hash mismatch. | |
| 19 | 21 | ||
| 20 | 22 | Changes In 0.2.1 | |
| 21 | 23 | ----------------- |
app/src/main/java/eu/lepiller/nani/DictionaryDownloadActivity.java
| 195 | 195 | input = connection.getInputStream(); | |
| 196 | 196 | file = new File(e.getValue() + ".sha256"); | |
| 197 | 197 | file.getParentFile().mkdirs(); | |
| 198 | + | File old_file = new File(file + ".old"); | |
| 199 | + | if(old_file.exists()) { | |
| 200 | + | old_file.delete(); | |
| 201 | + | } | |
| 202 | + | if(file.exists()) { | |
| 203 | + | file.renameTo(old_file); | |
| 204 | + | } | |
| 198 | 205 | output = new FileOutputStream(file); | |
| 199 | 206 | while((count = input.read(data)) != -1) { | |
| 200 | 207 | if (isCancelled()) { | |
… | |||
| 203 | 210 | } | |
| 204 | 211 | output.write(data, 0, count); | |
| 205 | 212 | } | |
| 213 | + | ||
| 214 | + | if(old_file.exists()) { | |
| 215 | + | // Check that we continue to download the same file | |
| 216 | + | String old_hash = Dictionary.readSha256FromFile(old_file); | |
| 217 | + | String new_hash = Dictionary.readSha256FromFile(file); | |
| 218 | + | ||
| 219 | + | if(old_hash.compareTo(new_hash) != 0) { | |
| 220 | + | // Remove file that is now different | |
| 221 | + | d.remove(); | |
| 222 | + | } | |
| 223 | + | ||
| 224 | + | old_file.delete(); | |
| 225 | + | } | |
| 206 | 226 | } | |
| 207 | 227 | ||
| 208 | 228 | // .sha256 file now downloaded | |
app/src/main/java/eu/lepiller/nani/dictionary/Dictionary.java
| 3 | 3 | import android.content.Context; | |
| 4 | 4 | import android.graphics.drawable.Drawable; | |
| 5 | 5 | import android.os.Build; | |
| 6 | + | import android.util.Log; | |
| 7 | + | ||
| 6 | 8 | import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat; | |
| 7 | 9 | ||
| 8 | 10 | import java.io.File; | |
| 11 | + | import java.io.FileNotFoundException; | |
| 12 | + | import java.io.FileReader; | |
| 13 | + | import java.io.IOException; | |
| 9 | 14 | import java.net.URL; | |
| 10 | 15 | import java.util.Map; | |
| 11 | 16 | ||
… | |||
| 75 | 80 | abstract int getDrawableId(); | |
| 76 | 81 | ||
| 77 | 82 | abstract public void remove(); | |
| 83 | + | ||
| 84 | + | public static String readSha256FromFile(File file) throws IOException { | |
| 85 | + | StringBuilder sb = new StringBuilder(); | |
| 86 | + | char data[] = new char[4096]; | |
| 87 | + | FileReader fr = new FileReader(file); | |
| 88 | + | int count; | |
| 89 | + | while((count = fr.read(data)) != -1) { | |
| 90 | + | sb.append(data, 0, count); | |
| 91 | + | } | |
| 92 | + | ||
| 93 | + | return sb.toString().trim(); | |
| 94 | + | } | |
| 78 | 95 | } | |
app/src/main/java/eu/lepiller/nani/dictionary/FileDictionary.java
| 73 | 73 | byte[] result = digest.digest(); | |
| 74 | 74 | String hash = String.format("%0" + (result.length*2) + "X", new BigInteger(1, result)).toLowerCase(); | |
| 75 | 75 | ||
| 76 | - | StringBuilder sb = new StringBuilder(); | |
| 77 | 76 | try { | |
| 78 | - | char data[] = new char[4096]; | |
| 79 | - | FileReader fr = new FileReader(sha256); | |
| 80 | - | int count; | |
| 81 | - | while((count = fr.read(data)) != -1) { | |
| 82 | - | sb.append(data, 0, count); | |
| 83 | - | } | |
| 77 | + | String expected = readSha256FromFile(sha256); | |
| 84 | 78 | ||
| 85 | 79 | // If the file has the expected hash, we have it | |
| 86 | - | Log.d(TAG, "expected: " + sb.toString().trim() + "; actual: " + hash); | |
| 87 | - | return sb.toString().trim().compareTo(hash) == 0; | |
| 80 | + | Log.d(TAG, "expected: " + expected + "; actual: " + hash); | |
| 81 | + | return expected.compareTo(hash) == 0; | |
| 88 | 82 | } catch (FileNotFoundException e) { | |
| 89 | 83 | e.printStackTrace(); | |
| 90 | 84 | } catch (IOException e) { |