1 /* This file is part of pr-downloader (GPL v2 or later), see the LICENSE file */
2 
3 #include "PlasmaDownloader.h"
4 #include "FileSystem/FileSystem.h"
5 #include "Util.h"
6 #include "Downloader/Download.h"
7 #include "Logger.h"
8 #include "lib/soap/soapContentServiceSoap12Proxy.h"
9 #include "lib/soap/ContentServiceSoap.nsmap"
10 
11 
CPlasmaDownloader()12 CPlasmaDownloader::CPlasmaDownloader():
13 	torrentPath(fileSystem->getSpringDir()+PATH_DELIMITER +  "torrent" + PATH_DELIMITER)
14 {
15 	fileSystem->createSubdirs(this->torrentPath);
16 }
17 
search(std::list<IDownload * > & result,const std::string & name,IDownload::category category)18 bool CPlasmaDownloader::search(std::list<IDownload*>& result, const std::string& name, IDownload::category category)
19 {
20 	LOG_DEBUG("%s",name.c_str());
21 	ContentServiceSoap12Proxy service;
22 	_Plasma__DownloadFile file;
23 	_Plasma__DownloadFileResponse fileResponse;
24 	std::string tmpname=name;
25 	file.internalName=&tmpname;
26 	int res;
27 	res=service.DownloadFile(&file, &fileResponse);
28 	if (res != SOAP_OK) {
29 		LOG_ERROR("Soap error: %d: %s",res, service.soap_fault_string());
30 		return false;
31 	}
32 	if (!fileResponse.DownloadFileResult) {
33 		LOG_DEBUG("No file found for criteria %s",name.c_str());
34 		return false;
35 	}
36 
37 	std::vector<std::string>::iterator it;
38 	IDownload::category cat=category;
39 	std::string fileName=fileSystem->getSpringDir() + PATH_DELIMITER;
40 	switch (fileResponse.resourceType) {
41 	case Plasma__ResourceType__Map:
42 		cat=IDownload::CAT_MAPS;
43 		fileName.append("maps");
44 		break;
45 	case Plasma__ResourceType__Mod:
46 		cat=IDownload::CAT_GAMES;
47 		fileName.append("games");
48 		break;
49 	default:
50 		LOG_DEBUG("Unknown category in result: %d", cat);
51 		cat=IDownload::CAT_NONE;
52 		break;
53 	}
54 	fileName+=PATH_DELIMITER;
55 	if (fileResponse.links->string.size()==0) {
56 		LOG_DEBUG("No mirror in plasma result.");
57 		return false;
58 	}
59 
60 	std::string torrent;
61 	torrent.assign((char*)fileResponse.torrent->__ptr,fileResponse.torrent->__size);
62 	IDownload* dl = new IDownload("",name);
63 	//parse torrent data and fill set values inside dl
64 	const bool bres = fileSystem->parseTorrent((char*)fileResponse.torrent->__ptr, fileResponse.torrent->__size, dl);
65 	if ( (dl->name == "") || (!bres)) {
66 		LOG_ERROR("Couldn't parse torrent filename");
67 		return false;
68 	}
69 
70 	//set full path name
71 	fileName.append(dl->name);
72 	dl->name=fileName;
73 	dl->cat=cat;
74 	LOG_DEBUG("Got filename \"%s\" from torrent",fileName.c_str());
75 
76 	for (it=fileResponse.links->string.begin(); it!=fileResponse.links->string.end(); ++it) {
77 		dl->addMirror((*it).c_str());
78 	}
79 	for (it=fileResponse.dependencies->string.begin(); it!=fileResponse.dependencies->string.end(); ++it) {
80 		dl->addDepend((*it).c_str());
81 	}
82 	result.push_back(dl);
83 	return true;
84 }
85 
download(IDownload * download,int max_parallel)86 bool CPlasmaDownloader::download(IDownload* download,int max_parallel)
87 {
88 	LOG_DEBUG("%s",download->name.c_str());
89 	return httpDownload->download(download);
90 }
91