MediaDownloader.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "precomp__gen_ff.h"
  2. #include "main.h"
  3. #include "../Agave/Language/api_language.h"
  4. #include "../../..\Components\wac_network\wac_network_http_receiver_api.h"
  5. #include "../nu/AutoWide.h"
  6. #include "../nu/AutoChar.h"
  7. #include "../nu/AutoCharFn.h"
  8. #include "wa2frontend.h"
  9. #include "resource.h"
  10. #include "../nu/ns_wc.h"
  11. #include "../nu/refcount.h"
  12. #include "api/skin/widgets/xuidownloadslist.h"
  13. #include <shlobj.h>
  14. #include <shlwapi.h>
  15. #include <api/script/objects/systemobj.h>
  16. extern wchar_t *INI_DIR;
  17. static void createDirForFileW(wchar_t *str)
  18. {
  19. wchar_t *p = str;
  20. if ((p[0] ==L'\\' || p[0] ==L'/') && (p[1] ==L'\\' || p[1] ==L'/'))
  21. {
  22. p += 2;
  23. while (p && *p && *p !=L'\\' && *p !=L'/') p++;
  24. if (!p || !*p) return ;
  25. p++;
  26. while (p && *p && *p !=L'\\' && *p !=L'/') p++;
  27. }
  28. else
  29. {
  30. while (p && *p && *p !=L'\\' && *p !=L'/') p++;
  31. }
  32. while (p && *p)
  33. {
  34. while (p && *p !=L'\\' && *p !=L'/' && *p) p = CharNextW(p);
  35. if (p && *p)
  36. {
  37. wchar_t lp = *p;
  38. *p = 0;
  39. CreateDirectoryW(str, NULL);
  40. *p++ = lp;
  41. }
  42. }
  43. }
  44. static wchar_t* defaultPathToStore()
  45. {
  46. static wchar_t pathToStore[MAX_PATH] = {0};
  47. if(FAILED(SHGetFolderPathW(NULL, CSIDL_MYMUSIC, NULL, SHGFP_TYPE_CURRENT, pathToStore)))
  48. {
  49. if(FAILED(SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, pathToStore)))
  50. {
  51. lstrcpynW(pathToStore, L"C:\\My Music", MAX_PATH);
  52. }
  53. }
  54. return pathToStore;
  55. }
  56. static void GetPathToStore(wchar_t path_to_store[MAX_PATH])
  57. {
  58. GetPrivateProfileStringW( L"gen_ml_config", L"extractpath", defaultPathToStore(), path_to_store, MAX_PATH, (const wchar_t *)SendMessageW( plugin.hwndParent, WM_WA_IPC, 0, IPC_GETMLINIFILEW ) );
  59. }
  60. void Winamp2FrontEnd::getDownloadPath(wchar_t path2store[MAX_PATH])
  61. {
  62. GetPathToStore(path2store);
  63. }
  64. void Winamp2FrontEnd::setDownloadPath(const wchar_t * path2store)
  65. {
  66. WritePrivateProfileStringA( "gen_ml_config", "extractpath", AutoChar( path2store, CP_UTF8 ), (const char *)SendMessageW( plugin.hwndParent, WM_WA_IPC, 0, IPC_GETMLINIFILE ) );
  67. }
  68. class DownloadCallback : public Countable<ifc_downloadManagerCallback>
  69. {
  70. public:
  71. DownloadCallback( const wchar_t *destination_filepath, bool storeInMl = true, bool notifyDownloadsList = true )
  72. {
  73. WCSCPYN( this->destination_filepath, destination_filepath, MAX_PATH );
  74. this->storeInMl = storeInMl;
  75. this->notifyDownloadsList = notifyDownloadsList;
  76. }
  77. void OnFinish( DownloadToken token );
  78. void OnError( DownloadToken token, int error )
  79. {
  80. if ( notifyDownloadsList )
  81. DownloadsList::onDownloadError( token, error );
  82. Release();
  83. }
  84. void OnCancel( DownloadToken token )
  85. {
  86. if ( notifyDownloadsList )
  87. DownloadsList::onDownloadCancel( token );
  88. Release();
  89. }
  90. void OnTick( DownloadToken token )
  91. {
  92. if ( notifyDownloadsList )
  93. DownloadsList::onDownloadTick( token );
  94. }
  95. const wchar_t *getPreferredFilePath()
  96. {
  97. return destination_filepath;
  98. }
  99. bool getStoreInML()
  100. {
  101. return storeInMl;
  102. }
  103. REFERENCE_COUNT_IMPLEMENTATION;
  104. protected:
  105. RECVS_DISPATCH;
  106. private:
  107. bool storeInMl;
  108. bool notifyDownloadsList;
  109. wchar_t destination_filepath[ MAX_PATH ];
  110. };
  111. // TODO: benski> this is a big hack. we should have a MIME manager in Winamp
  112. struct
  113. {
  114. const char *mime; const char *ext;
  115. }
  116. hack_mimes[] = { {"audio/mpeg", ".mp3"} };
  117. void DownloadCallback::OnFinish(DownloadToken token)
  118. {
  119. api_httpreceiver *http = WAC_API_DOWNLOADMANAGER->GetReceiver(token);
  120. if (http)
  121. {
  122. const char *extension = 0;
  123. const char *headers = http->getallheaders();
  124. // we're trying to figure out wtf kind of file this is
  125. if (!extension) // just adding this if to make this easier to re-arrange
  126. {
  127. // 1) check if there's a content-disposition, it might have an extension
  128. const char *content_disposition = http->getheader("content-disposition");
  129. if (content_disposition)
  130. {
  131. const char *content_filename = strstr(content_disposition, "filename=");
  132. if (content_filename && *content_filename)
  133. {
  134. content_filename+=9;
  135. extension = PathFindExtensionA(content_filename);
  136. }
  137. }
  138. }
  139. if (!extension)
  140. {
  141. // 2) check MIME type for something good
  142. const char *content_type = http->getheader("content-type");
  143. if (content_type)
  144. {
  145. for (int i=0;i!=sizeof(hack_mimes)/sizeof(hack_mimes[0]);i++)
  146. {
  147. if (!strcmp(hack_mimes[i].mime, content_type))
  148. {
  149. extension=hack_mimes[i].ext;
  150. }
  151. }
  152. }
  153. }
  154. const char *url = http->get_url();
  155. if (!extension)
  156. {
  157. // 3) check if URL has an extension
  158. if (url) // umm, we better have a URL :) but worth a check
  159. {
  160. extension = PathFindExtensionA(url);
  161. }
  162. }
  163. if (!extension)
  164. {
  165. // 4) ask for winamp's default extension and use that (most likely mp3)
  166. extension=".mp3"; // TODO: actually use the setting and not hardcode this :)
  167. }
  168. // then we rename the file
  169. wchar_t temppath[MAX_PATH-14] = {0};
  170. wchar_t filename[MAX_PATH] = {0};
  171. GetTempPathW(MAX_PATH-14, temppath);
  172. GetTempFileNameW(temppath, L"atf", 0, filename);
  173. PathRemoveExtensionW(filename);
  174. PathAddExtensionW(filename, AutoWide(extension));
  175. const wchar_t *downloadDest = WAC_API_DOWNLOADMANAGER->GetLocation(token);
  176. MoveFileW(downloadDest, filename);
  177. // then we build a filename with ATF
  178. wchar_t formatted_filename[MAX_PATH] = {0}, path_to_store[MAX_PATH] = {0};
  179. if (!WCSICMP(this->getPreferredFilePath(), L""))
  180. GetPathToStore(path_to_store);
  181. else
  182. WCSCPYN(path_to_store, this->getPreferredFilePath(), MAX_PATH);
  183. // TODO: benski> this is very temporary
  184. char temp_filename[MAX_PATH] = {0}, *t = temp_filename,
  185. *tfn = PathFindFileNameA(url);
  186. while(tfn && *tfn)
  187. {
  188. if(*tfn == '%')
  189. {
  190. if(_strnicmp(tfn,"%20",3))
  191. {
  192. *t = *tfn;
  193. }
  194. else{
  195. *t = ' ';
  196. tfn = CharNextA(tfn);
  197. tfn = CharNextA(tfn);
  198. }
  199. }
  200. else
  201. {
  202. *t = *tfn;
  203. }
  204. tfn = CharNextA(tfn);
  205. t = CharNextA(t);
  206. }
  207. *t = 0;
  208. PathCombineW(formatted_filename, path_to_store, AutoWide(temp_filename));
  209. createDirForFileW(formatted_filename);
  210. // then move the file there
  211. if (!MoveFileW(filename, formatted_filename))
  212. {
  213. CopyFileW(filename, formatted_filename, FALSE);
  214. DeleteFileW(filename);
  215. }
  216. //Std::messageBox(formatted_filename, filename, 0);
  217. if (this->getStoreInML() && PathFileExistsW(formatted_filename))
  218. {
  219. // then add to the media library :)
  220. // TOOD: benski> use api_mldb because it's more thread-friendly than SendMessageW
  221. HWND library = wa2.getMediaLibrary();
  222. LMDB_FILE_ADD_INFOW fi = {const_cast<wchar_t *>(formatted_filename), -1, -1};
  223. SendMessageW(library, WM_ML_IPC, (WPARAM)&fi, ML_IPC_DB_ADDORUPDATEFILEW);
  224. PostMessage(library, WM_ML_IPC, 0, ML_IPC_DB_SYNCDB);
  225. }
  226. if (notifyDownloadsList)
  227. DownloadsList::onDownloadEnd(token, const_cast<wchar_t *>(formatted_filename));
  228. SystemObject::onDownloadFinished(AutoWide(url), true, formatted_filename);
  229. Release();
  230. return;
  231. }
  232. if (notifyDownloadsList)
  233. DownloadsList::onDownloadEnd(token, NULL);
  234. Release();
  235. }
  236. int Winamp2FrontEnd::DownloadFile( const char *url, const wchar_t *destfilepath, bool addToMl, bool notifyDownloadsList )
  237. {
  238. DownloadCallback *callback = new DownloadCallback( destfilepath, addToMl, notifyDownloadsList );
  239. DownloadToken dt = WAC_API_DOWNLOADMANAGER->Download( url, callback );
  240. // Notify <DownloadsList/>
  241. if ( notifyDownloadsList )
  242. DownloadsList::onDownloadStart( url, dt );
  243. return 0;
  244. /*
  245. HTTPRETRIEVEFILEW func = (HTTPRETRIEVEFILEW) SendMessageW(hwnd_winamp, WM_WA_IPC, 0, IPC_GETHTTPGETTERW);
  246. if (func || func == (HTTPRETRIEVEFILEW)1)
  247. return func(NULL, url, destfilename, title);
  248. else
  249. return 0;
  250. */
  251. }
  252. #define CBCLASS DownloadCallback
  253. START_DISPATCH;
  254. REFERENCE_COUNTED;
  255. VCB( IFC_DOWNLOADMANAGERCALLBACK_ONFINISH, OnFinish )
  256. VCB( IFC_DOWNLOADMANAGERCALLBACK_ONTICK, OnTick )
  257. VCB( IFC_DOWNLOADMANAGERCALLBACK_ONERROR, OnError )
  258. VCB( IFC_DOWNLOADMANAGERCALLBACK_ONCANCEL, OnCancel )
  259. END_DISPATCH;
  260. #undef CBCLASS