Search in sources :

Example 1 with FeedFile

use of de.danoeh.antennapod.core.feed.FeedFile in project AntennaPod by AntennaPod.

the class HttpDownloaderTest method setupFeedFile.

private FeedFileImpl setupFeedFile(String downloadUrl, String title, boolean deleteExisting) {
    FeedFileImpl feedfile = new FeedFileImpl(downloadUrl);
    String fileUrl = new File(destDir, title).getAbsolutePath();
    File file = new File(fileUrl);
    if (deleteExisting) {
        Log.d(TAG, "Deleting file: " + file.delete());
    }
    feedfile.setFile_url(fileUrl);
    return feedfile;
}
Also used : FeedFile(de.danoeh.antennapod.core.feed.FeedFile) File(java.io.File)

Example 2 with FeedFile

use of de.danoeh.antennapod.core.feed.FeedFile in project AntennaPod by AntennaPod.

the class HttpDownloaderTest method download.

private Downloader download(String url, String title, boolean expectedResult, boolean deleteExisting, String username, String password, boolean deleteOnFail) {
    FeedFile feedFile = setupFeedFile(url, title, deleteExisting);
    DownloadRequest request = new DownloadRequest(feedFile.getFile_url(), url, title, 0, feedFile.getTypeAsInt(), username, password, deleteOnFail, null);
    Downloader downloader = new HttpDownloader(request);
    downloader.call();
    DownloadStatus status = downloader.getResult();
    assertNotNull(status);
    assertTrue(status.isSuccessful() == expectedResult);
    assertTrue(status.isDone());
    // the file should not exist if the download has failed and deleteExisting was true
    assertTrue(!deleteExisting || new File(feedFile.getFile_url()).exists() == expectedResult);
    return downloader;
}
Also used : HttpDownloader(de.danoeh.antennapod.core.service.download.HttpDownloader) DownloadStatus(de.danoeh.antennapod.core.service.download.DownloadStatus) FeedFile(de.danoeh.antennapod.core.feed.FeedFile) DownloadRequest(de.danoeh.antennapod.core.service.download.DownloadRequest) Downloader(de.danoeh.antennapod.core.service.download.Downloader) HttpDownloader(de.danoeh.antennapod.core.service.download.HttpDownloader) FeedFile(de.danoeh.antennapod.core.feed.FeedFile) File(java.io.File)

Example 3 with FeedFile

use of de.danoeh.antennapod.core.feed.FeedFile in project AntennaPod by AntennaPod.

the class DownloadRequester method download.

private void download(Context context, FeedFile item, FeedFile container, File dest, boolean overwriteIfExists, String username, String password, String lastModified, boolean deleteOnFailure, Bundle arguments) {
    final boolean partiallyDownloadedFileExists = item.getFile_url() != null;
    if (isDownloadingFile(item)) {
        Log.e(TAG, "URL " + item.getDownload_url() + " is already being downloaded");
        return;
    }
    if (!isFilenameAvailable(dest.toString()) || (!partiallyDownloadedFileExists && dest.exists())) {
        Log.d(TAG, "Filename already used.");
        if (isFilenameAvailable(dest.toString()) && overwriteIfExists) {
            boolean result = dest.delete();
            Log.d(TAG, "Deleting file. Result: " + result);
        } else {
            // find different name
            File newDest = null;
            for (int i = 1; i < Integer.MAX_VALUE; i++) {
                String newName = FilenameUtils.getBaseName(dest.getName()) + "-" + i + FilenameUtils.EXTENSION_SEPARATOR + FilenameUtils.getExtension(dest.getName());
                Log.d(TAG, "Testing filename " + newName);
                newDest = new File(dest.getParent(), newName);
                if (!newDest.exists() && isFilenameAvailable(newDest.toString())) {
                    Log.d(TAG, "File doesn't exist yet. Using " + newName);
                    break;
                }
            }
            if (newDest != null) {
                dest = newDest;
            }
        }
    }
    Log.d(TAG, "Requesting download of url " + item.getDownload_url());
    String baseUrl = (container != null) ? container.getDownload_url() : null;
    item.setDownload_url(URLChecker.prepareURL(item.getDownload_url(), baseUrl));
    DownloadRequest.Builder builder = new DownloadRequest.Builder(dest.toString(), item).withAuthentication(username, password).lastModified(lastModified).deleteOnFailure(deleteOnFailure).withArguments(arguments);
    DownloadRequest request = builder.build();
    download(context, request);
}
Also used : DownloadRequest(de.danoeh.antennapod.core.service.download.DownloadRequest) FeedFile(de.danoeh.antennapod.core.feed.FeedFile) File(java.io.File)

Example 4 with FeedFile

use of de.danoeh.antennapod.core.feed.FeedFile in project AntennaPod by AntennaPod.

the class HttpDownloaderTest method testCancel.

public void testCancel() {
    final String url = HTTPBin.BASE_URL + "/delay/3";
    FeedFileImpl feedFile = setupFeedFile(url, "delay", true);
    final Downloader downloader = new HttpDownloader(new DownloadRequest(feedFile.getFile_url(), url, "delay", 0, feedFile.getTypeAsInt()));
    Thread t = new Thread() {

        @Override
        public void run() {
            downloader.call();
        }
    };
    t.start();
    downloader.cancel();
    try {
        t.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    DownloadStatus result = downloader.getResult();
    assertTrue(result.isDone());
    assertFalse(result.isSuccessful());
    assertTrue(result.isCancelled());
    assertFalse(new File(feedFile.getFile_url()).exists());
}
Also used : HttpDownloader(de.danoeh.antennapod.core.service.download.HttpDownloader) DownloadStatus(de.danoeh.antennapod.core.service.download.DownloadStatus) Downloader(de.danoeh.antennapod.core.service.download.Downloader) HttpDownloader(de.danoeh.antennapod.core.service.download.HttpDownloader) DownloadRequest(de.danoeh.antennapod.core.service.download.DownloadRequest) FeedFile(de.danoeh.antennapod.core.feed.FeedFile) File(java.io.File)

Aggregations

FeedFile (de.danoeh.antennapod.core.feed.FeedFile)4 File (java.io.File)4 DownloadRequest (de.danoeh.antennapod.core.service.download.DownloadRequest)3 DownloadStatus (de.danoeh.antennapod.core.service.download.DownloadStatus)2 Downloader (de.danoeh.antennapod.core.service.download.Downloader)2 HttpDownloader (de.danoeh.antennapod.core.service.download.HttpDownloader)2