JSAPI2_DownloaderAPI.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "JSAPI2_Downloader.h"
  2. #include "JSAPI2_Security.h"
  3. #include "main.h"
  4. #include "../Agave/Language/api_language.h"
  5. #include "JSAPI.h"
  6. #include "../nu/AutoChar.h"
  7. #include "api.h"
  8. #include "resource.h"
  9. #include "../Plugins/General/gen_ml/ml.h"
  10. #include <api/service/svcs/svc_imgload.h>
  11. JSAPI2::DownloaderAPI::DownloaderAPI(const wchar_t *_key, JSAPI::ifc_info *_info)
  12. {
  13. info = _info;
  14. key = _key;
  15. refCount = 1;
  16. }
  17. enum
  18. {
  19. DISP_DOWNLOADERAPI_DOWNLOADMEDIA,
  20. };
  21. #define DISP_TABLE \
  22. CHECK_ID(DownloadMedia, DISP_DOWNLOADERAPI_DOWNLOADMEDIA)\
  23. #define CHECK_ID(str, id)\
  24. if (CSTR_EQUAL == CompareStringW(lcid, NORM_IGNORECASE, rgszNames[i], -1, L## #str, -1))\
  25. { rgdispid[i] = id; continue; }
  26. HRESULT JSAPI2::DownloaderAPI::GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid)
  27. {
  28. bool unknowns = false;
  29. for (unsigned int i = 0;i != cNames;i++)
  30. {
  31. DISP_TABLE;
  32. rgdispid[i] = DISPID_UNKNOWN;
  33. unknowns = true;
  34. }
  35. if (unknowns)
  36. return DISP_E_UNKNOWNNAME;
  37. else
  38. return S_OK;
  39. }
  40. HRESULT JSAPI2::DownloaderAPI::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
  41. {
  42. return E_NOTIMPL;
  43. }
  44. HRESULT JSAPI2::DownloaderAPI::GetTypeInfoCount(unsigned int FAR * pctinfo)
  45. {
  46. return E_NOTIMPL;
  47. }
  48. bool IsImage(const wchar_t *filename);
  49. bool IsPlaylist(const wchar_t *filename);
  50. bool IsMedia(const wchar_t *filename);
  51. bool GetOnlineDownloadPath(const wchar_t *key, const wchar_t *svcname, wchar_t path_to_store[MAX_PATH]);
  52. HRESULT JSAPI2::DownloaderAPI::DownloadMedia(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  53. {
  54. JSAPI_VERIFY_METHOD(wFlags);
  55. JSAPI_VERIFY_PARAMCOUNT_OPTIONAL(pdispparams, 1, 3);
  56. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_BSTR, puArgErr);
  57. JSAPI_VERIFY_PARAMTYPE_OPTIONAL(pdispparams, 2, VT_BSTR, puArgErr);
  58. JSAPI_VERIFY_PARAMTYPE_OPTIONAL(pdispparams, 3, VT_BOOL, puArgErr);
  59. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  60. if (security.GetActionAuthorization(L"downloader", L"downloadmedia", key, info, JSAPI2::api_security::ACTION_PROMPT) == JSAPI2::api_security::ACTION_ALLOWED)
  61. {
  62. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  63. const wchar_t *url = JSAPI_PARAM(pdispparams, 1).bstrVal;
  64. wchar_t *destFileSpec=JSAPI_PARAM_OPTIONAL(pdispparams, 2, bstrVal, PathFindFileNameW(url));
  65. //filter reserved characters in file name
  66. CleanNameForPath(destFileSpec);
  67. // verify that passed-in URL is a valid media type
  68. if (!IsImage(destFileSpec) && !IsPlaylist(destFileSpec) && !IsMedia(destFileSpec))
  69. {
  70. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_FALSE);
  71. return S_OK;
  72. }
  73. wchar_t path_to_store[MAX_PATH] = {0};
  74. if (GetOnlineDownloadPath(this->key, this->info->GetName(), path_to_store))
  75. {
  76. wchar_t dlgtitle[256] = {0};
  77. CreateDirectoryW(path_to_store, NULL);
  78. wchar_t destfile[MAX_PATH] = {0};
  79. PathCombineW(destfile, path_to_store, destFileSpec);
  80. httpRetrieveFileW(hMainWindow, AutoChar(url), destfile, getStringW(IDS_DOWNLOADING,dlgtitle,256));
  81. // TODO: optional adding to media library or not (param 3)
  82. LMDB_FILE_ADD_INFOW fi = {const_cast<wchar_t *>(destfile), -1, -1};
  83. sendMlIpc(ML_IPC_DB_ADDORUPDATEFILEW, (WPARAM)&fi);
  84. sendMlIpc(ML_IPC_DB_SYNCDB, 0);
  85. }
  86. else
  87. {
  88. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_FALSE);
  89. }
  90. }
  91. else
  92. {
  93. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_FALSE);
  94. }
  95. return S_OK;
  96. }
  97. #undef CHECK_ID
  98. #define CHECK_ID(str, id) case id: return str(wFlags, pdispparams, pvarResult, puArgErr);
  99. HRESULT JSAPI2::DownloaderAPI::Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr)
  100. {
  101. switch (dispid)
  102. {
  103. DISP_TABLE;
  104. }
  105. return DISP_E_MEMBERNOTFOUND;
  106. }
  107. STDMETHODIMP JSAPI2::DownloaderAPI::QueryInterface(REFIID riid, PVOID *ppvObject)
  108. {
  109. if (!ppvObject)
  110. return E_POINTER;
  111. else if (IsEqualIID(riid, IID_IDispatch))
  112. *ppvObject = (IDispatch *)this;
  113. else if (IsEqualIID(riid, IID_IUnknown))
  114. *ppvObject = this;
  115. else
  116. {
  117. *ppvObject = NULL;
  118. return E_NOINTERFACE;
  119. }
  120. AddRef();
  121. return S_OK;
  122. }
  123. ULONG JSAPI2::DownloaderAPI::AddRef(void)
  124. {
  125. return InterlockedIncrement(&refCount);
  126. }
  127. ULONG JSAPI2::DownloaderAPI::Release(void)
  128. {
  129. LONG lRef = InterlockedDecrement(&refCount);
  130. if (lRef == 0) delete this;
  131. return lRef;
  132. }