init.lua 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. local skynet = require "skynet"
  2. local s = require "service"
  3. local mysql = require "skynet.db.mysql"
  4. local runconfig = require("run_config")
  5. local db = nil
  6. local cjson = require "cjson"
  7. local tools = require "tools"
  8. s.resp.parse_template_info = function(info,template_info)
  9. --片头
  10. local head_video = {}
  11. local type = 1 -- 类型 1=片头视频,2=视频,3=片尾视频,4=bgm音频,5=片尾音频,6=图片
  12. local video_crop_time = info.video_crop_time
  13. local ratio = info.ratio
  14. local user_id = info.user_id
  15. -- skynet.error("(info.template_info",info.template_info)
  16. -- if template_info.piantou_video_data.is_random then --说明选择的是随机
  17. -- local folder_list = template_info.piantou_video_data.random_folder_list
  18. -- if folder_list ==nil or #folder_list ==0 then
  19. -- skynet.error("开始随机生成全部片头")
  20. -- --说明选择的全部随机
  21. -- local isOk,list = s.resp.get_random_res_by_type(user_id,1,ratio,0)
  22. -- if not isOk then
  23. -- skynet.error("缺少片头资源!")
  24. -- else
  25. -- if #list>=1 then
  26. -- skynet.error("获取全部随机的一个指定片头")
  27. -- table.insert(head_video,1,{video_id =list[1].id,path =cjson.decode( list[tools.getRandomIndex(list)].file_info).path,duration=list[1].duration })
  28. -- tools.dump(head_video)
  29. -- end
  30. -- end
  31. -- else -- 说明选择了指定文件夹随机
  32. -- local isOk,list = s.resp.get_random_res_by_folder_list(1,ratio,folder_list,0,user_id)
  33. -- if not isOk then
  34. -- skynet.error("缺少片头资源!")
  35. -- else
  36. -- if #list>=1 then
  37. -- table.insert(head_video,1,list[tools.getRandomIndex(list)])
  38. -- end
  39. -- end
  40. -- end
  41. -- else
  42. -- if template_info.piantou_video_data.is_specified then --说明是指定了文件
  43. -- local isok,item = s.resp.get_res_file_by_id(template_info.piantou_video_data.specified_res_list[1].file_id)
  44. -- if isok then
  45. -- table.insert(head_video,1,item)
  46. -- end
  47. -- else
  48. -- skynet.error("参数错误!没有指定随机也没指定固定文件")
  49. -- end
  50. -- end
  51. if template_info.piantou_video_data.random_folder_resource_data~=nil then
  52. local data = template_info.piantou_video_data.random_folder_resource_data
  53. table.insert(head_video,1,{video_id =data.info.id,path =data.info.path,duration=data.info.duration,info=data })
  54. -- table.insert(head_video,1,data)
  55. end
  56. if #head_video <= 0 then
  57. skynet.error("片头是空的")
  58. end
  59. --内容
  60. local content_video = {}
  61. local json_info = cjson.decode(info.template_info)
  62. -- skynet.error("json_info.subtitles_audio_duration",json_info.subtitles_audio_duration)
  63. -- skynet.error("head_video[1]",head_video[1].duration)
  64. -- tools.dump(json_info.subtitles_audio_duration)
  65. -- skynet.error("head_video[1]",head_video[1].info)
  66. -- tools.dump(head_video[1].info)
  67. local need_num = math.floor((json_info.subtitles_audio_duration - head_video[1].duration) / (video_crop_time)) +1
  68. if template_info.content_video_data.is_random then --说明选择的是随机
  69. local folder_list = template_info.content_video_data.random_folder_list
  70. if folder_list ==nil or #folder_list ==0 then
  71. --说明选择的全部随机
  72. local isOk,list = s.resp.get_random_res_by_type(user_id,2,ratio,(video_crop_time+5))
  73. if not isOk then
  74. return skynet.error("缺少内容资源!")
  75. else
  76. skynet.error("need_num",need_num)
  77. if need_num>#list then
  78. skynet.error("缺少内容资源!")
  79. end
  80. local random_array = tools.getRandomArray(list)
  81. -- skynet.error("random_array")
  82. -- tools.dump(random_array)
  83. for i = 1, need_num, 1 do
  84. local random_index = random_array[i]
  85. table.insert(content_video,i,{video_id =list[random_index].id,path =cjson.decode( list[random_index].file_info).path,duration=list[random_index].duration,info = list[random_index].file_info })
  86. -- table.insert(content_video,i,list[random_index].file_info)
  87. end
  88. end
  89. else -- 说明选择了指定文件夹随机
  90. local isOk,list = s.resp.get_random_res_by_folder_list(2,ratio,folder_list,(video_crop_time+5),user_id)
  91. if not isOk then
  92. return skynet.error("缺少内容资源!")
  93. else
  94. if need_num>#list then
  95. skynet.error("缺少内容资源!")
  96. end
  97. local random_array = tools.getRandomArray(list)
  98. for i = 1, need_num, 1 do
  99. local random_index = random_array[i]
  100. table.insert(content_video,i,{video_id =list[random_index].id,path =cjson.decode( list[random_index].file_info).path,duration=list[random_index].duration ,info = list[random_index].file_info})
  101. -- table.insert(content_video,i,list[random_index].file_info)
  102. end
  103. end
  104. end
  105. else
  106. if template_info.content_video_data.is_specified then --说明是指定了文件
  107. local specified_res_list = template_info.content_video_data.specified_res_list
  108. if #specified_res_list<need_num then
  109. skynet.error("指定文件缺少",need_num)
  110. else
  111. for i = 1, #specified_res_list, 1 do
  112. local isok,item = s.resp.get_res_file_by_id(specified_res_list[1].file_id)
  113. if isok then
  114. -- table.insert(content_video,i, item.file_info)
  115. table.insert(head_video,i,{video_id =item.id,path =cjson.decode( item.file_info).path,duration=item.duration,info = item.file_info})
  116. end
  117. end
  118. end
  119. else
  120. skynet.error("参数错误!没有指定随机也没指定固定文件")
  121. end
  122. end
  123. --片尾
  124. local end_video = {}
  125. -- if template_info.pianwei_video_data.is_random then --说明选择的是随机
  126. -- local folder_list = template_info.pianwei_video_data.random_folder_list
  127. -- if folder_list ==nil or #folder_list ==0 then
  128. -- --说明选择的全部随机
  129. -- local isOk,list = s.resp.get_random_res_by_type(user_id,3,ratio,0)
  130. -- if not isOk then
  131. -- return skynet.error("缺少片尾资源!")
  132. -- else
  133. -- if #list>=1 then
  134. -- table.insert(end_video,1,{video_id =list[1].id,path =cjson.decode( list[tools.getRandomIndex(list)].file_info).path,duration=list[1].duration })
  135. -- end
  136. -- end
  137. -- else -- 说明选择了指定文件夹随机
  138. -- local isOk,list = s.resp.get_random_res_by_folder_list(3,ratio,folder_list,0,user_id)
  139. -- if not isOk then
  140. -- return skynet.error("缺少片尾资源!")
  141. -- else
  142. -- if #list>=1 then
  143. -- table.insert(end_video,1,{video_id =list[1].id,path =cjson.decode( list[tools.getRandomIndex(list)].file_info).path,duration=list[1].duration })
  144. -- end
  145. -- end
  146. -- end
  147. -- else
  148. -- if template_info.pianwei_video_data.is_specified then --说明是指定了文件
  149. -- local isok,item = s.resp.get_res_file_by_id(template_info.pianwei_video_data.specified_res_list[1].file_id)
  150. -- if isok then
  151. -- table.insert(end_video,1,item)
  152. -- end
  153. -- else
  154. -- skynet.error("参数错误!没有指定随机也没指定固定文件")
  155. -- end
  156. -- end
  157. if template_info.pianwei_video_data.random_folder_resource_data~=nil then
  158. local data = template_info.pianwei_video_data.random_folder_resource_data
  159. table.insert(end_video,1,{video_id =data.info.id,path =data.info.path,duration=data.info.duration,info = data })
  160. -- table.insert(end_video,1,data)
  161. end
  162. if #end_video<=0 then
  163. skynet.error("添加片尾资源失败")
  164. end
  165. --BGM音效
  166. local bgm_audio = {}
  167. -- if template_info.bgm_audio_data.is_random then --说明选择的是随机
  168. -- local folder_list = template_info.bgm_audio_data.random_folder_list
  169. -- if template_info.bgm_audio_data.random_folder_list ==nil or #template_info.bgm_audio_data.random_folder_list ==0 then
  170. -- --说明选择的全部随机
  171. -- local isOk,list = s.resp.get_random_res_by_type(user_id,4,ratio,0)
  172. -- if not isOk then
  173. -- skynet.error("缺少BGM音效资源!")
  174. -- else
  175. -- if #list>=1 then
  176. -- table.insert(bgm_audio,1,{video_id =list[1].id,path =cjson.decode( list[tools.getRandomIndex(list)].file_info).path,duration=list[1].duration })
  177. -- end
  178. -- end
  179. -- else -- 说明选择了指定文件夹随机
  180. -- local isOk,list = s.resp.get_random_res_by_folder_list(4,ratio,folder_list,0,user_id)
  181. -- if not isOk then
  182. -- skynet.error("缺少BGM音效资源!")
  183. -- else
  184. -- if #list>=1 then
  185. -- table.insert(bgm_audio,1,{video_id =list[1].id,path =cjson.decode( list[tools.getRandomIndex(list)].file_info).path,duration=list[1].duration })
  186. -- end
  187. -- end
  188. -- end
  189. -- else
  190. -- if template_info.bgm_audio_data.is_specified then --说明是指定了文件
  191. -- local isok,item = s.resp.get_res_file_by_id(template_info.bgm_audio_data.specified_res_list[1].file_id)
  192. -- if isok then
  193. -- table.insert(bgm_audio,1,item)
  194. -- end
  195. -- else
  196. -- skynet.error("参数错误!没有指定随机也没指定固定文件")
  197. -- end
  198. -- end
  199. if template_info.bgm_audio_data.random_folder_resource_data~=nil then
  200. local data = template_info.bgm_audio_data.random_folder_resource_data
  201. table.insert(bgm_audio,1,{video_id =data.info.id,path =data.info.path,duration=data.info.duration,info = data })
  202. -- table.insert(bgm_audio,1,data)
  203. end
  204. return head_video,content_video,end_video,bgm_audio
  205. end
  206. s.resp.getUserById = function(id)
  207. end
  208. s.resp.updateUserInfoById = function(id)
  209. end
  210. s.resp.createUser = function()
  211. end
  212. s.resp.deleteUserById = function(id)
  213. end
  214. s.resp.verify = function(msg_body)
  215. local isok = false
  216. local user_data = nil
  217. isok,user_data = s.resp.select_user_by_account(msg_body.account)
  218. if not isok or user_data==nil then
  219. return isok,user_data
  220. end
  221. isok = false
  222. if msg_body.password==user_data.password then
  223. isok = true
  224. -- user_data = {user_id=1,user_name="小丑"}
  225. end
  226. return isok,user_data
  227. end
  228. s.resp.select_user_by_account = function(account)
  229. local sql = string.format("select * from users where account = '%s'",account)
  230. skynet.error("sql",sql)
  231. local res = db:query(sql)
  232. -- 判断是否找到数据
  233. if res and #res > 0 then
  234. local tab = tools.getDbResData(res)
  235. skynet.error("Found data:",tab)
  236. return true,tab
  237. else
  238. skynet.error("No data found.")
  239. return false,nil
  240. end
  241. -- tools.dump(res)
  242. end
  243. s.resp.select_user_by_user_id = function(user_id)
  244. end
  245. s.resp.add_new_user = function(msg_body)
  246. -- skynet.error("msg_body",msg_body.group_type)
  247. local sql = string.format("INSERT INTO users (name,account, password,permit_id,group_id) VALUES ('%s', '%s','%s',%d,%d)",msg_body.user_name,msg_body.account,msg_body.password,msg_body.permit_id,msg_body.group_id)
  248. local res = db:query(sql)
  249. return true
  250. end
  251. --- 新建文件夹
  252. s.resp.new_folder = function(msg_body,fd)
  253. skynet.fork(function(_fd)
  254. local sql = string.format("INSERT INTO folder_list_tab (user_id,folder_name, folder_type,is_public,classification_id) VALUES (%d, '%s',%d,%d,%d)",msg_body.user_id,msg_body.folder_name,msg_body.folder_type,msg_body.is_public,msg_body.classification_id)
  255. -- skynet.error(sql)
  256. local res = db:query(sql)
  257. tools.response_db_new_folder(_fd,true)
  258. end,fd)
  259. return true
  260. end
  261. --获取资源文件夹列表
  262. s.resp.folder_list = function(msg_body,fd)
  263. skynet.fork(function(_fd)
  264. local sql = string.format("select * from folder_list_tab where user_id = %d AND folder_type = %d AND classification_id = %d",msg_body.user_id,msg_body.type,msg_body.classification_id)
  265. local res = db:query(sql)
  266. -- skynet.error('folder_list',sql)
  267. -- 判断是否找到数据
  268. if res and #res > 0 then
  269. local tab = {}
  270. if #res == 1 then
  271. tab[1] = tools.getDbResData(res)
  272. else
  273. tab = tools.getDbResData(res)
  274. end
  275. skynet.error("Found data:")
  276. -- tools.dump(tab)
  277. -- return true,tab
  278. tools.response_db_folder_list(_fd,msg_body,true,tab)
  279. else
  280. skynet.error("No data found.")
  281. tools.response_db_folder_list(_fd,msg_body,false,nil)
  282. -- return false,nil
  283. end
  284. end,fd)
  285. return true
  286. end
  287. --获取文件夹列表每个文件夹的文件数量
  288. s.resp.folder_res_list_nums = function(folder_list,fd,total_count)
  289. skynet.fork(function(_fd)
  290. local temp ={}
  291. for i = 1, #folder_list, 1 do
  292. local msg_body = folder_list[i]
  293. -- tools.dump(msg_body)
  294. local sql = string.format("select * from res_list_tab where user_id = %d AND folder_id = %d AND is_public = %d AND is_delete = 0",msg_body.user_id,msg_body.id,msg_body.is_public)
  295. local res = db:query(sql)
  296. local count = 0
  297. if res and #res > 0 then
  298. count = #res
  299. skynet.error("Found data:")
  300. -- tools.dump(tab)
  301. -- return true,tab
  302. else
  303. skynet.error("No data found.",msg_body.id)
  304. end
  305. temp[msg_body.id] = count
  306. -- table.insert(temp,msg_body.id,count)
  307. end
  308. tools.response_db_folder_list_nums(_fd,folder_list,temp,total_count)
  309. end,fd)
  310. end
  311. --获取指定文件夹内的资源列表
  312. s.resp.folder_res_list = function(msg_body,fd,call_back)
  313. skynet.fork(function(_fd)
  314. local sql_duration = ""
  315. if msg_body.duration~=nil then
  316. sql_duration = string.format("AND duration >= %d",msg_body.duration)
  317. end
  318. local sql = string.format("select * from res_list_tab where user_id = %d AND folder_id = %d AND is_public = %d AND is_delete = 0"..sql_duration,msg_body.user_id,msg_body.folder_id,msg_body.is_public)
  319. local res = db:query(sql)
  320. skynet.error('folder_res_list',sql)
  321. -- 判断是否找到数据
  322. if res and #res > 0 then
  323. local tab = {}
  324. if #res == 1 then
  325. tab[1] = tools.getDbResData(res)
  326. else
  327. tab = tools.getDbResData(res)
  328. end
  329. skynet.error("Found data:")
  330. if not call_back and _fd~=nil then
  331. tools.response_db_folder_res_list(_fd,msg_body,true,tab)
  332. else
  333. if call_back ~=nil then
  334. call_back(tab)
  335. end
  336. end
  337. -- tools.dump(tab)
  338. -- return true,tab
  339. else
  340. skynet.error("No data found.",call_back)
  341. if call_back == nil and _fd~=nil then
  342. tools.response_db_folder_res_list(_fd,msg_body,false,nil)
  343. else
  344. if call_back ~=nil then
  345. call_back({})
  346. end
  347. end
  348. -- return false,nil
  349. end
  350. end,fd)
  351. return true
  352. end
  353. s.resp.on_recv = function (source, msg_id, msg_body,fd,...)
  354. if db==nil then
  355. return false,nil
  356. end
  357. if s.resp[msg_id]==nil then
  358. return false,nil
  359. end
  360. return s.resp[msg_id](msg_body,fd,...)
  361. end
  362. --添加资源数据到表里
  363. s.resp.add_res_file = function(msg_body)
  364. skynet.fork(function()
  365. local temp = '{"width": 100, "height": 100, "duration": 123}'
  366. temp = cjson.encode(msg_body)
  367. local duration = 0
  368. if msg_body.duration~=nil then
  369. duration = tonumber(msg_body.duration)
  370. end
  371. local sql = string.format("INSERT INTO res_list_tab (user_id, folder_id, file_type, file_name, is_public,file_info,duration,classification_id) VALUES (%d, %d, %d, '%s',%d,'%s',%f,%d)",msg_body.user_id,msg_body.folder_id,msg_body.stype,msg_body.file_name,msg_body.is_public,temp,duration,msg_body.classification_id)
  372. local res = db:query(sql)
  373. end)
  374. return true
  375. end
  376. --更改文件夹名
  377. s.resp.reset_folder_name = function(msg_body)
  378. local isOk = s.resp.get_res_folder_by_id(msg_body.folder_id)
  379. if not isOk then
  380. return false
  381. end
  382. local sql = string.format("UPDATE folder_list_tab SET folder_name ='%s' WHERE id = %d AND is_public = %d",msg_body.name,msg_body.folder_id,msg_body.is_public)
  383. db:query(sql)
  384. return true
  385. end
  386. --更改资源文件名
  387. s.resp.reset_res_name = function(msg_body)
  388. local isOk = s.resp.get_res_file_by_id(msg_body.file_id)
  389. if not isOk then
  390. return false
  391. end
  392. local sql = string.format("UPDATE res_list_tab SET file_name ='%s' WHERE id = %d AND folder_id = %d AND is_public = %d AND is_delete = 0",msg_body.name,msg_body.file_id,msg_body.folder_id,msg_body.is_public)
  393. db:query(sql)
  394. return true
  395. end
  396. --根据id获取资源
  397. s.resp.get_res_file_by_id = function(file_id)
  398. local sql = string.format("select * from res_list_tab where id = %d AND is_delete = 0",file_id)
  399. -- skynet.error("sql",sql)
  400. local res = db:query(sql)
  401. -- 判断是否找到数据
  402. if res and #res > 0 then
  403. local tab = tools.getDbResData(res)
  404. skynet.error("Found data:",tab)
  405. return true,tab
  406. else
  407. skynet.error("No data found.")
  408. return false,nil
  409. end
  410. end
  411. --根据id获取文件夹
  412. s.resp.get_res_folder_by_id = function(folder_id)
  413. local sql = string.format("select * from folder_list_tab where id = %d ",folder_id)
  414. -- skynet.error("sql",sql)
  415. local res = db:query(sql)
  416. -- 判断是否找到数据
  417. if res and #res > 0 then
  418. local tab = tools.getDbResData(res)
  419. skynet.error("Found data:",tab)
  420. return true,tab
  421. else
  422. skynet.error("No data found.")
  423. return false,nil
  424. end
  425. end
  426. --搜索资源
  427. s.resp.search_res = function(msg_body,fd)
  428. skynet.fork(function(_fd)
  429. local sql = string.format("SELECT * FROM res_list_tab WHERE file_type = %d AND file_name LIKE '%%%s%%' AND is_delete = 0 AND classification_id = %d",msg_body.type ,msg_body.search_content,msg_body.classification_id)
  430. skynet.error("sql",sql)
  431. local res = db:query(sql)
  432. if res and #res > 0 then
  433. local tab = {}
  434. if #res == 1 then
  435. tab[1] = tools.getDbResData(res)
  436. else
  437. tab = tools.getDbResData(res)
  438. end
  439. skynet.error("Found data:")
  440. -- tools.dump(tab)
  441. -- return true,tab
  442. tools.response_db_search_res(_fd,msg_body,true,tab)
  443. else
  444. skynet.error("No data found.")
  445. -- return false,nil
  446. tools.response_db_search_res(_fd,msg_body,false,nil)
  447. end
  448. end,fd)
  449. return true
  450. end
  451. --根据所有文件id查到所有文件的url并删除
  452. local fork_get_file_list_url_and_delete = function(deleteString,call_back)
  453. local sql = string.format("SELECT * FROM res_list_tab WHERE id IN (%s)", deleteString)
  454. local res = db:query(sql)
  455. if res and #res > 0 then
  456. local tab = {}
  457. if #res == 1 then
  458. tab[1] = tools.getDbResData(res)
  459. else
  460. tab = tools.getDbResData(res)
  461. end
  462. skynet.error("Found data:fork_get_file_list_url_and_delete")
  463. local url_list = {}
  464. local count = 1
  465. for i = 1, #tab, 1 do
  466. if tab[i].ref_id==0 then
  467. local file_info = cjson.decode(tab[i].file_info)
  468. table.insert(url_list,count,file_info.path)
  469. count = count + 1
  470. end
  471. end
  472. skynet.send("tools_work","lua","delete_file",url_list)
  473. -- tools.dump(url_list)
  474. -- return true,tab
  475. else
  476. skynet.error("No data found.")
  477. -- return false,nil
  478. end
  479. if call_back~=nil then
  480. call_back()
  481. end
  482. end
  483. --执行删除文件操作
  484. local fork_delete_file = function(msg_body)
  485. -- tools.dump(msg_body)
  486. local listToDelete = {}
  487. for i = 1, #msg_body.file_id_list, 1 do
  488. table.insert(listToDelete,i,msg_body.file_id_list[i])
  489. end
  490. local deleteString = table.concat(listToDelete, ',')
  491. local sql = string.format("UPDATE res_list_tab SET is_delete = %d WHERE id IN (%s)",1,deleteString)
  492. db:query(sql)
  493. skynet.fork(fork_get_file_list_url_and_delete,deleteString,function()
  494. local delete_sql = string.format("DELETE FROM res_list_tab WHERE id IN (%s) AND is_public = %d AND user_id = %d", deleteString,msg_body.is_public,msg_body.user_id)
  495. db:query(delete_sql)
  496. end)
  497. end
  498. --执行删除文件夹操作
  499. local fork_delete_folder = function(msg)
  500. for i = 1, #msg.folder_id_list, 1 do
  501. local isok = s.resp.folder_res_list({
  502. folder_id = msg.folder_id_list[i],
  503. user_id = msg.user_id,
  504. is_public=msg.is_public},nil,function(list)
  505. -- skynet.error("执行删除文件夹操作",#list)
  506. if #list>0 then
  507. local temp = {}
  508. for i = 1, #list, 1 do
  509. table.insert(temp,i,list[i].id)
  510. end
  511. skynet.fork(fork_delete_file,{file_id_list=temp,user_id=msg.user_id,is_public=msg.is_public})
  512. end
  513. end)
  514. end
  515. end
  516. --删除资源文件
  517. s.resp.delete_res = function(msg_body)
  518. skynet.fork(fork_delete_file,msg_body)
  519. return true
  520. end
  521. --删除文件夹
  522. s.resp.delete_folder =function(msg_body)
  523. local listToDelete = {}
  524. for i = 1, #msg_body.folder_id_list, 1 do
  525. table.insert(listToDelete,i,msg_body.folder_id_list[i])
  526. end
  527. local deleteString = table.concat(listToDelete, ',')
  528. local sql = string.format("DELETE FROM folder_list_tab WHERE id IN (%s) AND is_public = %d AND user_id = %d", deleteString,msg_body.is_public,msg_body.user_id)
  529. db:query(sql)
  530. skynet.fork(fork_delete_folder,msg_body)
  531. return true
  532. end
  533. --新建模板
  534. s.resp.create_template = function(msg_body,fd)
  535. local template_info = "{}"
  536. if msg_body.template_info~=nil then
  537. template_info = msg_body.template_info
  538. end
  539. local sql = string.format("INSERT INTO template_list_tab (user_id,project_name, book_name,ratio,video_stype,subtitles,subtitles_audio,video_nums,video_crop_time,template_info) VALUES (%d, '%s','%s',%d,%d,'%s','%s',%d,%d,'%s')",msg_body.user_id,msg_body.project_name,msg_body.book_name,msg_body.ratio,msg_body.video_stype,msg_body.subtitles,msg_body.subtitles_audio,msg_body.video_nums,msg_body.video_crop_time,template_info)
  540. local res = db:query(sql)
  541. -- skynet.error("sql",sql)
  542. return true
  543. end
  544. --获取模板列表
  545. s.resp.get_template_list = function(msg_body,fd)
  546. skynet.fork(function(_fd)
  547. local sql = string.format("select * from template_list_tab where user_id = %d AND video_stype = %d",msg_body.user_id,msg_body.video_stype)
  548. skynet.error("sql",sql)
  549. local res = db:query(sql)
  550. -- 判断是否找到数据
  551. if res and #res > 0 then
  552. local tab = {}
  553. if #res == 1 then
  554. tab[1] = tools.getDbResData(res)
  555. else
  556. tab = tools.getDbResData(res)
  557. end
  558. skynet.error("Found data:",tab)
  559. -- return true,tab
  560. tools.response_db_get_template_list(_fd,msg_body,true,tab)
  561. else
  562. skynet.error("No data found.")
  563. -- return false,nil
  564. tools.response_db_get_template_list(_fd,msg_body,false,nil)
  565. end
  566. end,fd)
  567. return true
  568. end
  569. --删除模板
  570. s.resp.delete_template = function(msg_body)
  571. local listToDelete = {}
  572. for i = 1, #msg_body.template_id_list, 1 do
  573. table.insert(listToDelete,i,msg_body.template_id_list[i])
  574. end
  575. local deleteString = table.concat(listToDelete, ',')
  576. local sql = string.format("DELETE FROM template_list_tab WHERE id IN (%s) AND user_id = %d", deleteString,msg_body.user_id)
  577. db:query(sql)
  578. skynet.error(sql)
  579. return true
  580. end
  581. --保存/修改模板
  582. s.resp.save_template = function(msg_body,fd)
  583. local template_info = "{}"
  584. if msg_body.template_info~="" and msg_body.template_info~=nil then
  585. template_info = cjson.decode(msg_body.template_info)
  586. if template_info.creat_video_list~=nil and template_info.creat_video_list~="" then
  587. if #template_info.creat_video_list >0 then
  588. for i = 1, #template_info.creat_video_list, 1 do
  589. if template_info.creat_video_list[i]~=nil then
  590. if template_info.creat_video_list[i].title_html~=nil then
  591. template_info.creat_video_list[i].title_html = tools.base64encode(template_info.creat_video_list[i].title_html)
  592. end
  593. end
  594. end
  595. end
  596. end
  597. template_info = cjson.encode(template_info)
  598. end
  599. local sql = string.format("UPDATE template_list_tab SET project_name = '%s' , book_name = '%s' ,ratio = %d, video_stype = %d, subtitles = '%s', subtitles_audio = '%s' ,video_nums = %d, video_crop_time = %d, template_info = '%s' WHERE id = %d ",msg_body.project_name,msg_body.book_name,msg_body.ratio,msg_body.video_stype,msg_body.subtitles,msg_body.subtitles_audio,msg_body.video_nums,msg_body.video_crop_time,template_info,msg_body.template_id)
  600. -- skynet.error("sql",sql)
  601. db:query(sql)
  602. return true
  603. end
  604. --搜索模板
  605. s.resp.search_template = function(msg_body,fd)
  606. skynet.fork(function(_fd)
  607. local sql = string.format("SELECT * FROM template_list_tab WHERE user_id = %d AND video_stype = %d AND book_name LIKE '%%%s%%'",msg_body.user_id ,msg_body.video_stype,msg_body.search_content)
  608. -- local sql = string.format("select * from template_list_tab where user_id = %d AND video_stype = %d",msg_body.user_id,msg_body.video_stype)
  609. skynet.error("sql",sql)
  610. local res = db:query(sql)
  611. -- 判断是否找到数据
  612. if res and #res > 0 then
  613. local tab = {}
  614. if #res == 1 then
  615. tab[1] = tools.getDbResData(res)
  616. else
  617. tab = tools.getDbResData(res)
  618. end
  619. skynet.error("Found data:",tab)
  620. -- return true,tab
  621. tools.response_db_search_template(_fd,msg_body,true,tab)
  622. else
  623. skynet.error("No data found.")
  624. -- return false,nil
  625. tools.response_db_search_template(_fd,msg_body,false,nil)
  626. end
  627. end,fd)
  628. return true
  629. end
  630. --根据模板ID获取模板信息
  631. s.resp.get_template_info_by_id = function(msg_body,fd,call_back)
  632. skynet.fork(function(_fd)
  633. local sql = string.format("SELECT * FROM template_list_tab WHERE user_id = %d AND id = %d",msg_body.user_id ,msg_body.template_id)
  634. -- local sql = string.format("select * from template_list_tab where user_id = %d AND video_stype = %d",msg_body.user_id,msg_body.video_stype)
  635. skynet.error("sql",sql)
  636. local res = db:query(sql)
  637. -- 判断是否找到数据
  638. if res and #res > 0 then
  639. local tab = tools.getDbResData(res)
  640. skynet.error("Found data:",tab)
  641. -- return true,tab
  642. if call_back~=nil and fd ==nil then
  643. call_back(tab)
  644. else
  645. tools.response_db_get_template_info_by_id(_fd,msg_body,true,tab)
  646. end
  647. else
  648. skynet.error("No data found.")
  649. -- return false,nil
  650. if call_back~=nil and fd ==nil then
  651. call_back(nil)
  652. else
  653. tools.response_db_search_template(_fd,msg_body,false,nil)
  654. end
  655. end
  656. end,fd)
  657. return true
  658. end
  659. --获取指定类型所有文件
  660. s.resp.get_file_list_by_type = function(msg_body,fd)
  661. skynet.fork(function(_fd,_msg_body)
  662. local sql_duration = ""
  663. if msg_body.duration~=nil then
  664. sql_duration = string.format(" AND duration >= %f",msg_body.duration)
  665. end
  666. local sql = string.format("SELECT * FROM res_list_tab WHERE file_type = %d AND user_id = %d AND is_delete = 0"..sql_duration,msg_body.type,msg_body.user_id)
  667. local res = db:query(sql)
  668. -- skynet.error(sql)
  669. if res and #res > 0 then
  670. local tab = {}
  671. if #res == 1 then
  672. tab[1] = tools.getDbResData(res)
  673. else
  674. tab = tools.getDbResData(res)
  675. end
  676. tools.response_db_get_file_list_by_type(_fd,_msg_body,true,tab)
  677. -- return true,tab
  678. else
  679. skynet.error("No data found.")
  680. -- return false,nil
  681. tools.response_db_get_file_list_by_type(_fd,_msg_body,false,nil)
  682. end
  683. end,fd,msg_body)
  684. end
  685. --随机一个类型,获取相应数量的资源列表
  686. s.resp.get_random_res_by_type = function(user_id,file_type,classification_id,duration)
  687. local sql = string.format("SELECT *FROM res_list_tab WHERE file_type = %d AND is_delete = 0 AND classification_id = %d AND duration>= %f AND user_id = %d", file_type,classification_id,duration,user_id)
  688. local res = db:query(sql)
  689. if res and #res > 0 then
  690. local tab = {}
  691. if #res == 1 then
  692. tab[1] = tools.getDbResData(res)
  693. else
  694. tab = tools.getDbResData(res)
  695. end
  696. -- local test = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  697. -- tools.shuffle(tab)
  698. -- tools.dump(test)
  699. -- tools.dump(tab)
  700. return true,tab
  701. else
  702. skynet.error("No data found.")
  703. return false,nil
  704. end
  705. end
  706. --获取一个类型,指定在文件夹列表里随机
  707. s.resp.get_random_res_by_folder_list = function(file_type,classification_id,folder_list,duration,user_id)
  708. local listToSelect = {}
  709. for i = 1, #folder_list, 1 do
  710. table.insert(listToSelect,i,folder_list[i].folder_id)
  711. end
  712. local selectString = table.concat(listToSelect, ',')
  713. skynet.error("file_type",file_type)
  714. skynet.error("classification_id",classification_id)
  715. skynet.error("folder_list")
  716. tools.dump(folder_list)
  717. skynet.error("user_id",user_id)
  718. local sql = string.format("SELECT* FROM res_list_tab WHERE file_type = %d AND is_delete = 0 AND classification_id = %d AND duration>= %f AND user_id = %d AND folder_id in(%s)", file_type,classification_id,duration,user_id,selectString)
  719. local res = db:query(sql)
  720. if res and #res > 0 then
  721. local tab = {}
  722. if #res == 1 then
  723. tab[1] = tools.getDbResData(res)
  724. else
  725. tab = tools.getDbResData(res)
  726. end
  727. -- tools.dump(tab)
  728. return true,tab
  729. else
  730. skynet.error("No data found.")
  731. return false,nil
  732. end
  733. end
  734. --生成视频数据库
  735. s.resp.generate_video = function(msg_body,fd)
  736. s.resp.get_template_info_by_id(msg_body,nil,function(tab)
  737. -- tools.dump(tab)
  738. if tab ~=nil then
  739. local isOk,error_info = s.resp.check_template_isRight(tab)
  740. if not isOk then
  741. return tools.response(fd,200,error_info)
  742. end
  743. -- tools.dump(tab)
  744. local user_id = msg_body.user_id
  745. local template_id = msg_body.template_id
  746. local file_name = tab.book_name
  747. local video_stype = tab.video_stype
  748. --先查找是否有这个名的文件夹
  749. local sql = string.format("SELECT * FROM generate_video_folder_list_tab WHERE user_id = %d AND book_name = '%s' AND video_stype = %d ",user_id,file_name,video_stype)
  750. local res = db:query(sql)
  751. local info = tools.getDbResData(res)
  752. local write_video_db = function(folder_id)
  753. skynet.error("开始生成",folder_id)
  754. local template_info = cjson.decode(tab.template_info)
  755. local gen_num = #template_info.creat_video_list
  756. local custom = {}
  757. if template_info.custom~=nil and template_info.custom~="" then
  758. custom = template_info.custom
  759. end
  760. -- skynet.error("custom")
  761. -- tools.dump(custom)
  762. for i = 1, gen_num, 1 do -- 模拟数据每次生成5条
  763. local item_info = template_info.creat_video_list[i]
  764. local head_video,content_video,end_video,bgm_audio = s.resp.parse_template_info(tab,item_info)
  765. -- tools.dump(head_video)
  766. -- tools.dump(content_video)
  767. -- tools.dump(end_video)
  768. -- tools.dump(bgm_audio)
  769. local is_corner_mark = item_info.is_corner_mark
  770. local is_video_bottom_text = item_info.is_video_bottom_text
  771. local hot_tag_type = 0
  772. local item_custom = {}
  773. if item_info.custom~=nil and item_info.custom~="" then
  774. item_custom = item_info.custom
  775. end
  776. if is_corner_mark then
  777. hot_tag_type = 1
  778. end
  779. local fictitious_tag_type = 0
  780. if is_video_bottom_text then
  781. fictitious_tag_type = 1
  782. end
  783. local gen_info = {user_id=0,id=0,project_name=tab.project_name,
  784. book_name=tab.book_name,
  785. ratio=tab.ratio,
  786. video_stype=tab.video_stype,
  787. crop_time=tab.video_crop_time,
  788. marketing_title_info={marketing_title_top_h = 0,stroke_h=0,marketing_title=tools.base64decode(item_info.title_html)},
  789. video_list = content_video,
  790. subtitles = {stype=0,subtitle_font="",subtitles_path=template_info.subtitles_path},
  791. subtitles_audio={path = template_info.subtitles_audio_path,duration=template_info.subtitles_audio_duration },
  792. video_last_frame =end_video[1],
  793. video_first_frame = head_video[1],
  794. bgm_list = bgm_audio[1],
  795. hot_tag = {type=hot_tag_type,path=""}, --上边
  796. fictitious_tag = {type=fictitious_tag_type,path=""}, --下边
  797. custom = custom,
  798. video_custom = item_custom,
  799. }
  800. local video_name = gen_info.book_name.."__视频"..i
  801. local book_name = gen_info.book_name
  802. local ratio = gen_info.ratio
  803. local video_stype = gen_info.video_stype
  804. local video_url = ""
  805. local generate_video_info = {}
  806. generate_video_info =item_info
  807. generate_video_info.book_name = tab.book_name
  808. generate_video_info.project_name = tab.project_name
  809. generate_video_info.ratio = tab.ratio
  810. generate_video_info.video_stype = tab.video_stype
  811. generate_video_info = cjson.encode(generate_video_info)
  812. local video_state = 0
  813. sql = string.format("INSERT INTO generate_video_list_tab (user_id,template_id, folder_id,video_name,book_name,ratio,video_stype,video_url,video_state,generate_video_info) VALUES (%d,%d,%d,'%s','%s',%d,%d,'%s',%d,'%s')",user_id,
  814. template_id,
  815. folder_id,
  816. video_name,
  817. book_name,
  818. ratio,
  819. video_stype,
  820. video_url,video_state,generate_video_info)
  821. -- skynet.error("123sss")
  822. -- tools.dump(gen_info)
  823. db:query(sql)
  824. -- skynet.error(sql)
  825. sql = string.format("SELECT * FROM generate_video_list_tab WHERE user_id = %d and folder_id = %d and is_use = 0 LIMIT 1",user_id,folder_id)
  826. res = db:query(sql)
  827. if res and #res > 0 then
  828. local tab = {}
  829. if #res > 0 then
  830. tab = tools.getDbResData(res)
  831. gen_info.id = tab.id
  832. gen_info.user_id = user_id
  833. sql = string.format("UPDATE generate_video_list_tab SET is_use = %d WHERE id =%d",1,gen_info.id)
  834. db:query(sql)
  835. skynet.send("tools_work","lua","generate_video",gen_info)
  836. end
  837. -- return true,tab
  838. else
  839. skynet.error("No data found.")
  840. end
  841. end
  842. --获取5个未使用的生成视频文件
  843. tools.response_db_generate_video(fd,true)
  844. end
  845. if res and #res > 0 then --存在这个文件夹,直接写入到这个文件夹
  846. local folder_id = info.id
  847. write_video_db(folder_id)
  848. else -- 没有这个文件夹则创建一个
  849. sql = string.format("INSERT INTO generate_video_folder_list_tab (user_id,book_name, generate_video_info,video_stype) VALUES (%d, '%s','%s' ,%d)",user_id,file_name,"{}",video_stype)
  850. db:query(sql)
  851. --查找新建的文件夹id
  852. sql = string.format("SELECT * FROM generate_video_folder_list_tab WHERE user_id = %d AND book_name = '%s' AND video_stype = %d ",user_id,file_name,video_stype)
  853. res = db:query(sql)
  854. info = tools.getDbResData(res)
  855. local folder_id = info.id
  856. write_video_db(folder_id)
  857. end
  858. end
  859. end)
  860. return true
  861. end
  862. --获取生成视频文件夹列表
  863. s.resp.get_generate_video_folder_list = function(msg_body,fd)
  864. skynet.fork(function(_fd,_msg_body)
  865. local sql = string.format("select * from generate_video_folder_list_tab where user_id = %d AND video_stype = %d",_msg_body.user_id,_msg_body.video_stype)
  866. local res = db:query(sql)
  867. -- skynet.error('folder_list',sql)
  868. -- 判断是否找到数据
  869. if res and #res > 0 then
  870. local tab = {}
  871. if #res == 1 then
  872. tab[1] = tools.getDbResData(res)
  873. else
  874. tab = tools.getDbResData(res)
  875. end
  876. skynet.error("Found data:")
  877. -- tools.dump(tab)
  878. -- return true,tab
  879. tools.response_db_generate_video_folder_list(_fd,msg_body,true,tab)
  880. else
  881. skynet.error("No data found.")
  882. tools.response_db_folder_list(_fd,msg_body,false,nil)
  883. -- return false,nil
  884. end
  885. end,fd,msg_body)
  886. return true
  887. end
  888. --获取文件夹内的生成视频
  889. s.resp.get_video_list_by_folder_id = function(msg_body,fd,call_back)
  890. skynet.fork(function(_fd)
  891. local sql = string.format("select * from generate_video_list_tab where user_id = %d AND folder_id = %d AND is_delete = 0",msg_body.user_id,msg_body.folder_id)
  892. local res = db:query(sql)
  893. -- skynet.error('folder_res_list',sql)
  894. -- 判断是否找到数据
  895. if res and #res > 0 then
  896. local tab = {}
  897. if #res == 1 then
  898. tab[1] = tools.getDbResData(res)
  899. else
  900. tab = tools.getDbResData(res)
  901. end
  902. skynet.error("Found data:")
  903. if not call_back and _fd~=nil then
  904. tools.response_db_get_video_list_by_folder_id(_fd,msg_body,true,tab)
  905. else
  906. if call_back ~=nil then
  907. call_back(tab)
  908. end
  909. end
  910. -- tools.dump(tab)
  911. -- return true,tab
  912. else
  913. skynet.error("No data found.",call_back)
  914. if call_back == nil and _fd~=nil then
  915. tools.response_db_get_video_list_by_folder_id(_fd,msg_body,false,nil)
  916. else
  917. if call_back ~=nil then
  918. call_back({})
  919. end
  920. end
  921. -- return false,nil
  922. end
  923. end,fd)
  924. return true
  925. end
  926. --根据所有生成视频文件id查到所有文件的url并删除
  927. local fork_get_gen_video_file_list_url_and_delete = function(delete_list,call_back)
  928. local url_list = {}
  929. for i = 1, #delete_list, 1 do
  930. table.insert(url_list,i, delete_list[i].path)
  931. end
  932. skynet.send("tools_work","lua","delete_file",url_list)
  933. if call_back~=nil then
  934. call_back()
  935. end
  936. end
  937. --执行删除生成视频文件操作
  938. local fork_delete_gen_video_file = function(msg_body)
  939. -- tools.dump(msg_body)
  940. local listToDelete = {}
  941. for i = 1, #msg_body.file_list, 1 do
  942. table.insert(listToDelete,i,msg_body.file_list[i].id)
  943. end
  944. local deleteString = table.concat(listToDelete, ',')
  945. local sql = string.format("UPDATE generate_video_list_tab SET is_delete = %d WHERE id IN (%s)",1,deleteString)
  946. db:query(sql)
  947. skynet.fork(fork_get_gen_video_file_list_url_and_delete,msg_body.file_list,function()
  948. local delete_sql = string.format("DELETE FROM generate_video_list_tab WHERE id IN (%s) AND user_id = %d", deleteString,msg_body.user_id)
  949. db:query(delete_sql)
  950. skynet.error(delete_sql)
  951. end)
  952. end
  953. --执行删除生成视频文件夹操作
  954. local fork_delete_gen_video_folder = function(msg)
  955. for i = 1, #msg.folder_id_list, 1 do
  956. local isok = s.resp.get_video_list_by_folder_id({
  957. folder_id = msg.folder_id_list[i],
  958. user_id = msg.user_id},nil,function(list)
  959. skynet.error("执行删除生成视频文件夹操作",#list)
  960. if #list>0 then
  961. local temp = {}
  962. for i = 1, #list, 1 do
  963. table.insert(temp,i,list[i])
  964. end
  965. skynet.fork(fork_delete_gen_video_file,{file_list=temp,user_id=msg.user_id})
  966. end
  967. end)
  968. local delete_sql = string.format("DELETE FROM generate_video_folder_list_tab WHERE id = %d", msg.folder_id_list[i])
  969. db:query(delete_sql)
  970. end
  971. end
  972. --删除生成视频文件夹
  973. s.resp.delete_generate_video_folder = function(msg_body,fd)
  974. skynet.fork(fork_delete_gen_video_folder,msg_body,fd)
  975. return true
  976. end
  977. --删除生成视频
  978. s.resp.delete_generate_video = function(msg_body,fd)
  979. local temp = {}
  980. msg_body.file_list = {}
  981. local listToDelete = {}
  982. for i = 1, #msg_body.file_id_list, 1 do
  983. table.insert(listToDelete,i,msg_body.file_id_list[i])
  984. end
  985. local deleteString = table.concat(listToDelete, ',')
  986. local sql = string.format("select * from generate_video_list_tab WHERE id IN (%s) AND is_delete = 0",deleteString)
  987. local res = db:query(sql)
  988. skynet.error(sql)
  989. if res and #res > 0 then
  990. local tab = {}
  991. if #res == 1 then
  992. tab[1] = tools.getDbResData(res)
  993. else
  994. tab = tools.getDbResData(res)
  995. end
  996. for i = 1, #tab, 1 do
  997. table.insert(msg_body.file_list,i,tab[i])
  998. end
  999. skynet.fork(fork_delete_gen_video_file,msg_body,fd)
  1000. skynet.error("Found data:")
  1001. else
  1002. skynet.error("No data found.")
  1003. end
  1004. return true
  1005. end
  1006. --重命名生成视频文件夹的名字
  1007. s.resp.reset_generate_video_folder_name = function(msg_body,fd)
  1008. local sql = string.format("UPDATE generate_video_list_tab SET video_name ='%s' WHERE id = %d ",msg_body.name,msg_body.folder_id)
  1009. db:query(sql)
  1010. return true
  1011. end
  1012. --生成视频搜索
  1013. s.resp.search_generate_video_file = function(msg_body,fd)
  1014. skynet.fork(function(_fd)
  1015. -- tools.dump(msg_body)
  1016. local sql = string.format("SELECT * FROM generate_video_list_tab WHERE video_name LIKE '%%%s%%' AND is_delete = 0 AND video_stype = %d" ,msg_body.search_content,msg_body.video_stype)
  1017. -- skynet.error("sql",sql)
  1018. local res = db:query(sql)
  1019. if res and #res > 0 then
  1020. local tab = {}
  1021. if #res == 1 then
  1022. tab[1] = tools.getDbResData(res)
  1023. else
  1024. tab = tools.getDbResData(res)
  1025. end
  1026. skynet.error("Found data:")
  1027. -- tools.dump(tab)
  1028. -- return true,tab
  1029. tools.response_db_search_generate_video_file(_fd,msg_body,true,tab)
  1030. else
  1031. skynet.error("No data found.")
  1032. -- return false,nil
  1033. tools.response_db_search_generate_video_file(_fd,msg_body,false,nil)
  1034. end
  1035. end,fd)
  1036. return true
  1037. end
  1038. --获取生成视频文件夹列表每个文件夹的文件数量
  1039. s.resp.folder_gen_video_list_nums = function(folder_list,fd,total_count)
  1040. skynet.fork(function(_fd)
  1041. local temp ={}
  1042. for i = 1, #folder_list, 1 do
  1043. local msg_body = folder_list[i]
  1044. -- tools.dump(msg_body)
  1045. local sql = string.format("select * from generate_video_list_tab where user_id = %d AND folder_id = %d AND is_delete = 0",msg_body.user_id,msg_body.id)
  1046. local res = db:query(sql)
  1047. local count = 0
  1048. if res and #res > 0 then
  1049. count = #res
  1050. skynet.error("Found data:")
  1051. -- tools.dump(tab)
  1052. -- return true,tab
  1053. else
  1054. skynet.error("No data found.",msg_body.id)
  1055. end
  1056. temp[msg_body.id] = count
  1057. -- table.insert(temp,msg_body.id,count)
  1058. end
  1059. tools.response_db_folder_generate_video_list_nums(_fd,folder_list,temp,total_count)
  1060. end,fd)
  1061. end
  1062. --更新生成视频的状态
  1063. s.resp.update_gen_video_file_status = function(msg_body)
  1064. local custom = "{}"
  1065. if msg_body.custom~=nil and msg_body.custom~="" then
  1066. custom = msg_body.custom
  1067. end
  1068. local sql = string.format("UPDATE generate_video_list_tab SET video_url = '%s', path = '%s' , custom = '%s', video_state = %d WHERE id = %d and user_id = %d",msg_body.surl,msg_body.path,custom,msg_body.state,msg_body.id,msg_body.user_id)
  1069. db:query(sql)
  1070. skynet.error(sql)
  1071. return true
  1072. end
  1073. --检测指定类型文件是否够数量
  1074. s.resp.checkNumsByType = function(msg_body)
  1075. local duration = 0
  1076. if msg_body.duration~= nil then
  1077. duration = msg_body.duration
  1078. end
  1079. local limit = msg_body.limit
  1080. local type = msg_body.type
  1081. local sql = string.format("select *from res_list_tab where duration >= %f AND file_type= %d AND user_id = %d LIMIT %d",duration,type,msg_body.user_id,limit)
  1082. -- skynet.error("sql",sql)
  1083. local res = db:query(sql)
  1084. if res and #res > 0 then
  1085. if #res >= tonumber(limit) then
  1086. return true
  1087. end
  1088. return false
  1089. else
  1090. return false
  1091. end
  1092. end
  1093. --修改密码
  1094. s.resp.reset_password = function(msg_body)
  1095. local sql = string.format("UPDATE users SET password = '%s' WHERE id = %d ",msg_body.new_pw,msg_body.user_id)
  1096. db:query(sql)
  1097. return true
  1098. end
  1099. --检测是否有此文件
  1100. s.resp.check_have_file = function(msg_body)
  1101. local sql = string.format("select * from res_list_tab where ref_id = %d AND is_delete = 0 AND user_id = %d LIMIT 1",msg_body.file_id,msg_body.user_id)
  1102. -- skynet.error("sql",sql)
  1103. local res = db:query(sql)
  1104. -- 判断是否找到数据
  1105. if res and #res > 0 then
  1106. local tab = tools.getDbResData(res)
  1107. skynet.error("Found data:",tab)
  1108. return true,tab
  1109. else
  1110. skynet.error("No data found.")
  1111. return false,nil
  1112. end
  1113. end
  1114. --检测是否有此文件夹
  1115. s.resp.check_have_folder = function(msg_body)
  1116. local sql = string.format("select * from folder_list_tab where ref_id = %d AND user_id = %d LIMIT 1",msg_body.folder_id,msg_body.user_id)
  1117. -- skynet.error("sql",sql)
  1118. local res = db:query(sql)
  1119. -- 判断是否找到数据
  1120. if res and #res > 0 then
  1121. local tab = tools.getDbResData(res)
  1122. skynet.error("Found data:",tab)
  1123. return true,tab
  1124. else
  1125. skynet.error("No data found.")
  1126. return false,nil
  1127. end
  1128. end
  1129. --检查模板是否正常
  1130. s.resp.check_template_isRight = function(info)
  1131. local isok = s.resp.checkNumsByType({
  1132. duration = 0,
  1133. type = 1,
  1134. limit = 1,
  1135. user_id = info.user_id})
  1136. if not isok then
  1137. return false,cjson.encode({code=10001,msg = "新建模板失败,片头资源不足!"})
  1138. end
  1139. isok = s.resp.checkNumsByType({
  1140. duration = 0,
  1141. type = 3,
  1142. limit = 1,
  1143. user_id = info.user_id}
  1144. )
  1145. if not isok then
  1146. return false,cjson.encode({code=10001,msg = "新建模板失败,片尾资源不足"})
  1147. end
  1148. local subtitles_audio_duration = 0
  1149. if info.template_info ~=nil and info.template_info~="" then
  1150. local template_info = cjson.decode(info.template_info)
  1151. if template_info.subtitles_audio_duration~=nil then
  1152. subtitles_audio_duration = tonumber(template_info.subtitles_audio_duration)
  1153. end
  1154. end
  1155. local duration = info.video_crop_time + 5
  1156. local limit = math.floor(subtitles_audio_duration/duration)
  1157. skynet.error("limit",limit)
  1158. skynet.error("subtitles_audio_duration",subtitles_audio_duration)
  1159. skynet.error("duration",duration)
  1160. isok = s.resp.checkNumsByType({
  1161. duration = duration,
  1162. type = 2,
  1163. limit =limit+1 ,
  1164. user_id = info.user_id}
  1165. )
  1166. if not isok then
  1167. return false,cjson.encode({code=10001,msg = "正片资源不足"})
  1168. end
  1169. isok = s.resp.checkNumsByType({
  1170. duration = 0,
  1171. type = 4,
  1172. limit = 1,
  1173. user_id = info.user_id}
  1174. )
  1175. if not isok then
  1176. return false,cjson.encode({code=10001,msg = "BGM音效资源不足!"})
  1177. end
  1178. return true,nil
  1179. end
  1180. --接收到删除引用的文件列表信息
  1181. s.resp.recv_ref_file_list_del_info = function(msg_body)
  1182. skynet.error("接收到删除引用的文件列表信息")
  1183. local listToDelete = {}
  1184. for i = 1, #msg_body.file_id_list, 1 do
  1185. table.insert(listToDelete,i,msg_body.file_id_list[i])
  1186. end
  1187. local deleteString = table.concat(listToDelete, ',')
  1188. -- local sql = string.format("UPDATE res_list_tab SET is_delete = %d WHERE ref_id IN (%s)",1,deleteString)
  1189. -- db:query(sql)
  1190. local delete_sql = string.format("DELETE FROM res_list_tab WHERE ref_id IN (%s)", deleteString)
  1191. db:query(delete_sql)
  1192. skynet.error(delete_sql)
  1193. return true
  1194. end
  1195. --检测引用的文件夹是否有自己上传的文件
  1196. s.resp.check_ref_folder_have_self_file = function(msg_body)
  1197. local check_user_folder = function(info) --检测用户的文件夹
  1198. local user_id = info.user_id
  1199. local isHave = false
  1200. local folder_id = info.id
  1201. local sql = string.format("select * from res_list_tab where folder_id = %d AND is_delete = 0 AND user_id = %d ",folder_id,user_id)
  1202. local res = db:query(sql)
  1203. local delete_tab = {}
  1204. -- 判断是否找到数据
  1205. if res and #res > 0 then
  1206. local tab = {}
  1207. if #res == 1 then
  1208. tab[1] = tools.getDbResData(res)
  1209. else
  1210. tab = tools.getDbResData(res)
  1211. end
  1212. local count = 1
  1213. for i = 1, #tab, 1 do
  1214. if tab[i].ref_id==0 then
  1215. isHave = true
  1216. else
  1217. table.insert(delete_tab,count,tab[i])
  1218. count = count+1
  1219. end
  1220. end
  1221. return isHave,delete_tab,info
  1222. else
  1223. return isHave,delete_tab,info
  1224. end
  1225. end
  1226. local sql = ""
  1227. local folder_id = msg_body.folder_id
  1228. --获取当前引用的文件夹
  1229. sql = string.format("select * from folder_list_tab where ref_id = %d ",folder_id)
  1230. local res = db:query(sql)
  1231. local tab_list = {}
  1232. if res and #res > 0 then
  1233. local tab = {}
  1234. if #res == 1 then
  1235. tab[1] = tools.getDbResData(res)
  1236. else
  1237. tab = tools.getDbResData(res)
  1238. end
  1239. for i = 1, #tab, 1 do
  1240. local isHave,delete_tab,info = check_user_folder(tab[i])
  1241. table.insert(tab_list,i,{isHave = isHave,delete_tab=delete_tab,info=info})
  1242. end
  1243. return tab_list
  1244. else
  1245. return tab_list
  1246. end
  1247. end
  1248. --接收到删除引用的文件夹列表信息
  1249. s.resp.recv_ref_folder_list_del_info = function(msg_body)
  1250. local delete_file_list = function(delete_tab)
  1251. local listToDelete = {}
  1252. for i = 1, #delete_tab, 1 do
  1253. table.insert(listToDelete,i,delete_tab[i].ref_id)
  1254. end
  1255. s.resp.recv_ref_file_list_del_info({file_id_list=listToDelete})
  1256. end
  1257. skynet.error("接收到删除引用的文件夹列表信息")
  1258. tools.dump(msg_body)
  1259. for i = 1, #msg_body.folder_id_list, 1 do
  1260. local folder_id = msg_body.folder_id_list[i]
  1261. local list = s.resp.check_ref_folder_have_self_file({folder_id=folder_id})
  1262. for i = 1, #list, 1 do
  1263. tools.dump(list[i])
  1264. delete_file_list(list[i].delete_tab)
  1265. local isHave = list[i].isHave
  1266. local folder_info = list[i].info
  1267. if isHave then --有自定义的文件不删除这个文件夹,但是要将其引用设置为0
  1268. skynet.error("有自定义的文件不删除这个文件夹,但是要将其引用设置为0")
  1269. local sql = string.format("UPDATE folder_list_tab SET ref_id = %d WHERE id = %d AND user_id = %d",0,folder_info.id,folder_info.user_id)
  1270. db:query(sql)
  1271. skynet.error(sql)
  1272. else --没有自己上传的文件夹,将其文件夹删除
  1273. skynet.error("没有自己上传的文件夹,将其文件夹删除")
  1274. local sql = string.format("DELETE FROM folder_list_tab WHERE id = %d AND user_id = %d ",folder_info.id,folder_info.user_id)
  1275. db:query(sql)
  1276. skynet.error(sql)
  1277. end
  1278. end
  1279. end
  1280. return true
  1281. end
  1282. --接收到修改引用的文件信息
  1283. s.resp.recv_ref_file_modify_info = function(msg_body)
  1284. local file_id = msg_body.file_id
  1285. local name = msg_body.name
  1286. local sql = string.format("UPDATE res_list_tab SET folder_name ='%s' WHERE ref_id = %d",name,file_id)
  1287. db:query(sql)
  1288. return true
  1289. end
  1290. --接收到修改引用的文件夹信息
  1291. s.resp.recv_ref_folder_modify_info = function(msg_body)
  1292. local folder_id = msg_body.folder_id
  1293. local name = msg_body.name
  1294. local sql = string.format("UPDATE folder_list_tab SET folder_name ='%s' WHERE ref_id = %d",name,folder_id)
  1295. db:query(sql)
  1296. return true
  1297. end
  1298. s.init = function()
  1299. db=mysql.connect({
  1300. host=runconfig.db_tost,
  1301. port=runconfig.db_port,
  1302. database=runconfig.db_name,
  1303. user="root",
  1304. password=runconfig.db_pw,
  1305. max_packet_size = 1024 * 1024,
  1306. on_connect = nil
  1307. })
  1308. if not db then
  1309. skynet.error("failed to connect")
  1310. skynet.exit()
  1311. else
  1312. skynet.error("success to connect to mysql server")
  1313. end
  1314. --设置utf8字符集
  1315. local res = db:query("set charset utf8");
  1316. end
  1317. s.start(...)