tools.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. local httpd = require "http.httpd"
  2. local sockethelper = require "http.sockethelper"
  3. local skynet = require "skynet"
  4. local httpd = require "http.httpd"
  5. local cjson = require "cjson"
  6. local crypt = require "client.crypt"
  7. local runConfig = require "run_config"
  8. local M = {
  9. }
  10. M.read_request = function(id)
  11. -- limit request body size to 8192 (you can pass nil to unlimit)
  12. -- 一般的业务不需要处理大量上行数据,为了防止攻击,做了一个 8K 限制。这个限制可以去掉。
  13. -- local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id), 8192)
  14. local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id))
  15. return {code=code,url=url,method=method,header=header,body=body}
  16. end
  17. M.response = function(id, ...)
  18. local ok, err = httpd.write_response(sockethelper.writefunc(id), ...)
  19. if not ok then
  20. -- if err == sockethelper.socket_error , that means socket closed.
  21. skynet.error(string.format("fd = %d, %s", id, err))
  22. end
  23. end
  24. --检查工具返回的字段是否缺失
  25. M.checkToolsSyncVideo= function(table)
  26. local requiredFields = {"classification_id","stype","folder_id", "height", "width","duration", "user_id", "ratio", "category_name","file_name","size","surl","path"} -- Define the required fields here
  27. for _, field in pairs(requiredFields) do
  28. if not table[field] then
  29. return false,field
  30. end
  31. end
  32. return true
  33. end
  34. --检查工具返回的字段是否缺失
  35. M.checkData= function(origin,table)
  36. for _, field in pairs(origin) do
  37. if not table[field] then
  38. return false,field
  39. end
  40. end
  41. return true
  42. end
  43. M.dump = function(res, tab)
  44. if runConfig.is_debug == nil then
  45. return
  46. end
  47. tab = tab or 0
  48. if(tab == 0) then
  49. skynet.error("............dump...........")
  50. end
  51. if type(res) == "table" then
  52. skynet.error(string.rep("\t", tab).."{")
  53. for k,v in pairs(res) do
  54. if type(v) == "table" then
  55. skynet.error(k.."=")
  56. M.dump(v, tab + 1)
  57. else
  58. skynet.error(string.rep("\t", tab), k, "=", v, ",")
  59. end
  60. end
  61. skynet.error(string.rep("\t", tab).."}")
  62. else
  63. skynet.error(string.rep("\t", tab) , res)
  64. end
  65. end
  66. M.getDbResData = function(res)
  67. local tab = {}
  68. if #res >1 then
  69. local i = 1;
  70. for k, v in pairs(res) do
  71. -- skynet.error("Row " .. k)
  72. local tab_1 = {}
  73. for field, value in pairs(v) do
  74. tab_1[field] = value
  75. -- skynet.error(field .. ": " .. value)
  76. end
  77. tab[i] = tab_1;
  78. i=i+1;
  79. end
  80. else
  81. for k, v in pairs(res) do
  82. for field, value in pairs(v) do
  83. tab[field] = value
  84. end
  85. end
  86. end
  87. return tab
  88. end
  89. M.tokenEncode = function(user_data)
  90. return crypt.base64encode(user_data)
  91. end
  92. M.tokenDecode = function(base_str)
  93. return crypt.base64decode(base_str)
  94. end
  95. M.base64decode = function(base_str)
  96. return crypt.base64decode(base_str)
  97. end
  98. M.base64encode = function(base_str)
  99. return crypt.base64encode(base_str)
  100. end
  101. M.getPageData = function(page, count,list)
  102. local _start = (page-1)*count+1
  103. local _end =page*count
  104. -- skynet.error("_start",_start)
  105. -- skynet.error("_end",_end)
  106. -- skynet.error("list",#list)
  107. local _temp_list = {}
  108. if _start <1 or _end <1 then
  109. return _temp_list
  110. end
  111. if #list <=0 then
  112. return _temp_list
  113. end
  114. if #list < _start then
  115. return _temp_list
  116. end
  117. -- local total_page = math.floor(#list/count) +1
  118. -- if (#list%count)>0 then
  119. -- total_page = total_page+1
  120. -- end
  121. if _end > #list then
  122. local index = 1
  123. for i = _start, #list do
  124. _temp_list[index] = list[i]
  125. index= index+1
  126. end
  127. else
  128. local index = 1
  129. for i = _start, _end do
  130. _temp_list[index] = list[i]
  131. index= index+1
  132. end
  133. end
  134. return _temp_list,#list
  135. end
  136. --回应db新建文件夹
  137. M.response_db_new_folder = function(fd,is_ok,error_info)
  138. if not is_ok then
  139. if error_info~=nil then
  140. return M.response(fd,200,cjson.encode({code=10001,msg = error_info}))
  141. end
  142. return M.response(fd,200,cjson.encode({code=10001,msg = "新建文件夹失败!"}))
  143. end
  144. return M.response(fd,200,cjson.encode({code=10000,msg = "新建文件夹成功!"}))
  145. end
  146. --回应db获取资源文件夹列表
  147. M.response_db_folder_list = function(fd,msg_body,isok,tab)
  148. if not isok then
  149. return M.response(fd,200, '{"code": 10000, "msg": "获取资源文件夹列表失败!", "data": [] }')
  150. end
  151. local total_count = 0
  152. local list = tab
  153. list,total_count = M.getPageData(msg_body.page,msg_body.count,list)
  154. if #list <= 0 or list == nil then
  155. return M.response(fd,200,'{"code": 10000, "msg": "获取资源文件夹列表失败!", "data": [] }')
  156. -- return M.response(fd,200,cjson.encode({code=10000,msg = "获取资源文件夹列表成功!",data=folder_list,total_count=total_count}))
  157. end
  158. skynet.call("dbmgr","lua","on_recv","folder_res_list_nums",list,fd,total_count)
  159. end
  160. M.response_db_folder_list_nums = function(fd,list,count_list,total_count)
  161. local folder_list = {}
  162. for i = 1, #list, 1 do
  163. folder_list[i] = {file_nums=count_list[list[i].id],folder_name=list[i].folder_name,folder_id=list[i].id}
  164. end
  165. return M.response(fd,200,cjson.encode({code=10000,msg = "获取资源文件夹列表成功!",data=folder_list,total_count=total_count}))
  166. end
  167. --回应db获取指定文件夹内的资源列表
  168. M.response_db_folder_res_list = function(fd,msg_body,isok,tab)
  169. if not isok then
  170. return M.response(fd,200,'{"code": 10000, "msg": "获取文件夹资源失败", "data": [] }')
  171. end
  172. local list = tab
  173. local folder_list = {}
  174. local total_count = 0
  175. -- M.dump(list)
  176. -- skynet.error("list",list)
  177. -- skynet.error("msg_body",msg_body)
  178. -- M.dump(msg_body)
  179. list,total_count = M.getPageData(msg_body.page,msg_body.count,list)
  180. for i = 1, #list, 1 do
  181. -- skynet.error("create_time")
  182. -- M.dump(list[i])
  183. table.insert(folder_list,i,{create_time=list[i].create_time,file_id=list[i].id,info=list[i].file_info,file_name=list[i].file_name})
  184. end
  185. if #list<=0 then
  186. return M.response(fd,200,'{"code": 10000, "msg": "获取文件夹资源失败", "data": [] }')
  187. end
  188. return M.response(fd,200,cjson.encode({code=10000,msg = "获取文件夹资源成功!",data=folder_list,total_count=total_count}))
  189. end
  190. M.response_db_search_res = function(fd,msg_body,isok,tab)
  191. local error_json = '{"code": 10000, "msg": "搜索失败", "data": [] }'
  192. if not isok then
  193. return M.response(fd,200,error_json)
  194. end
  195. local list = tab
  196. local file_list = {}
  197. local total_count = 0
  198. -- M.dump(list)
  199. -- skynet.error("list",list)
  200. -- skynet.error("msg_body",msg_body)
  201. -- tools.dump(msg_body)
  202. list ,total_count = M.getPageData(msg_body.page,msg_body.count,list)
  203. for i = 1, #list, 1 do
  204. table.insert(file_list,i,{create_time=list[i].create_time,file_id=list[i].id,info=list[i].file_info,file_name=list[i].file_name})
  205. end
  206. if #list<=0 then
  207. return M.response(fd,200,error_json)
  208. end
  209. return M.response(fd,200,cjson.encode({code=10000,msg = "搜索成功",data=file_list,total_count=total_count}))
  210. end
  211. M.response_db_get_file_list_by_type = function(fd,msg_body,isok,tab)
  212. local error_json = '{"code": 10000, "msg": "获取指定类型所有文件失败!", "data": [] }'
  213. if not isok then
  214. return M.response(fd,200,error_json)
  215. end
  216. local list = tab
  217. local file_list = {}
  218. local total_count = 0
  219. list ,total_count = M.getPageData(msg_body.page,msg_body.count,list)
  220. for i = 1, #list, 1 do
  221. table.insert(file_list,i,{create_time=list[i].create_time,file_id=list[i].id,info=list[i].file_info,file_name=list[i].file_name})
  222. end
  223. if #list<=0 then
  224. return M.response(fd,200,error_json)
  225. end
  226. return M.response(fd,200,cjson.encode({code=10000,msg = "获取指定类型所有文件成功",data=file_list,total_count=total_count}))
  227. end
  228. M.response_db_get_template_list = function(fd,msg_body,isok,tab)
  229. local error_json = '{"code": 10000, "msg": "获取模板列表失败", "data": [] }'
  230. if not isok then
  231. return M.response(fd,200,error_json)
  232. end
  233. local list = tab
  234. local template_list = {}
  235. local total_count = 0
  236. list ,total_count = M.getPageData(msg_body.page,msg_body.count,list)
  237. for i = 1, #list, 1 do
  238. table.insert(template_list,i,{create_time=list[i].create_time,
  239. template_id=list[i].id,
  240. project_name=list[i].project_name,
  241. book_name=list[i].book_name,
  242. ratio=list[i].ratio,
  243. video_stype=list[i].video_stype,
  244. subtitles=list[i].subtitles,
  245. subtitles_audio=list[i].subtitles_audio,
  246. video_nums=list[i].video_nums,
  247. video_crop_time=list[i].video_crop_time,
  248. info=list[i].template_info})
  249. end
  250. if #list<=0 then
  251. return M.response(fd,200,error_json)
  252. end
  253. return M.response(fd,200,cjson.encode({code=10000,msg = "获取模板列表成功",data=template_list,total_count=total_count}))
  254. end
  255. M.response_db_search_template = function(fd,msg_body,isok,tab)
  256. local error_json = '{"code": 10000, "msg": "搜索模板失败", "data": [] }'
  257. if not isok then
  258. return M.response(fd,200,error_json)
  259. end
  260. local list = tab
  261. local template_list = {}
  262. local total_count = 0
  263. list ,total_count = M.getPageData(msg_body.page,msg_body.count,list)
  264. for i = 1, #list, 1 do
  265. table.insert(template_list,i,{create_time=list[i].create_time,
  266. template_id=list[i].id,
  267. project_name=list[i].project_name,
  268. book_name=list[i].book_name,
  269. ratio=list[i].ratio,
  270. video_stype=list[i].video_stype,
  271. subtitles=list[i].subtitles,
  272. subtitles_audio=list[i].subtitles_audio,
  273. video_nums=list[i].video_nums,
  274. video_crop_time=list[i].video_crop_time,
  275. info=list[i].template_info})
  276. end
  277. if #list<=0 then
  278. return M.response(fd,200,error_json)
  279. end
  280. return M.response(fd,200,cjson.encode({code=10000,msg = "搜索模板成功",data=template_list,total_count=total_count}))
  281. end
  282. M.response_db_get_template_info_by_id = function(fd,msg_body,isok,tab)
  283. local error_json = '{"code": 10000, "msg": "获取模板信息失败!", "data": [] }'
  284. if not isok then
  285. return M.response(fd,200,error_json)
  286. end
  287. local info = {create_time=tab.create_time,
  288. template_id=tab.id,
  289. project_name=tab.project_name,
  290. book_name=tab.book_name,
  291. ratio=tab.ratio,
  292. video_stype=tab.video_stype,
  293. subtitles=tab.subtitles,
  294. subtitles_audio=tab.subtitles_audio,
  295. video_nums=tab.video_nums,
  296. video_crop_time=tab.video_crop_time,
  297. info=tab.template_info}
  298. return M.response(fd,200,cjson.encode({code=10000,msg = "获取模板信息成功!",data=info}))
  299. end
  300. M.response_db_generate_video_folder_list = function(fd,msg_body,isok,tab)
  301. local err_code = '{"code": 10000, "msg": "获取生成视频文件夹列表失败!", "data": [] }'
  302. if not isok then
  303. return M.response(fd,200, err_code)
  304. end
  305. local total_count = 0
  306. local list = tab
  307. list,total_count = M.getPageData(msg_body.page,msg_body.count,list)
  308. if #list <= 0 or list == nil then
  309. return M.response(fd,200,err_code)
  310. -- return M.response(fd,200,cjson.encode({code=10000,msg = "获取资源文件夹列表成功!",data=folder_list,total_count=total_count}))
  311. end
  312. skynet.call("dbmgr","lua","on_recv","folder_gen_video_list_nums",list,fd,total_count)
  313. end
  314. M.response_db_folder_generate_video_list_nums = function(fd,list,count_list,total_count)
  315. local folder_list = {}
  316. for i = 1, #list, 1 do
  317. folder_list[i] = {file_nums=count_list[list[i].id],folder_name=list[i].book_name,folder_id=list[i].id,create_time=list[i].create_time,info=list[i].generate_video_info}
  318. end
  319. return M.response(fd,200,cjson.encode({code=10000,msg = "获取生成视频文件夹列表成功!",data=folder_list,total_count=total_count}))
  320. end
  321. --回应db获取文件夹内的生成视频
  322. M.response_db_get_video_list_by_folder_id = function(fd,msg_body,isok,tab)
  323. if not isok then
  324. return M.response(fd,200,'{"code": 10000, "msg": "获取文件夹内的生成视频失败", "data": [] }')
  325. end
  326. local list = tab
  327. local folder_list = {}
  328. local total_count = 0
  329. list,total_count = M.getPageData(msg_body.page,msg_body.count,list)
  330. for i = 1, #list, 1 do
  331. table.insert(folder_list,i,{template_id=list[i].template_id,video_state=list[i].video_state,video_url=list[i].video_url,create_time=list[i].create_time,file_id=list[i].id,info=list[i].generate_video_info,file_name=list[i].video_name,custom=list[i].custom,path=list[i].path})
  332. end
  333. if #list<=0 then
  334. return M.response(fd,200,'{"code": 10000, "msg": "获取文件夹内的生成视频失败", "data": [] }')
  335. end
  336. return M.response(fd,200,cjson.encode({code=10000,msg = "获取文件夹内的生成视频成功!",data=folder_list,total_count=total_count}))
  337. end
  338. M.response_db_search_generate_video_file = function(fd,msg_body,isok,tab)
  339. local error_json = '{"code": 10000, "msg": "搜索失败", "data": [] }'
  340. if not isok then
  341. return M.response(fd,200,error_json)
  342. end
  343. local list = tab
  344. local file_list = {}
  345. local total_count = 0
  346. list ,total_count = M.getPageData(msg_body.page,msg_body.count,list)
  347. for i = 1, #list, 1 do
  348. table.insert(file_list,i,{template_id=list[i].template_id,video_state=list[i].video_state,video_url=list[i].video_url,create_time=list[i].create_time,file_id=list[i].id,info=list[i].generate_video_info,file_name=list[i].video_name,custom=list[i].custom,path=list[i].path})
  349. -- table.insert(file_list,i,{create_time=list[i].create_time,file_id=list[i].id,info=list[i].generate_video_info,file_name=list[i].video_name})
  350. end
  351. if #list<=0 then
  352. return M.response(fd,200,error_json)
  353. end
  354. return M.response(fd,200,cjson.encode({code=10000,msg = "搜索成功",data=file_list,total_count=total_count}))
  355. end
  356. M.response_db_generate_video = function(fd,isok)
  357. if not isok then
  358. return M.response(fd,200,cjson.encode({code=10001,msg = "生成视频失败"}))
  359. end
  360. return M.response(fd,200,cjson.encode({code=10000,msg = "生成视频成功"}))
  361. end
  362. M.response_db_check_folder_repeat_file = function(fd,msg_body,isok,tab)
  363. -- skynet.error("response_db_check_folder_repeat_file",tab)
  364. -- M.dump(tab)
  365. if #tab.normal_file_name_list<=0 then
  366. tab.normal_file_name_list = ''
  367. end
  368. if #tab.repeat_file_name_list<=0 then
  369. tab.repeat_file_name_list = ''
  370. end
  371. return M.response(fd,200,cjson.encode({code=10000,msg = "检测完毕!",data=tab}))
  372. end
  373. M.response_db_get_share_user_list = function(fd,msg_body,isok,tab)
  374. local error_json = '{"code": 10000, "msg": "获取失败", "data": [] }'
  375. local list = tab
  376. local user_list = {}
  377. local total_count = 0
  378. list ,total_count = M.getPageData(msg_body.page,msg_body.count,list)
  379. if #list<=0 then
  380. return M.response(fd,200,error_json)
  381. end
  382. local temp = {}
  383. for i = 1, #list, 1 do
  384. table.insert(temp,i,{user_id=list[i].id,name=list[i].name,group_id=list[i].group_id})
  385. end
  386. return M.response(fd,200,cjson.encode({code=10000,msg = "获取成功!",data=temp,total_count=total_count}))
  387. end
  388. M.response_db_get_folder_info = function(fd,msg_body,isok,tab)
  389. local error_json = '{"code": 10000, "msg": "获取失败", "data": [] }'
  390. if not isok or #tab<=0 then
  391. return M.response(fd,200,error_json)
  392. end
  393. return M.response(fd,200,cjson.encode({code=10000,msg = "获取成功!",data=tab}))
  394. end
  395. M.response_db_get_file_info_by_id = function(fd,msg_body,isok,tab)
  396. local error_json = '{"code": 10001, "msg": "获取失败" }'
  397. if not isok or #tab<=0 then
  398. return M.response(fd,200,error_json)
  399. end
  400. return M.response(fd,200,cjson.encode({code=10000,msg = "获取成功!",data=tab}))
  401. end
  402. M.response_db_get_not_have_file_list_by_list = function(fd,msg_body,isok,tab)
  403. local error_json = '{"code": 10000, "msg": "没有被删除的文件","data": [] }'
  404. if not isok or #tab<=0 then
  405. return M.response(fd,200,error_json)
  406. end
  407. return M.response(fd,200,cjson.encode({code=10000,msg = "获取被删除的文件!",data=tab}))
  408. end
  409. M.response_setItem = function(fd,isok)
  410. local error_json = '{"code": 10001, "msg": "保存数据失败" }'
  411. if not isok then
  412. return M.response(fd,200,error_json)
  413. end
  414. return M.response(fd,200,cjson.encode({code=10000,msg = "保存数据成功"}))
  415. end
  416. M.response_getItem = function(fd,isok,tab)
  417. local error_json = '{"code": 10001, "msg": "获取数据失败" }'
  418. if not isok then
  419. return M.response(fd,200,error_json)
  420. end
  421. if tab==nil then
  422. return M.response(fd,200,error_json)
  423. end
  424. return M.response(fd,200,cjson.encode({code=10000,msg = "获取数据成功",data=tab}))
  425. end
  426. M.getRandomIndex = function(array)
  427. local count = #array
  428. local index = math.random(1,count)
  429. return index
  430. end
  431. M.getRandomArray = function(array)
  432. local numberTable = {}
  433. for i = 1, #array do
  434. table.insert(numberTable,i)
  435. end
  436. -- local count = #array
  437. function getRandom()
  438. local index = math.random(1,#numberTable)
  439. local random = numberTable[index]
  440. table.remove(numberTable, index)
  441. return random
  442. end
  443. local randomNumList = {}
  444. for i = 1, #array do
  445. local random = getRandom()
  446. table.insert(randomNumList,random)
  447. end
  448. return randomNumList
  449. end
  450. return M