MediaManage.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include "stdafx.h"
  2. #include "mediaManage.h"
  3. #include <set>
  4. #include <algorithm>
  5. #include "SpBase.h"
  6. #define DEFAULT_DOWNLOAD_PATH "D:/rvc/Downloads/package"
  7. #define DEFAULT_RESOURSE_PATH "D:/rvc/addata/Video"
  8. #define DEFAULT_DOWNLOAD_PATH_C "C:/rvc/Downloads/package"
  9. #define DEFAULT_RESOURSE_PATH_C "C:/rvc/addata/Video"
  10. #define DEFAULT_HEAD_CONTAIN "银行"
  11. #define DEFAULT_FEN_CONTAIN "分行"
  12. #define DEFAULT_NETWORK_CONTAIN "支行"
  13. #define DEFAULT_NETWORK2_CONTAIN "网点"
  14. mediaManage::mediaManage()
  15. {
  16. m_curParse.clear();
  17. if (checkDirExist(DEFAULT_RESOURSE_PATH))
  18. {
  19. Dbg("Set defaultAdvertPath:%s, defaultDownloadPath:%s", DEFAULT_RESOURSE_PATH, DEFAULT_DOWNLOAD_PATH);
  20. setDefaultAddvertPath(string(DEFAULT_RESOURSE_PATH));
  21. setDefaultDownloadPath(string(DEFAULT_DOWNLOAD_PATH));
  22. }
  23. else
  24. {
  25. Dbg("Set defaultAdvertPath:%s, defaultDownloadPath:%s", DEFAULT_RESOURSE_PATH_C, DEFAULT_DOWNLOAD_PATH_C);
  26. setDefaultAddvertPath(string(DEFAULT_RESOURSE_PATH_C));
  27. setDefaultDownloadPath(string(DEFAULT_DOWNLOAD_PATH_C));
  28. }
  29. }
  30. void mediaManage::setDefaultAddvertPath(string addvertPath){ m_addvertPath = addvertPath; }
  31. void mediaManage::setDefaultDownloadPath(string downloadPath){ m_downloadPath = downloadPath; }
  32. void mediaManage::GetPlayList(vector<ResourceParse> &resourceList)
  33. {
  34. resourceList.clear();
  35. for (vector<ParseInfo>::iterator i = m_curParse.begin(); i != m_curParse.end(); i++)
  36. {
  37. for (vector<ResourceParse>::iterator j = i->m_mediaList.begin(); j != i->m_mediaList.end(); j++)
  38. {
  39. if (checkInVaildTime(j->vaildTime, true) && checkInPlayTime(j->playTime, true))
  40. resourceList.push_back(*j);
  41. }
  42. }
  43. }
  44. void mediaManage::GetPlayListByLocal(vector<ResourceParse>& resourceList)
  45. {
  46. resourceList.clear();
  47. #ifdef RVC_OS_WIN
  48. for each (auto i in m_localList)
  49. {
  50. if (checkInVaildTime(i.vaildTime, true) && checkInPlayTime(i.playTime, true))
  51. resourceList.push_back(i);
  52. }
  53. #else
  54. for (vector<ResourceParse>::iterator it = m_localList.begin(); it < m_localList.end(); it++)
  55. {
  56. resourceList.push_back(*it);
  57. }
  58. #endif // _WIN32
  59. }
  60. bool mediaManage::InitResourseList(vector<string> resourceList)
  61. {
  62. /*
  63. (1)原resourseList不存在,初始化资源
  64. (2)原resourseList存在,删除失效资源,添加新资源
  65. 求差集
  66. */
  67. vector<string> needDelete(10), needAdd(10);
  68. set<string> oldResourceList, newResourseList;
  69. //get the old resource List
  70. for (vector<ParseInfo>::iterator j = m_curParse.begin(); j != m_curParse.end(); j++)
  71. oldResourceList.insert(j->m_resourceName);
  72. for (vector<string>::iterator i = resourceList.begin(); i != resourceList.end(); i++)
  73. newResourseList.insert(*i);
  74. auto deletePos = set_difference(oldResourceList.begin(), oldResourceList.end(), newResourseList.begin(), newResourseList.end(), needDelete.begin());//need to delete
  75. needDelete.resize(deletePos - needDelete.begin());//重新确定needDelete大小
  76. auto addPos = set_difference(newResourseList.begin(), newResourseList.end(), oldResourceList.begin(), oldResourceList.end(), needAdd.begin());//need to add
  77. needAdd.resize(addPos - needAdd.begin());//重新确定needAdd大小
  78. DeleteResourceList(needDelete);
  79. AddResourceList(needAdd);
  80. return true;
  81. }
  82. bool mediaManage::AddResourceList(vector<string> resourceList)
  83. {
  84. if (0 == resourceList.size())
  85. return true;
  86. for (vector<string>::iterator i = resourceList.begin(); i != resourceList.end(); i++)
  87. {
  88. int pos = i->find(".zip");
  89. if (-1 == pos)
  90. continue;
  91. string dirName = i->substr(0, pos);
  92. string fileName = m_downloadPath + SPLIT_SLASH_STR + *i;
  93. string dirPath = m_addvertPath + SPLIT_SLASH_STR + dirName;
  94. Dbg("dirName:%s, fileName:%s, dirPath:%s", dirName.c_str(), fileName.c_str(), dirPath.c_str());
  95. wstring wfileName, wdirPath;
  96. StringToWstring(wfileName, fileName);
  97. StringToWstring(wdirPath, dirPath);
  98. //(1)已解压;(2)压缩文件不存在;(3)解压失败
  99. if (!checkFileExist(dirPath))
  100. {
  101. Dbg("dir %s not exist!", dirPath.c_str());
  102. continue;//压缩文件夹不存在
  103. }
  104. /*
  105. if (!checkFileExist(dirPath) && createDir(dirPath) && !Unzip2Folder((BSTR)wfileName.c_str(), (BSTR)wdirPath.c_str()))
  106. {
  107. Dbg("unzip %s to %s fail!", fileName.c_str(), dirPath.c_str());
  108. continue;
  109. }
  110. */
  111. string configPath = "";
  112. if (!findVaildConfig(dirPath, configPath))
  113. {
  114. Dbg("can not find config file in %s//%s", dirPath.c_str(), configPath.c_str());
  115. continue;//配置文件不存在
  116. }
  117. vector<ResourceParse> mediaList;
  118. if (!parseResourceIni(configPath.c_str(), mediaList))
  119. {
  120. Dbg("parse config %s fail!", configPath.c_str());
  121. continue;//配置文件读取失败
  122. }
  123. string resourcePath = dirPath + SPLIT_SLASH_STR;
  124. /* if (!getUniqueDir(dirPath, resourcePath))
  125. {
  126. Dbg("error:find multi resource dir!");
  127. continue;//未找到唯一得广告目录
  128. }
  129. resourcePath = dirPath + "\\" + resourcePath + "\\";
  130. */
  131. for (vector<ResourceParse>::iterator j = mediaList.begin(); j != mediaList.end(); j++)
  132. j->resourcePath = resourcePath;
  133. ParseInfo resourseInfo(*i, mediaList);
  134. m_curParse.push_back(resourseInfo);//modify resourse list
  135. }
  136. return true;
  137. }
  138. void mediaManage::InitResourceListByLocal()
  139. {
  140. string headFile = m_addvertPath + SPLIT_SLASH_STR + HEADINI_NAME;
  141. string branchFile = m_addvertPath + SPLIT_SLASH_STR + BRANCHINI_NAME;
  142. string networkFile = m_addvertPath + SPLIT_SLASH_STR + NETWORKINI_NAME;
  143. vector<ResourceParse> headList, branchList, networkList, allList;
  144. if (!checkFileExist(headFile))
  145. Dbg("head config %s not exist!", headFile.c_str());
  146. else if (!parseResourceIni(headFile.c_str(), headList))
  147. Dbg("parse head config %s fail!", headFile.c_str());
  148. if (!checkFileExist(branchFile))
  149. Dbg("network config %s not exist!", networkFile.c_str());
  150. else if (!parseResourceIni(branchFile.c_str(), branchList))
  151. Dbg("parse branch config %s fail!", branchFile.c_str());
  152. if (!checkFileExist(networkFile))
  153. Dbg("network config %s not exist!", networkFile.c_str());
  154. else if (!parseResourceIni(networkFile.c_str(), networkList))
  155. Dbg("parse network config %s fail!", networkFile.c_str());
  156. m_localList.clear();
  157. #ifdef RVC_OS_WIN
  158. for each (auto i in headList)
  159. m_localList.push_back(i);
  160. for each (auto i in branchList)
  161. m_localList.push_back(i);
  162. for each (auto i in networkList)
  163. m_localList.push_back(i);
  164. #else
  165. for (vector<ResourceParse>::iterator it = headList.begin(); it < headList.end(); ++it)
  166. {
  167. m_localList.push_back(*it);
  168. }
  169. for (vector<ResourceParse>::iterator it = branchList.begin(); it < branchList.end(); ++it)
  170. {
  171. m_localList.push_back(*it);
  172. }
  173. for (vector<ResourceParse>::iterator it = networkList.begin(); it < networkList.end(); ++it)
  174. {
  175. m_localList.push_back(*it);
  176. }
  177. #endif // RVC_OS_WIN
  178. std::stable_sort(m_localList.begin(), m_localList.end(), [](const ResourceParse& a, const ResourceParse& b) {
  179. return a.priority > b.priority;
  180. });//按priority排序
  181. for (auto i = m_localList.begin(); i != m_localList.end(); i++)
  182. {
  183. auto pos = i->videoNames.find(SPLIT_SLASH_STR);
  184. if (pos > 0)
  185. {
  186. i->resourcePath = m_addvertPath + SPLIT_SLASH_STR + i->videoNames.substr(0, pos + 1);
  187. i->videoNames = i->videoNames.substr(pos + 1, i->videoNames.length() - pos - 1);
  188. }
  189. else
  190. i->resourcePath = m_addvertPath + SPLIT_SLASH_STR;
  191. }
  192. }
  193. void mediaManage::DeleteResourceList(vector<string> resourceList)
  194. {//不删除文件夹中文件,防止占用问题,定期清理
  195. if (0 == resourceList.size())
  196. return;
  197. for (vector<string>::iterator i = resourceList.begin(); i != resourceList.end(); i++)
  198. {
  199. for (vector<ParseInfo>::iterator j = m_curParse.begin(); j != m_curParse.end(); j++)
  200. {
  201. if (*i == j->m_resourceName)
  202. {
  203. m_curParse.erase(j);//迭代器失效
  204. break;
  205. }
  206. }
  207. }
  208. }
  209. bool mediaManage::findVaildConfig(string dirPath, string &configPath)
  210. {
  211. string headFile = dirPath + SPLIT_SLASH_STR + HEADINI_NAME;
  212. string branchFile = dirPath + SPLIT_SLASH_STR + BRANCHINI_NAME;
  213. string networkFile = dirPath + SPLIT_SLASH_STR + NETWORKINI_NAME;
  214. if (checkFileExist(headFile))
  215. {
  216. configPath = headFile;
  217. return true;
  218. }
  219. else if (checkFileExist(branchFile))
  220. {
  221. configPath = branchFile;
  222. return true;
  223. }
  224. else if (checkFileExist(networkFile))
  225. {
  226. configPath = networkFile;
  227. return true;
  228. }
  229. else
  230. return false;
  231. }
  232. void mediaManage::clearOutdataResource()
  233. {
  234. vector<string> dirInfo, needDelete(10);
  235. set<string> oldResourceList, newResourseList;
  236. try
  237. {
  238. getDirs(m_addvertPath, dirInfo);
  239. for (vector<ParseInfo>::iterator i = m_curParse.begin(); i != m_curParse.end(); i++)
  240. {
  241. int pos = i->m_resourceName.find(".zip");
  242. if (-1 == pos)
  243. continue;
  244. newResourseList.insert(i->m_resourceName.substr(0, pos));
  245. }
  246. for (vector<string>::iterator i = dirInfo.begin(); i != dirInfo.end(); i++)
  247. {
  248. if(-1 != i->find(DEFAULT_HEAD_CONTAIN) || -1 != i->find(DEFAULT_FEN_CONTAIN) || -1 != i->find(DEFAULT_NETWORK_CONTAIN) || -1 != i->find(DEFAULT_NETWORK2_CONTAIN))
  249. oldResourceList.insert(*i);
  250. }
  251. auto deletePos = set_difference(oldResourceList.begin(), oldResourceList.end(), newResourseList.begin(), newResourseList.end(), needDelete.begin());//need to delete
  252. needDelete.resize(deletePos - needDelete.begin());//重新确定needDelete大小
  253. for (vector<string>::iterator i = needDelete.begin(); i != needDelete.end(); i++)
  254. {
  255. string path = m_addvertPath + SPLIT_SLASH_STR + *i;
  256. Dbg("remove Dir %s %s!", *i, removeDir(path) ? "success" : "fail");
  257. }
  258. }
  259. catch (exception* e)
  260. {
  261. Dbg("remove Dir exception:%s", e->what());
  262. }
  263. }