init.lua 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  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.select_video_file~=nil then
  52. local data = template_info.piantou_video_data.select_video_file
  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.select_video_file~=nil then
  158. local data = template_info.pianwei_video_data.select_video_file
  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.select_video_file~=nil then
  200. local data = template_info.bgm_audio_data.select_video_file
  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_duration = ""
  430. if msg_body.duration~=nil then
  431. sql_duration = string.format(" AND duration >= %f",msg_body.duration)
  432. end
  433. 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"..sql_duration,msg_body.type ,msg_body.search_content,msg_body.classification_id)
  434. -- skynet.error("sql",sql)
  435. local res = db:query(sql)
  436. if res and #res > 0 then
  437. local tab = {}
  438. if #res == 1 then
  439. tab[1] = tools.getDbResData(res)
  440. else
  441. tab = tools.getDbResData(res)
  442. end
  443. skynet.error("Found data:")
  444. -- tools.dump(tab)
  445. -- return true,tab
  446. tools.response_db_search_res(_fd,msg_body,true,tab)
  447. else
  448. skynet.error("No data found.")
  449. -- return false,nil
  450. tools.response_db_search_res(_fd,msg_body,false,nil)
  451. end
  452. end,fd)
  453. return true
  454. end
  455. --根据所有文件id查到所有文件的url并删除
  456. local fork_get_file_list_url_and_delete = function(deleteString,call_back)
  457. local sql = string.format("SELECT * FROM res_list_tab WHERE id IN (%s)", deleteString)
  458. local res = db:query(sql)
  459. if res and #res > 0 then
  460. local tab = {}
  461. if #res == 1 then
  462. tab[1] = tools.getDbResData(res)
  463. else
  464. tab = tools.getDbResData(res)
  465. end
  466. skynet.error("Found data:fork_get_file_list_url_and_delete")
  467. local url_list = {}
  468. local count = 1
  469. for i = 1, #tab, 1 do
  470. if tab[i].ref_id==0 then
  471. local file_info = cjson.decode(tab[i].file_info)
  472. table.insert(url_list,count,file_info.path)
  473. count = count + 1
  474. end
  475. end
  476. skynet.send("tools_work","lua","delete_file",url_list)
  477. -- tools.dump(url_list)
  478. -- return true,tab
  479. else
  480. skynet.error("No data found.")
  481. -- return false,nil
  482. end
  483. if call_back~=nil then
  484. call_back()
  485. end
  486. end
  487. --执行删除文件操作
  488. local fork_delete_file = function(msg_body)
  489. -- tools.dump(msg_body)
  490. local listToDelete = {}
  491. for i = 1, #msg_body.file_id_list, 1 do
  492. table.insert(listToDelete,i,msg_body.file_id_list[i])
  493. end
  494. local deleteString = table.concat(listToDelete, ',')
  495. local sql = string.format("UPDATE res_list_tab SET is_delete = %d WHERE id IN (%s)",1,deleteString)
  496. db:query(sql)
  497. skynet.fork(fork_get_file_list_url_and_delete,deleteString,function()
  498. 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)
  499. db:query(delete_sql)
  500. end)
  501. end
  502. --执行删除文件夹操作
  503. local fork_delete_folder = function(msg)
  504. for i = 1, #msg.folder_id_list, 1 do
  505. local isok = s.resp.folder_res_list({
  506. folder_id = msg.folder_id_list[i],
  507. user_id = msg.user_id,
  508. is_public=msg.is_public},nil,function(list)
  509. -- skynet.error("执行删除文件夹操作",#list)
  510. if #list>0 then
  511. local temp = {}
  512. for i = 1, #list, 1 do
  513. table.insert(temp,i,list[i].id)
  514. end
  515. skynet.fork(fork_delete_file,{file_id_list=temp,user_id=msg.user_id,is_public=msg.is_public})
  516. end
  517. end)
  518. end
  519. end
  520. --删除资源文件
  521. s.resp.delete_res = function(msg_body)
  522. skynet.fork(fork_delete_file,msg_body)
  523. return true
  524. end
  525. --删除文件夹
  526. s.resp.delete_folder =function(msg_body)
  527. local listToDelete = {}
  528. for i = 1, #msg_body.folder_id_list, 1 do
  529. table.insert(listToDelete,i,msg_body.folder_id_list[i])
  530. end
  531. local deleteString = table.concat(listToDelete, ',')
  532. 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)
  533. db:query(sql)
  534. skynet.fork(fork_delete_folder,msg_body)
  535. return true
  536. end
  537. --新建模板
  538. s.resp.create_template = function(msg_body,fd)
  539. local template_info = "{}"
  540. if msg_body.template_info~=nil then
  541. template_info = msg_body.template_info
  542. end
  543. 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)
  544. local res = db:query(sql)
  545. -- skynet.error("sql",sql)
  546. return true
  547. end
  548. --获取模板列表
  549. s.resp.get_template_list = function(msg_body,fd)
  550. skynet.fork(function(_fd)
  551. 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)
  552. skynet.error("sql",sql)
  553. local res = db:query(sql)
  554. -- 判断是否找到数据
  555. if res and #res > 0 then
  556. local tab = {}
  557. if #res == 1 then
  558. tab[1] = tools.getDbResData(res)
  559. else
  560. tab = tools.getDbResData(res)
  561. end
  562. skynet.error("Found data:",tab)
  563. -- return true,tab
  564. tools.response_db_get_template_list(_fd,msg_body,true,tab)
  565. else
  566. skynet.error("No data found.")
  567. -- return false,nil
  568. tools.response_db_get_template_list(_fd,msg_body,false,nil)
  569. end
  570. end,fd)
  571. return true
  572. end
  573. --删除模板
  574. s.resp.delete_template = function(msg_body)
  575. local listToDelete = {}
  576. for i = 1, #msg_body.template_id_list, 1 do
  577. table.insert(listToDelete,i,msg_body.template_id_list[i])
  578. end
  579. local deleteString = table.concat(listToDelete, ',')
  580. local sql = string.format("DELETE FROM template_list_tab WHERE id IN (%s) AND user_id = %d", deleteString,msg_body.user_id)
  581. db:query(sql)
  582. skynet.error(sql)
  583. return true
  584. end
  585. --保存/修改模板
  586. s.resp.save_template = function(msg_body,fd)
  587. local template_info = "{}"
  588. if msg_body.template_info~="" and msg_body.template_info~=nil then
  589. template_info = cjson.decode(msg_body.template_info)
  590. if template_info.creat_video_list~=nil and template_info.creat_video_list~="" then
  591. if #template_info.creat_video_list >0 then
  592. for i = 1, #template_info.creat_video_list, 1 do
  593. if template_info.creat_video_list[i]~=nil then
  594. if template_info.creat_video_list[i].title_html~=nil then
  595. template_info.creat_video_list[i].title_html = tools.base64encode(template_info.creat_video_list[i].title_html)
  596. end
  597. end
  598. end
  599. end
  600. end
  601. template_info = cjson.encode(template_info)
  602. end
  603. 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)
  604. -- skynet.error("sql",sql)
  605. db:query(sql)
  606. return true
  607. end
  608. --搜索模板
  609. s.resp.search_template = function(msg_body,fd)
  610. skynet.fork(function(_fd)
  611. 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)
  612. -- 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)
  613. skynet.error("sql",sql)
  614. local res = db:query(sql)
  615. -- 判断是否找到数据
  616. if res and #res > 0 then
  617. local tab = {}
  618. if #res == 1 then
  619. tab[1] = tools.getDbResData(res)
  620. else
  621. tab = tools.getDbResData(res)
  622. end
  623. skynet.error("Found data:",tab)
  624. -- return true,tab
  625. tools.response_db_search_template(_fd,msg_body,true,tab)
  626. else
  627. skynet.error("No data found.")
  628. -- return false,nil
  629. tools.response_db_search_template(_fd,msg_body,false,nil)
  630. end
  631. end,fd)
  632. return true
  633. end
  634. --根据模板ID获取模板信息
  635. s.resp.get_template_info_by_id = function(msg_body,fd,call_back)
  636. skynet.fork(function(_fd)
  637. local sql = string.format("SELECT * FROM template_list_tab WHERE user_id = %d AND id = %d",msg_body.user_id ,msg_body.template_id)
  638. -- 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)
  639. skynet.error("sql",sql)
  640. local res = db:query(sql)
  641. -- 判断是否找到数据
  642. if res and #res > 0 then
  643. local tab = tools.getDbResData(res)
  644. skynet.error("Found data:",tab)
  645. -- return true,tab
  646. if call_back~=nil and fd ==nil then
  647. call_back(tab)
  648. else
  649. tools.response_db_get_template_info_by_id(_fd,msg_body,true,tab)
  650. end
  651. else
  652. skynet.error("No data found.")
  653. -- return false,nil
  654. if call_back~=nil and fd ==nil then
  655. call_back(nil)
  656. else
  657. tools.response_db_search_template(_fd,msg_body,false,nil)
  658. end
  659. end
  660. end,fd)
  661. return true
  662. end
  663. --获取指定类型所有文件
  664. s.resp.get_file_list_by_type = function(msg_body,fd)
  665. skynet.fork(function(_fd,_msg_body)
  666. local sql_duration = ""
  667. if msg_body.duration~=nil then
  668. sql_duration = string.format(" AND duration >= %f",msg_body.duration)
  669. end
  670. 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)
  671. local res = db:query(sql)
  672. -- skynet.error(sql)
  673. if res and #res > 0 then
  674. local tab = {}
  675. if #res == 1 then
  676. tab[1] = tools.getDbResData(res)
  677. else
  678. tab = tools.getDbResData(res)
  679. end
  680. tools.response_db_get_file_list_by_type(_fd,_msg_body,true,tab)
  681. -- return true,tab
  682. else
  683. skynet.error("No data found.")
  684. -- return false,nil
  685. tools.response_db_get_file_list_by_type(_fd,_msg_body,false,nil)
  686. end
  687. end,fd,msg_body)
  688. end
  689. --随机一个类型,获取相应数量的资源列表
  690. s.resp.get_random_res_by_type = function(user_id,file_type,classification_id,duration)
  691. 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)
  692. local res = db:query(sql)
  693. if res and #res > 0 then
  694. local tab = {}
  695. if #res == 1 then
  696. tab[1] = tools.getDbResData(res)
  697. else
  698. tab = tools.getDbResData(res)
  699. end
  700. -- local test = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  701. -- tools.shuffle(tab)
  702. -- tools.dump(test)
  703. -- tools.dump(tab)
  704. return true,tab
  705. else
  706. skynet.error("No data found.")
  707. return false,nil
  708. end
  709. end
  710. --获取一个类型,指定在文件夹列表里随机
  711. s.resp.get_random_res_by_folder_list = function(file_type,classification_id,folder_list,duration,user_id)
  712. local listToSelect = {}
  713. for i = 1, #folder_list, 1 do
  714. table.insert(listToSelect,i,folder_list[i].folder_id)
  715. end
  716. local selectString = table.concat(listToSelect, ',')
  717. skynet.error("file_type",file_type)
  718. skynet.error("classification_id",classification_id)
  719. skynet.error("folder_list")
  720. tools.dump(folder_list)
  721. skynet.error("user_id",user_id)
  722. 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)
  723. local res = db:query(sql)
  724. if res and #res > 0 then
  725. local tab = {}
  726. if #res == 1 then
  727. tab[1] = tools.getDbResData(res)
  728. else
  729. tab = tools.getDbResData(res)
  730. end
  731. -- tools.dump(tab)
  732. return true,tab
  733. else
  734. skynet.error("No data found.")
  735. return false,nil
  736. end
  737. end
  738. --生成视频数据库
  739. s.resp.generate_video = function(msg_body,fd)
  740. s.resp.get_template_info_by_id(msg_body,nil,function(tab)
  741. -- tools.dump(tab)
  742. if tab ~=nil then
  743. local isOk,error_info = s.resp.check_template_isRight(tab)
  744. if not isOk then
  745. return tools.response(fd,200,error_info)
  746. end
  747. -- tools.dump(tab)
  748. local user_id = msg_body.user_id
  749. local template_id = msg_body.template_id
  750. local file_name = tab.book_name
  751. local video_stype = tab.video_stype
  752. --先查找是否有这个名的文件夹
  753. 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)
  754. local res = db:query(sql)
  755. local info = tools.getDbResData(res)
  756. local write_video_db = function(folder_id)
  757. skynet.error("开始生成",folder_id)
  758. local template_info = cjson.decode(tab.template_info)
  759. local gen_num = #template_info.creat_video_list
  760. local custom = {}
  761. if template_info.custom~=nil and template_info.custom~="" then
  762. custom = template_info.custom
  763. end
  764. -- skynet.error("custom")
  765. -- tools.dump(custom)
  766. for i = 1, gen_num, 1 do -- 模拟数据每次生成5条
  767. local item_info = template_info.creat_video_list[i]
  768. local head_video,content_video,end_video,bgm_audio = s.resp.parse_template_info(tab,item_info)
  769. -- tools.dump(head_video)
  770. -- tools.dump(content_video)
  771. -- tools.dump(end_video)
  772. -- tools.dump(bgm_audio)
  773. local is_corner_mark = item_info.is_corner_mark
  774. local is_video_bottom_text = item_info.is_video_bottom_text
  775. local hot_tag_type = 0
  776. local item_custom = {}
  777. if item_info.custom~=nil and item_info.custom~="" then
  778. item_custom = item_info.custom
  779. end
  780. if is_corner_mark then
  781. hot_tag_type = 1
  782. end
  783. local fictitious_tag_type = 0
  784. if is_video_bottom_text then
  785. fictitious_tag_type = 1
  786. end
  787. local gen_info = {user_id=0,id=0,project_name=tab.project_name,
  788. book_name=tab.book_name,
  789. ratio=tab.ratio,
  790. video_stype=tab.video_stype,
  791. crop_time=tab.video_crop_time,
  792. marketing_title_info={marketing_title_top_h = 0,stroke_h=0,marketing_title=tools.base64decode(item_info.title_html)},
  793. video_list = content_video,
  794. subtitles = {stype=0,subtitle_font="",subtitles_path=template_info.subtitles_path},
  795. subtitles_audio={path = template_info.subtitles_audio_path,duration=template_info.subtitles_audio_duration },
  796. video_last_frame =end_video[1],
  797. video_first_frame = head_video[1],
  798. bgm_list = bgm_audio[1],
  799. hot_tag = {type=hot_tag_type,path=""}, --上边
  800. fictitious_tag = {type=fictitious_tag_type,path=""}, --下边
  801. custom = custom,
  802. video_custom = item_custom,
  803. }
  804. local video_name = gen_info.book_name.."__视频"..i
  805. local book_name = gen_info.book_name
  806. local ratio = gen_info.ratio
  807. local video_stype = gen_info.video_stype
  808. local video_url = ""
  809. local generate_video_info = {}
  810. generate_video_info =item_info
  811. generate_video_info.book_name = tab.book_name
  812. generate_video_info.project_name = tab.project_name
  813. generate_video_info.ratio = tab.ratio
  814. generate_video_info.video_stype = tab.video_stype
  815. generate_video_info = cjson.encode(generate_video_info)
  816. local video_state = 0
  817. 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,
  818. template_id,
  819. folder_id,
  820. video_name,
  821. book_name,
  822. ratio,
  823. video_stype,
  824. video_url,video_state,generate_video_info)
  825. -- skynet.error("123sss")
  826. -- tools.dump(gen_info)
  827. db:query(sql)
  828. -- skynet.error(sql)
  829. 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)
  830. res = db:query(sql)
  831. if res and #res > 0 then
  832. local tab = {}
  833. if #res > 0 then
  834. tab = tools.getDbResData(res)
  835. gen_info.id = tab.id
  836. gen_info.user_id = user_id
  837. sql = string.format("UPDATE generate_video_list_tab SET is_use = %d WHERE id =%d",1,gen_info.id)
  838. db:query(sql)
  839. skynet.send("tools_work","lua","generate_video",gen_info)
  840. end
  841. -- return true,tab
  842. else
  843. skynet.error("No data found.")
  844. end
  845. end
  846. --获取5个未使用的生成视频文件
  847. tools.response_db_generate_video(fd,true)
  848. end
  849. if res and #res > 0 then --存在这个文件夹,直接写入到这个文件夹
  850. local folder_id = info.id
  851. write_video_db(folder_id)
  852. else -- 没有这个文件夹则创建一个
  853. 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)
  854. db:query(sql)
  855. --查找新建的文件夹id
  856. 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)
  857. res = db:query(sql)
  858. info = tools.getDbResData(res)
  859. local folder_id = info.id
  860. write_video_db(folder_id)
  861. end
  862. end
  863. end)
  864. return true
  865. end
  866. --获取生成视频文件夹列表
  867. s.resp.get_generate_video_folder_list = function(msg_body,fd)
  868. skynet.fork(function(_fd,_msg_body)
  869. -- local count = msg_body.count
  870. -- local page = (msg_body.page - 1) * count
  871. 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)
  872. -- local sql = string.format("select * from generate_video_folder_list_tab where user_id = %d AND video_stype = %d limit %d,%d",_msg_body.user_id,_msg_body.video_stype,page,count)
  873. local res = db:query(sql)
  874. -- skynet.error('folder_list',sql)
  875. -- 判断是否找到数据
  876. if res and #res > 0 then
  877. local tab = {}
  878. if #res == 1 then
  879. tab[1] = tools.getDbResData(res)
  880. else
  881. tab = tools.getDbResData(res)
  882. end
  883. skynet.error("Found data:")
  884. -- tools.dump(tab)
  885. -- return true,tab
  886. tools.response_db_generate_video_folder_list(_fd,msg_body,true,tab)
  887. else
  888. skynet.error("No data found.")
  889. tools.response_db_folder_list(_fd,msg_body,false,nil)
  890. -- return false,nil
  891. end
  892. end,fd,msg_body)
  893. return true
  894. end
  895. --获取文件夹内的生成视频
  896. s.resp.get_video_list_by_folder_id = function(msg_body,fd,call_back)
  897. skynet.fork(function(_fd)
  898. 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)
  899. local res = db:query(sql)
  900. -- skynet.error('folder_res_list',sql)
  901. -- 判断是否找到数据
  902. if res and #res > 0 then
  903. local tab = {}
  904. if #res == 1 then
  905. tab[1] = tools.getDbResData(res)
  906. else
  907. tab = tools.getDbResData(res)
  908. end
  909. skynet.error("Found data:")
  910. if not call_back and _fd~=nil then
  911. tools.response_db_get_video_list_by_folder_id(_fd,msg_body,true,tab)
  912. else
  913. if call_back ~=nil then
  914. call_back(tab)
  915. end
  916. end
  917. -- tools.dump(tab)
  918. -- return true,tab
  919. else
  920. skynet.error("No data found.",call_back)
  921. if call_back == nil and _fd~=nil then
  922. tools.response_db_get_video_list_by_folder_id(_fd,msg_body,false,nil)
  923. else
  924. if call_back ~=nil then
  925. call_back({})
  926. end
  927. end
  928. -- return false,nil
  929. end
  930. end,fd)
  931. return true
  932. end
  933. --根据所有生成视频文件id查到所有文件的url并删除
  934. local fork_get_gen_video_file_list_url_and_delete = function(delete_list,call_back)
  935. local url_list = {}
  936. for i = 1, #delete_list, 1 do
  937. table.insert(url_list,i, delete_list[i].path)
  938. end
  939. skynet.send("tools_work","lua","delete_file",url_list)
  940. if call_back~=nil then
  941. call_back()
  942. end
  943. end
  944. --执行删除生成视频文件操作
  945. local fork_delete_gen_video_file = function(msg_body)
  946. -- tools.dump(msg_body)
  947. local listToDelete = {}
  948. for i = 1, #msg_body.file_list, 1 do
  949. table.insert(listToDelete,i,msg_body.file_list[i].id)
  950. end
  951. local deleteString = table.concat(listToDelete, ',')
  952. local sql = string.format("UPDATE generate_video_list_tab SET is_delete = %d WHERE id IN (%s)",1,deleteString)
  953. db:query(sql)
  954. skynet.fork(fork_get_gen_video_file_list_url_and_delete,msg_body.file_list,function()
  955. local delete_sql = string.format("DELETE FROM generate_video_list_tab WHERE id IN (%s) AND user_id = %d", deleteString,msg_body.user_id)
  956. db:query(delete_sql)
  957. skynet.error(delete_sql)
  958. end)
  959. end
  960. --执行删除生成视频文件夹操作
  961. local fork_delete_gen_video_folder = function(msg)
  962. for i = 1, #msg.folder_id_list, 1 do
  963. local isok = s.resp.get_video_list_by_folder_id({
  964. folder_id = msg.folder_id_list[i],
  965. user_id = msg.user_id},nil,function(list)
  966. skynet.error("执行删除生成视频文件夹操作",#list)
  967. if #list>0 then
  968. local temp = {}
  969. for i = 1, #list, 1 do
  970. table.insert(temp,i,list[i])
  971. end
  972. skynet.fork(fork_delete_gen_video_file,{file_list=temp,user_id=msg.user_id})
  973. end
  974. end)
  975. local delete_sql = string.format("DELETE FROM generate_video_folder_list_tab WHERE id = %d", msg.folder_id_list[i])
  976. db:query(delete_sql)
  977. end
  978. end
  979. --删除生成视频文件夹
  980. s.resp.delete_generate_video_folder = function(msg_body,fd)
  981. skynet.fork(fork_delete_gen_video_folder,msg_body,fd)
  982. return true
  983. end
  984. --删除生成视频
  985. s.resp.delete_generate_video = function(msg_body,fd)
  986. local temp = {}
  987. msg_body.file_list = {}
  988. local listToDelete = {}
  989. for i = 1, #msg_body.file_id_list, 1 do
  990. table.insert(listToDelete,i,msg_body.file_id_list[i])
  991. end
  992. local deleteString = table.concat(listToDelete, ',')
  993. local sql = string.format("select * from generate_video_list_tab WHERE id IN (%s) AND is_delete = 0",deleteString)
  994. local res = db:query(sql)
  995. skynet.error(sql)
  996. if res and #res > 0 then
  997. local tab = {}
  998. if #res == 1 then
  999. tab[1] = tools.getDbResData(res)
  1000. else
  1001. tab = tools.getDbResData(res)
  1002. end
  1003. for i = 1, #tab, 1 do
  1004. table.insert(msg_body.file_list,i,tab[i])
  1005. end
  1006. skynet.fork(fork_delete_gen_video_file,msg_body,fd)
  1007. skynet.error("Found data:")
  1008. else
  1009. skynet.error("No data found.")
  1010. end
  1011. return true
  1012. end
  1013. --重命名生成视频文件夹的名字
  1014. s.resp.reset_generate_video_folder_name = function(msg_body,fd)
  1015. local sql = string.format("UPDATE generate_video_list_tab SET video_name ='%s' WHERE id = %d ",msg_body.name,msg_body.folder_id)
  1016. db:query(sql)
  1017. return true
  1018. end
  1019. --生成视频搜索
  1020. s.resp.search_generate_video_file = function(msg_body,fd)
  1021. skynet.fork(function(_fd)
  1022. -- tools.dump(msg_body)
  1023. 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)
  1024. -- skynet.error("sql",sql)
  1025. local res = db:query(sql)
  1026. if res and #res > 0 then
  1027. local tab = {}
  1028. if #res == 1 then
  1029. tab[1] = tools.getDbResData(res)
  1030. else
  1031. tab = tools.getDbResData(res)
  1032. end
  1033. skynet.error("Found data:")
  1034. -- tools.dump(tab)
  1035. -- return true,tab
  1036. tools.response_db_search_generate_video_file(_fd,msg_body,true,tab)
  1037. else
  1038. skynet.error("No data found.")
  1039. -- return false,nil
  1040. tools.response_db_search_generate_video_file(_fd,msg_body,false,nil)
  1041. end
  1042. end,fd)
  1043. return true
  1044. end
  1045. --获取生成视频文件夹列表每个文件夹的文件数量
  1046. s.resp.folder_gen_video_list_nums = function(folder_list,fd,total_count)
  1047. skynet.fork(function(_fd)
  1048. local temp ={}
  1049. for i = 1, #folder_list, 1 do
  1050. local msg_body = folder_list[i]
  1051. -- tools.dump(msg_body)
  1052. 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)
  1053. local res = db:query(sql)
  1054. local count = 0
  1055. if res and #res > 0 then
  1056. count = #res
  1057. skynet.error("Found data:")
  1058. -- tools.dump(tab)
  1059. -- return true,tab
  1060. else
  1061. skynet.error("No data found.",msg_body.id)
  1062. end
  1063. temp[msg_body.id] = count
  1064. -- table.insert(temp,msg_body.id,count)
  1065. end
  1066. tools.response_db_folder_generate_video_list_nums(_fd,folder_list,temp,total_count)
  1067. end,fd)
  1068. end
  1069. --更新生成视频的状态
  1070. s.resp.update_gen_video_file_status = function(msg_body)
  1071. local custom = "{}"
  1072. if msg_body.custom~=nil and msg_body.custom~="" then
  1073. custom = msg_body.custom
  1074. end
  1075. 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)
  1076. db:query(sql)
  1077. skynet.error(sql)
  1078. return true
  1079. end
  1080. --检测指定类型文件是否够数量
  1081. s.resp.checkNumsByType = function(msg_body)
  1082. local duration = 0
  1083. if msg_body.duration~= nil then
  1084. duration = msg_body.duration
  1085. end
  1086. local limit = msg_body.limit
  1087. local type = msg_body.type
  1088. 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)
  1089. -- skynet.error("sql",sql)
  1090. local res = db:query(sql)
  1091. if res and #res > 0 then
  1092. if #res >= tonumber(limit) then
  1093. return true
  1094. end
  1095. return false
  1096. else
  1097. return false
  1098. end
  1099. end
  1100. --修改密码
  1101. s.resp.reset_password = function(msg_body)
  1102. local sql = string.format("UPDATE users SET password = '%s' WHERE id = %d ",msg_body.new_pw,msg_body.user_id)
  1103. db:query(sql)
  1104. return true
  1105. end
  1106. --检测是否有此文件
  1107. s.resp.check_have_file = function(msg_body)
  1108. 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)
  1109. -- skynet.error("sql",sql)
  1110. local res = db:query(sql)
  1111. -- 判断是否找到数据
  1112. if res and #res > 0 then
  1113. local tab = tools.getDbResData(res)
  1114. skynet.error("Found data:",tab)
  1115. return true,tab
  1116. else
  1117. skynet.error("No data found.")
  1118. return false,nil
  1119. end
  1120. end
  1121. --检测是否有此文件夹
  1122. s.resp.check_have_folder = function(msg_body)
  1123. 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)
  1124. -- skynet.error("sql",sql)
  1125. local res = db:query(sql)
  1126. -- 判断是否找到数据
  1127. if res and #res > 0 then
  1128. local tab = tools.getDbResData(res)
  1129. skynet.error("Found data:",tab)
  1130. return true,tab
  1131. else
  1132. skynet.error("No data found.")
  1133. return false,nil
  1134. end
  1135. end
  1136. --检查模板是否正常
  1137. s.resp.check_template_isRight = function(info)
  1138. local isok = s.resp.checkNumsByType({
  1139. duration = 0,
  1140. type = 1,
  1141. limit = 1,
  1142. user_id = info.user_id})
  1143. if not isok then
  1144. return false,cjson.encode({code=10001,msg = "新建模板失败,片头资源不足!"})
  1145. end
  1146. isok = s.resp.checkNumsByType({
  1147. duration = 0,
  1148. type = 3,
  1149. limit = 1,
  1150. user_id = info.user_id}
  1151. )
  1152. if not isok then
  1153. return false,cjson.encode({code=10001,msg = "新建模板失败,片尾资源不足"})
  1154. end
  1155. local subtitles_audio_duration = 0
  1156. if info.template_info ~=nil and info.template_info~="" then
  1157. local template_info = cjson.decode(info.template_info)
  1158. if template_info.subtitles_audio_duration~=nil then
  1159. subtitles_audio_duration = tonumber(template_info.subtitles_audio_duration)
  1160. end
  1161. end
  1162. local duration = info.video_crop_time + 5
  1163. local limit = math.floor(subtitles_audio_duration/duration)
  1164. skynet.error("limit",limit)
  1165. skynet.error("subtitles_audio_duration",subtitles_audio_duration)
  1166. skynet.error("duration",duration)
  1167. isok = s.resp.checkNumsByType({
  1168. duration = duration,
  1169. type = 2,
  1170. limit =limit+1 ,
  1171. user_id = info.user_id}
  1172. )
  1173. if not isok then
  1174. return false,cjson.encode({code=10001,msg = "正片资源不足"})
  1175. end
  1176. isok = s.resp.checkNumsByType({
  1177. duration = 0,
  1178. type = 4,
  1179. limit = 1,
  1180. user_id = info.user_id}
  1181. )
  1182. if not isok then
  1183. return false,cjson.encode({code=10001,msg = "BGM音效资源不足!"})
  1184. end
  1185. return true,nil
  1186. end
  1187. --接收到删除引用的文件列表信息
  1188. s.resp.recv_ref_file_list_del_info = function(msg_body)
  1189. skynet.error("接收到删除引用的文件列表信息")
  1190. local listToDelete = {}
  1191. for i = 1, #msg_body.file_id_list, 1 do
  1192. table.insert(listToDelete,i,msg_body.file_id_list[i])
  1193. end
  1194. local deleteString = table.concat(listToDelete, ',')
  1195. -- local sql = string.format("UPDATE res_list_tab SET is_delete = %d WHERE ref_id IN (%s)",1,deleteString)
  1196. -- db:query(sql)
  1197. local delete_sql = string.format("DELETE FROM res_list_tab WHERE ref_id IN (%s)", deleteString)
  1198. db:query(delete_sql)
  1199. skynet.error(delete_sql)
  1200. return true
  1201. end
  1202. --检测引用的文件夹是否有自己上传的文件
  1203. s.resp.check_ref_folder_have_self_file = function(msg_body)
  1204. local check_user_folder = function(info) --检测用户的文件夹
  1205. local user_id = info.user_id
  1206. local isHave = false
  1207. local folder_id = info.id
  1208. 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)
  1209. local res = db:query(sql)
  1210. local delete_tab = {}
  1211. -- 判断是否找到数据
  1212. if res and #res > 0 then
  1213. local tab = {}
  1214. if #res == 1 then
  1215. tab[1] = tools.getDbResData(res)
  1216. else
  1217. tab = tools.getDbResData(res)
  1218. end
  1219. local count = 1
  1220. for i = 1, #tab, 1 do
  1221. if tab[i].ref_id==0 then
  1222. isHave = true
  1223. else
  1224. table.insert(delete_tab,count,tab[i])
  1225. count = count+1
  1226. end
  1227. end
  1228. return isHave,delete_tab,info
  1229. else
  1230. return isHave,delete_tab,info
  1231. end
  1232. end
  1233. local sql = ""
  1234. local folder_id = msg_body.folder_id
  1235. --获取当前引用的文件夹
  1236. sql = string.format("select * from folder_list_tab where ref_id = %d ",folder_id)
  1237. local res = db:query(sql)
  1238. local tab_list = {}
  1239. if res and #res > 0 then
  1240. local tab = {}
  1241. if #res == 1 then
  1242. tab[1] = tools.getDbResData(res)
  1243. else
  1244. tab = tools.getDbResData(res)
  1245. end
  1246. for i = 1, #tab, 1 do
  1247. local isHave,delete_tab,info = check_user_folder(tab[i])
  1248. table.insert(tab_list,i,{isHave = isHave,delete_tab=delete_tab,info=info})
  1249. end
  1250. return tab_list
  1251. else
  1252. return tab_list
  1253. end
  1254. end
  1255. --接收到删除引用的文件夹列表信息
  1256. s.resp.recv_ref_folder_list_del_info = function(msg_body)
  1257. local delete_file_list = function(delete_tab)
  1258. local listToDelete = {}
  1259. for i = 1, #delete_tab, 1 do
  1260. table.insert(listToDelete,i,delete_tab[i].ref_id)
  1261. end
  1262. s.resp.recv_ref_file_list_del_info({file_id_list=listToDelete})
  1263. end
  1264. -- skynet.error("接收到删除引用的文件夹列表信息")
  1265. -- tools.dump(msg_body)
  1266. for i = 1, #msg_body.folder_id_list, 1 do
  1267. local folder_id = msg_body.folder_id_list[i]
  1268. local list = s.resp.check_ref_folder_have_self_file({folder_id=folder_id})
  1269. for i = 1, #list, 1 do
  1270. tools.dump(list[i])
  1271. delete_file_list(list[i].delete_tab)
  1272. local isHave = list[i].isHave
  1273. local folder_info = list[i].info
  1274. if isHave then --有自定义的文件不删除这个文件夹,但是要将其引用设置为0
  1275. skynet.error("有自定义的文件不删除这个文件夹,但是要将其引用设置为0")
  1276. 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)
  1277. db:query(sql)
  1278. skynet.error(sql)
  1279. else --没有自己上传的文件夹,将其文件夹删除
  1280. skynet.error("没有自己上传的文件夹,将其文件夹删除")
  1281. local sql = string.format("DELETE FROM folder_list_tab WHERE id = %d AND user_id = %d ",folder_info.id,folder_info.user_id)
  1282. db:query(sql)
  1283. skynet.error(sql)
  1284. end
  1285. end
  1286. end
  1287. return true
  1288. end
  1289. --接收到修改引用的文件信息
  1290. s.resp.recv_ref_file_modify_info = function(msg_body)
  1291. local file_id = msg_body.file_id
  1292. local name = msg_body.name
  1293. local sql = string.format("UPDATE res_list_tab SET folder_name ='%s' WHERE ref_id = %d",name,file_id)
  1294. db:query(sql)
  1295. return true
  1296. end
  1297. s.resp.update_generate_video_custom = function(msg_body)
  1298. local list = msg_body.data
  1299. skynet.fork(function()
  1300. for i = 1, #list, 1 do
  1301. local video_id = list[i].video_id
  1302. local custom = "{}"
  1303. if list[i].custom~=nil and list[i].custom~="" then
  1304. custom = list[i].custom
  1305. end
  1306. local sql = string.format("UPDATE generate_video_list_tab SET custom = '%s' WHERE id = %d and user_id = %d",custom,video_id,msg_body.user_id)
  1307. db:query(sql)
  1308. -- skynet.error(sql)
  1309. end
  1310. end)
  1311. -- skynet.error(sql)
  1312. return true
  1313. end
  1314. --接收到修改引用的文件夹信息
  1315. s.resp.recv_ref_folder_modify_info = function(msg_body)
  1316. local folder_id = msg_body.folder_id
  1317. local name = msg_body.name
  1318. local sql = string.format("UPDATE folder_list_tab SET folder_name ='%s' WHERE ref_id = %d",name,folder_id)
  1319. db:query(sql)
  1320. return true
  1321. end
  1322. --获取用户所有的文件数
  1323. s.resp.get_user_folder_list_nums = function(msg_body)
  1324. local sql = string.format("select COUNT(user_id) AS nums from folder_list_tab where user_id = %d ",msg_body.user_id)
  1325. local res = db:query(sql)
  1326. local count = 0
  1327. if res and #res > 0 then
  1328. count = res.nums
  1329. end
  1330. return count
  1331. end
  1332. --获取用户指定类型的数量
  1333. s.resp.get_user_file_count_by_type = function(msg_body)
  1334. local sql = string.format("select COUNT(user_id) AS nums from res_list_tab where user_id = %d AND file_type = %d",msg_body.user_id,msg_body.file_type)
  1335. local res = db:query(sql)
  1336. local count = 0
  1337. if res and #res > 0 then
  1338. count = res.nums
  1339. end
  1340. return count
  1341. end
  1342. s.init = function()
  1343. db=mysql.connect({
  1344. host=runconfig.db_tost,
  1345. port=runconfig.db_port,
  1346. database=runconfig.db_name,
  1347. user="root",
  1348. password=runconfig.db_pw,
  1349. max_packet_size = 1024 * 1024,
  1350. on_connect = nil
  1351. })
  1352. if not db then
  1353. skynet.error("failed to connect")
  1354. skynet.exit()
  1355. else
  1356. skynet.error("success to connect to mysql server")
  1357. end
  1358. --设置utf8字符集
  1359. local res = db:query("set charset utf8");
  1360. end
  1361. s.start(...)