use of org.nzbhydra.searching.SearchResultEntity in project nzbhydra2 by theotherp.
the class Downloader method addBySearchResultIds.
@Transactional
public AddNzbsResponse addBySearchResultIds(List<AddFilesRequest.SearchResult> searchResults, String category) {
NzbAddingType addingType = downloaderConfig.getNzbAddingType();
List<Long> addedNzbs = new ArrayList<>();
try {
for (AddFilesRequest.SearchResult entry : searchResults) {
Long guid = Long.valueOf(entry.getSearchResultId());
String categoryToSend;
if (Strings.isNullOrEmpty(category) && !"N/A".equals(entry.getOriginalCategory())) {
categoryToSend = entry.getOriginalCategory();
} else {
categoryToSend = category;
}
if (addingType == NzbAddingType.UPLOAD) {
// Uploading NZBs can only be done via proxying
DownloadResult result = nzbHandler.getFileByGuid(guid, FileDownloadAccessType.PROXY, SearchSource.INTERNAL);
String externalId = addNzb(result.getContent(), result.getTitle(), categoryToSend);
result.getDownloadEntity().setExternalId(externalId);
nzbHandler.updateStatusByEntity(result.getDownloadEntity(), FileDownloadStatus.NZB_ADDED);
} else {
SearchResultEntity searchResultEntity = searchResultRepository.getOne(guid);
addLink(nzbHandler.getDownloadLink(guid, false, DownloadType.NZB), searchResultEntity.getTitle(), categoryToSend);
}
addedNzbs.add(guid);
}
} catch (InvalidSearchResultIdException | DownloaderException | EntityNotFoundException e) {
String message;
if (e instanceof DownloaderException) {
message = "Error while adding NZB(s) to downloader: " + e.getMessage();
} else if (e instanceof EntityNotFoundException) {
message = "Unable to find the search result in the database. Unable to download";
} else {
message = e.getMessage();
}
logger.error(message);
if (!addedNzbs.isEmpty()) {
message += ".\n" + addedNzbs.size() + " were added successfully";
}
Set<Long> searchResultIds = Sets.newHashSet(searchResults.stream().map(x -> Long.valueOf(x.getSearchResultId())).collect(Collectors.toSet()));
searchResultIds.removeAll(addedNzbs);
return new AddNzbsResponse(false, message, addedNzbs, searchResultIds);
}
return new AddNzbsResponse(true, null, addedNzbs, Collections.emptyList());
}
use of org.nzbhydra.searching.SearchResultEntity in project nzbhydra2 by theotherp.
the class TorrentFileHandler method getTorrentByGuid.
public DownloadResult getTorrentByGuid(long guid, FileDownloadAccessType accessType, SearchRequest.SearchSource accessSource) throws InvalidSearchResultIdException {
// Get result. if link contains magnet: return redirect to magnet URI. otherwise return file
SearchResultEntity result = searchResultRepository.findOne(guid);
if (result == null) {
logger.error("Download request with invalid/outdated GUID {}", guid);
throw new InvalidSearchResultIdException(guid, accessSource == SearchRequest.SearchSource.INTERNAL);
}
logger.info("Download request for \"{}\" from indexer {}", result.getTitle(), result.getIndexer().getName());
if (result.getLink().contains("magnet:") || accessType == FileDownloadAccessType.REDIRECT) {
return fileHandler.handleRedirect(accessSource, result);
} else {
return fileHandler.handleContentDownload(accessSource, result, "torrent");
}
}
use of org.nzbhydra.searching.SearchResultEntity in project nzbhydra2 by theotherp.
the class DownloadResultTest method shouldBuildFilenameCorrectly.
@Test
public void shouldBuildFilenameCorrectly() {
SearchResultEntity searchResultEntity = new SearchResultEntity();
searchResultEntity.setDownloadType(SearchResultItem.DownloadType.NZB);
FileDownloadEntity nzbDownloadEntity = new FileDownloadEntity();
nzbDownloadEntity.setSearchResult(searchResultEntity);
DownloadResult testee = DownloadResult.createSuccessfulDownloadResult("title", "content".getBytes(), nzbDownloadEntity);
assertThat(testee.getAsResponseEntity().getHeaders().get(HttpHeaders.CONTENT_DISPOSITION)).containsExactly("attachment; filename=title.nzb");
searchResultEntity.setDownloadType(SearchResultItem.DownloadType.TORRENT);
testee = DownloadResult.createSuccessfulDownloadResult("title", "content".getBytes(), nzbDownloadEntity);
assertThat(testee.getAsResponseEntity().getHeaders().get(HttpHeaders.CONTENT_DISPOSITION)).containsExactly("attachment; filename=title.torrent");
}
use of org.nzbhydra.searching.SearchResultEntity in project nzbhydra2 by theotherp.
the class SearchResultIdCalculatorTest method shouldCalculateSameSearchResultId.
@Test
public void shouldCalculateSameSearchResultId() throws Exception {
SearchResultEntity searchResultEntity = new SearchResultEntity();
IndexerEntity indexerEntity = new IndexerEntity();
indexerEntity.setName("indexerName");
indexerEntity = indexerRepository.save(indexerEntity);
searchResultEntity.setIndexer(indexerEntity);
searchResultEntity.setIndexerGuid("indexerGuid");
searchResultEntity.setTitle("title");
searchResultEntity = searchResultRepository.save(searchResultEntity);
assertEquals(-2991137394797183212L, SearchResultIdCalculator.calculateSearchResultId(searchResultEntity));
assertEquals(-2991137394797183212L, searchResultEntity.getId());
assertEquals(searchResultEntity, searchResultRepository.findOne(-2991137394797183212L));
}
use of org.nzbhydra.searching.SearchResultEntity in project nzbhydra2 by theotherp.
the class NzbDownloadingTests method setup.
@Before
public void setup() throws Exception {
System.setProperty("nzbhydra.dev.noApiKey", "true");
System.setProperty("server.host", "127.0.0.1");
mockServer = startClientAndServer(7070);
proxy = startClientAndProxy(7072);
mvc = MockMvcBuilders.webAppContextSetup(context).apply(SecurityMockMvcConfigurers.springSecurity()).build();
IndexerEntity indexer = new IndexerEntity();
indexer.setName("indexer");
indexerRepository.save(indexer);
searchResultRepository.deleteAll();
SearchResultEntity searchResult = new SearchResultEntity();
searchResult.setIndexer(indexer);
searchResult.setIndexerGuid("someNzbd");
searchResult.setLink("http://127.0.0.1:7070/getnzb?id=123");
searchResult.setTitle("someNzb");
searchResult.setPubDate(Instant.now());
searchResult.setFirstFound(Instant.now());
searchResult = searchResultRepository.save(searchResult);
searchResultId = searchResult.getId();
IndexerConfig indexerConfig = new IndexerConfig();
indexerConfig.setName("indexer");
indexerConfig.setHost("http://127.0.0.1:7070");
baseConfig.getIndexers().add(indexerConfig);
DownloaderConfig downloaderConfig = new DownloaderConfig();
downloaderConfig.setDownloaderType(DownloaderType.SABNZBD);
downloaderConfig.setName("sabnzbd");
downloaderConfig.setUrl("http://127.0.0.1:7070/sabnzbd/");
downloaderConfig.setNzbAddingType(NzbAddingType.SEND_LINK);
downloaderConfig.setApiKey("apikey");
baseConfig.getDownloading().getDownloaders().clear();
baseConfig.getDownloading().getDownloaders().add(downloaderConfig);
baseConfig.getSearching().setNzbAccessType(FileDownloadAccessType.REDIRECT);
downloaderProvider.handleNewConfig(new ConfigChangedEvent(this, new BaseConfig(), baseConfig));
}
Aggregations