Search in sources :

Example 11 with MediaFile

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());
            }
        }
    }
}
Also used : MediaFile(org.odk.collect.forms.MediaFile) InputStream(java.io.InputStream) MediaFile(org.odk.collect.forms.MediaFile) File(java.io.File)

Aggregations

MediaFile (org.odk.collect.forms.MediaFile)11 File (java.io.File)9 ManifestFile (org.odk.collect.forms.ManifestFile)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 Test (org.junit.Test)8 Analytics (org.odk.collect.analytics.Analytics)8 FormSource (org.odk.collect.forms.FormSource)7 Form (org.odk.collect.forms.Form)3 FormUtils.buildForm (org.odk.collect.formstest.FormUtils.buildForm)3 FormSourceException (org.odk.collect.forms.FormSourceException)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 DocumentFetchResult (org.odk.collect.android.utilities.DocumentFetchResult)1