tools.lua 15 KB

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