use of org.nzbhydra.config.NzbAddingType 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());
}
Aggregations