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;
}
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;
}
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);
}
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());
}
Aggregations