use of org.odk.collect.forms.MediaFile in project collect by opendatakit.
the class ServerFormDownloader method downloadMediaFiles.
private void downloadMediaFiles(String tempMediaPath, FormDownloaderListener stateListener, List<MediaFile> files, File tempDir, String formFileName) throws IOException, FormDownloadException.DownloadingInterrupted, FormSourceException {
File tempMediaDir = new File(tempMediaPath);
tempMediaDir.mkdir();
for (int i = 0; i < files.size(); i++) {
if (stateListener != null) {
stateListener.progressUpdate("", String.valueOf(i + 1), "");
}
MediaFile toDownload = files.get(i);
File tempMediaFile = new File(tempMediaDir, toDownload.getFilename());
String finalMediaPath = FileUtils.constructMediaPath(formsDirPath + File.separator + formFileName);
File finalMediaFile = new File(finalMediaPath, toDownload.getFilename());
if (!finalMediaFile.exists()) {
InputStream mediaFile = formSource.fetchMediaFile(toDownload.getDownloadUrl());
writeFile(mediaFile, tempMediaFile, tempDir, stateListener);
} else {
String currentFileHash = Md5.getMd5Hash(finalMediaFile);
String downloadFileHash = validateHash(toDownload.getHash());
if (currentFileHash != null && downloadFileHash != null && !currentFileHash.contentEquals(downloadFileHash)) {
// if the hashes match, it's the same file otherwise replace it with the new one
InputStream mediaFile = formSource.fetchMediaFile(toDownload.getDownloadUrl());
writeFile(mediaFile, tempMediaFile, tempDir, stateListener);
} else {
// exists, and the hash is the same
// no need to download it again
Timber.i("Skipping media file fetch -- file hashes identical: %s", finalMediaFile.getAbsolutePath());
}
}
}
}
Aggregations