init.lua 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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 cjson = require "cjson"
  6. local tools = require "tools"
  7. local db = nil
  8. --通知 start
  9. local response_db_new_folder = function(fd,is_ok)
  10. if not is_ok then
  11. return tools.response(fd,200,cjson.encode({code=10001,msg = "新建文件夹失败!"}))
  12. end
  13. return tools.response(fd,200,cjson.encode({code=10000,msg = "新建文件夹成功!"}))
  14. end
  15. --回应db获取资源文件夹列表
  16. local response_db_folder_list = function(fd,msg_body,isok,tab)
  17. if not isok then
  18. return tools.response(fd,200, '{"code": 10000, "msg": "获取资源文件夹列表失败!", "data": [] }')
  19. end
  20. local total_count = 0
  21. local list = tab
  22. list,total_count = tools.getPageData(msg_body.page,msg_body.count,list)
  23. if #list <= 0 or list == nil then
  24. return tools.response(fd,200,'{"code": 10000, "msg": "获取资源文件夹列表失败!", "data": [] }')
  25. -- return M.response(fd,200,cjson.encode({code=10000,msg = "获取资源文件夹列表成功!",data=folder_list,total_count=total_count}))
  26. end
  27. s.resp.folder_res_list_nums(list,fd,total_count)
  28. end
  29. --回应db获取指定文件夹内的资源列表
  30. local response_db_folder_res_list = function(fd,msg_body,isok,tab)
  31. if not isok then
  32. return tools.response(fd,200,'{"code": 10000, "msg": "获取文件夹资源失败", "data": [] }')
  33. end
  34. local list = tab
  35. local folder_list = {}
  36. local total_count = 0
  37. -- M.dump(list)
  38. -- skynet.error("list",list)
  39. -- skynet.error("msg_body",msg_body)
  40. -- M.dump(msg_body)
  41. list,total_count = tools.getPageData(msg_body.page,msg_body.count,list)
  42. for i = 1, #list, 1 do
  43. -- skynet.error("create_time")
  44. -- M.dump(list[i])
  45. table.insert(folder_list,i,{create_time=list[i].create_time,file_id=list[i].id,info=list[i].file_info,file_name=list[i].file_name})
  46. end
  47. if #list<=0 then
  48. return tools.response(fd,200,'{"code": 10000, "msg": "获取文件夹资源失败", "data": [] }')
  49. end
  50. return tools.response(fd,200,cjson.encode({code=10000,msg = "获取文件夹资源成功!",data=folder_list,total_count=total_count}))
  51. end
  52. local response_db_search_res = function(fd,msg_body,isok,tab)
  53. local error_json = '{"code": 10000, "msg": "搜索失败", "data": [] }'
  54. if not isok then
  55. return tools.response(fd,200,error_json)
  56. end
  57. local list = tab
  58. local file_list = {}
  59. local total_count = 0
  60. -- M.dump(list)
  61. -- skynet.error("list",list)
  62. -- skynet.error("msg_body",msg_body)
  63. -- tools.dump(msg_body)
  64. list ,total_count = tools.getPageData(msg_body.page,msg_body.count,list)
  65. for i = 1, #list, 1 do
  66. table.insert(file_list,i,{create_time=list[i].create_time,file_id=list[i].id,info=list[i].file_info,file_name=list[i].file_name})
  67. end
  68. if #list<=0 then
  69. return M.response(fd,200,error_json)
  70. end
  71. return tools.response(fd,200,cjson.encode({code=10000,msg = "搜索成功",data=file_list,total_count=total_count}))
  72. end
  73. local response_db_folder_list_nums = function(fd,list,count_list,total_count)
  74. local folder_list = {}
  75. for i = 1, #list, 1 do
  76. folder_list[i] = {file_nums=count_list[list[i].id],folder_name=list[i].folder_name,folder_id=list[i].id}
  77. end
  78. return tools.response(fd,200,cjson.encode({code=10000,msg = "获取资源文件夹列表成功!",data=folder_list,total_count=total_count}))
  79. end
  80. local response_db_tools_tools_create_folder = function(fd,isok,tab)
  81. if not isok then
  82. return tools.response(fd,200,cjson.encode({code=2,msg = "失败"}))
  83. end
  84. return tools.response(fd,200,cjson.encode({code=1,msg = "创建公共文件夹成功!",folder_id=tab[#tab].id}))
  85. end
  86. --通知 end
  87. local checkMsg = function(msg_body,user_data,orgin_body)
  88. local key = nil
  89. local user_id = nil
  90. if type(user_data) ~= "table" then
  91. return false,100,key,user_id
  92. end
  93. user_id = user_data.user_id
  94. if user_id==nil then
  95. return false,101,key,user_id
  96. end
  97. msg_body["user_id"] = user_id
  98. local isok ,key = tools.checkData(orgin_body,msg_body)
  99. if not isok then
  100. return false,102,key,user_id
  101. end
  102. return true,200,key,user_id
  103. end
  104. local response_error = function( isOk,err_code,key,fd,func_name)
  105. if err_code == 100 then
  106. tools.response(fd, 200, "token error!")
  107. elseif err_code == 101 then
  108. tools.response(fd,200,"user_id==nil "..func_name)
  109. elseif err_code == 102 then
  110. tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: %s.", key)}))
  111. end
  112. end
  113. --@@@@@@@@@@@@@@@@@@@@@@@@@@数据库 start
  114. --- 新建文件夹
  115. s.resp.db_public_new_folder = function(msg_body,fd)
  116. skynet.fork(function(_fd)
  117. local sql = string.format("INSERT INTO public_folder_list_tab (user_id,folder_name, folder_type,upload_user_id,classification_id) VALUES (%d, '%s',%d,%d,%d)",-1,msg_body.folder_name,msg_body.folder_type,msg_body.user_id,msg_body.classification_id)
  118. local res = db:query(sql)
  119. response_db_new_folder(_fd,true)
  120. end,fd)
  121. return true
  122. end
  123. --获取文件夹列表每个文件夹的文件数量
  124. s.resp.folder_res_list_nums = function(folder_list,fd,total_count)
  125. skynet.fork(function(_fd)
  126. local temp ={}
  127. for i = 1, #folder_list, 1 do
  128. local msg_body = folder_list[i]
  129. -- tools.dump(msg_body)
  130. local sql = string.format("select * from public_res_list_tab where user_id = %d AND folder_id = %d AND is_delete = 0",-1,msg_body.id)
  131. local res = db:query(sql)
  132. local count = 0
  133. if res and #res > 0 then
  134. count = #res
  135. skynet.error("Found data:")
  136. -- tools.dump(tab)
  137. -- return true,tab
  138. else
  139. skynet.error("No data found.",msg_body.id)
  140. end
  141. temp[msg_body.id] = count
  142. -- table.insert(temp,msg_body.id,count)
  143. end
  144. response_db_folder_list_nums(_fd,folder_list,temp,total_count)
  145. end,fd)
  146. end
  147. --- 获取资源文件夹列表
  148. s.resp.db_public_folder_list = function(msg_body,fd)
  149. skynet.fork(function(_fd)
  150. local sql = string.format("select * from public_folder_list_tab where user_id = %d AND folder_type = %d AND classification_id = %d",-1,msg_body.type,msg_body.classification_id)
  151. local res = db:query(sql)
  152. skynet.error('db_public_folder_list',sql)
  153. -- 判断是否找到数据
  154. if res and #res > 0 then
  155. local tab = {}
  156. if #res == 1 then
  157. tab[1] = tools.getDbResData(res)
  158. else
  159. tab = tools.getDbResData(res)
  160. end
  161. skynet.error("Found data:")
  162. -- tools.dump(tab)
  163. -- return true,tab
  164. response_db_folder_list(_fd,msg_body,true,tab)
  165. else
  166. skynet.error("No data found.")
  167. response_db_folder_list(_fd,msg_body,false,nil)
  168. -- return false,nil
  169. end
  170. end,fd)
  171. return true
  172. end
  173. --- 获取文件夹内所有资源
  174. s.resp.db_public_folder_res_list = function(msg_body,fd,call_back)
  175. skynet.fork(function(_fd)
  176. local sql_duration = ""
  177. -- if msg_body.duration~=nil then
  178. -- sql_duration = string.format(" AND duration >= %d",msg_body.duration)
  179. -- end
  180. local sql = string.format("select * from public_res_list_tab where user_id = %d AND folder_id = %d AND is_delete = 0"..sql_duration,-1,msg_body.folder_id)
  181. local res = db:query(sql)
  182. -- skynet.error('db_public_folder_res_list',sql)
  183. -- 判断是否找到数据
  184. if res and #res > 0 then
  185. local tab = {}
  186. if #res == 1 then
  187. tab[1] = tools.getDbResData(res)
  188. else
  189. tab = tools.getDbResData(res)
  190. end
  191. skynet.error("Found data:")
  192. if not call_back and _fd~=nil then
  193. response_db_folder_res_list(_fd,msg_body,true,tab)
  194. else
  195. if call_back ~=nil then
  196. call_back(tab)
  197. end
  198. end
  199. -- tools.dump(tab)
  200. -- return true,tab
  201. else
  202. skynet.error("No data found.",call_back)
  203. if call_back == nil and _fd~=nil then
  204. response_db_folder_res_list(_fd,msg_body,false,nil)
  205. else
  206. if call_back ~=nil then
  207. call_back({})
  208. end
  209. end
  210. -- return false,nil
  211. end
  212. end,fd)
  213. return true
  214. end
  215. --- 重命名资源
  216. s.resp.db_public_reset_res_name = function(msg_body,fd)
  217. local isOk = s.resp.get_res_file_by_id(msg_body.file_id)
  218. if not isOk then
  219. return false
  220. end
  221. local sql = string.format("UPDATE public_res_list_tab SET file_name ='%s' WHERE id = %d AND folder_id = %d AND is_delete = 0",msg_body.name,msg_body.file_id,msg_body.folder_id)
  222. db:query(sql)
  223. return true
  224. end
  225. --- 重命名文件夹
  226. s.resp.db_public_reset_folder_name = function(msg_body,fd)
  227. local isOk = s.resp.get_res_folder_by_id(msg_body.folder_id)
  228. if not isOk then
  229. return false
  230. end
  231. local sql = string.format("UPDATE public_folder_list_tab SET folder_name ='%s' WHERE id = %d ",msg_body.name,msg_body.folder_id)
  232. db:query(sql)
  233. return true
  234. end
  235. --根据所有文件id查到所有文件的url并删除
  236. local fork_get_file_list_url_and_delete = function(deleteString,call_back)
  237. local sql = string.format("SELECT file_info FROM public_res_list_tab WHERE id IN (%s)", deleteString)
  238. local res = db:query(sql)
  239. if res and #res > 0 then
  240. local tab = {}
  241. if #res == 1 then
  242. tab[1] = tools.getDbResData(res)
  243. else
  244. tab = tools.getDbResData(res)
  245. end
  246. skynet.error("Found data:fork_get_file_list_url_and_delete")
  247. local url_list = {}
  248. for i = 1, #tab, 1 do
  249. local file_info = cjson.decode(tab[i].file_info)
  250. table.insert(url_list,i,file_info.path)
  251. end
  252. skynet.send("tools_work","lua","delete_file",url_list)
  253. tools.dump(url_list)
  254. -- return true,tab
  255. else
  256. skynet.error("No data found.")
  257. -- return false,nil
  258. end
  259. if call_back~=nil then
  260. call_back()
  261. end
  262. end
  263. --执行删除文件操作
  264. local fork_delete_file = function(msg_body)
  265. -- tools.dump(msg_body)
  266. local listToDelete = {}
  267. for i = 1, #msg_body.file_id_list, 1 do
  268. table.insert(listToDelete,i,msg_body.file_id_list[i])
  269. end
  270. local deleteString = table.concat(listToDelete, ',')
  271. local sql = string.format("UPDATE public_res_list_tab SET is_delete = %d WHERE id IN (%s)",1,deleteString)
  272. db:query(sql)
  273. skynet.fork(fork_get_file_list_url_and_delete,deleteString,function()
  274. local delete_sql = string.format("DELETE FROM public_res_list_tab WHERE id IN (%s) AND user_id = %d", deleteString,-1)
  275. db:query(delete_sql)
  276. end)
  277. end
  278. --执行删除文件夹操作
  279. local fork_delete_folder = function(msg)
  280. for i = 1, #msg.folder_id_list, 1 do
  281. local isok = s.resp.folder_res_list({
  282. folder_id = msg.folder_id_list[i],
  283. user_id = -1},nil,nil,function(list)
  284. skynet.error("执行删除文件夹操作",#list)
  285. if #list>0 then
  286. local temp = {}
  287. for i = 1, #list, 1 do
  288. table.insert(temp,i,list[i].id)
  289. end
  290. skynet.fork(fork_delete_file,{file_id_list=temp,user_id=msg.user_id})
  291. end
  292. end)
  293. end
  294. end
  295. --- 文件夹删除
  296. s.resp.db_public_delete_folder = function(msg_body,fd)
  297. local listToDelete = {}
  298. for i = 1, #msg_body.folder_id_list, 1 do
  299. table.insert(listToDelete,i,msg_body.folder_id_list[i])
  300. end
  301. local deleteString = table.concat(listToDelete, ',')
  302. local sql = string.format("DELETE FROM public_folder_list_tab WHERE id IN (%s) AND user_id = %d", deleteString,-1)
  303. db:query(sql)
  304. skynet.fork(fork_delete_folder,msg_body)
  305. return true
  306. end
  307. --- 搜索文件
  308. s.resp.db_public_search_res = function(msg_body,fd)
  309. skynet.fork(function(_fd)
  310. local sql = string.format("SELECT * FROM public_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)
  311. skynet.error("sql",sql)
  312. local res = db:query(sql)
  313. if res and #res > 0 then
  314. local tab = {}
  315. if #res == 1 then
  316. tab[1] = tools.getDbResData(res)
  317. else
  318. tab = tools.getDbResData(res)
  319. end
  320. skynet.error("Found data:")
  321. -- tools.dump(tab)
  322. -- return true,tab
  323. response_db_search_res(_fd,msg_body,true,tab)
  324. else
  325. skynet.error("No data found.")
  326. -- return false,nil
  327. response_db_search_res(_fd,msg_body,false,nil)
  328. end
  329. end,fd)
  330. return true
  331. end
  332. --- 删除文件
  333. s.resp.db_public_delete_res = function(msg_body,fd)
  334. skynet.fork(fork_delete_file,msg_body)
  335. return true
  336. end
  337. s.resp.db_public_check_file_name_list_is_repeat_by_folder_id = function(msg_body,fd,call_back)
  338. skynet.fork(function()
  339. local nameString = ""
  340. for i = 1, #msg_body.file_name_list, 1 do
  341. nameString = nameString.."'"..msg_body.file_name_list[i].."'"
  342. if i<#msg_body.file_name_list then
  343. nameString = nameString..","
  344. end
  345. end
  346. local sql = string.format("SELECT file_name FROM public_res_list_tab WHERE file_name IN (%s) AND folder_id = %d ", nameString,msg_body.folder_id)
  347. local res = db:query(sql)
  348. local tab = {}
  349. skynet.error("sql",sql)
  350. if #res == 1 then
  351. tab[1] = tools.getDbResData(res)
  352. else
  353. tab = tools.getDbResData(res)
  354. end
  355. tools.dump(tab)
  356. local normal_file_name_list ={}
  357. local repeat_file_name_list ={}
  358. for i = 1, #tab, 1 do
  359. table.insert(repeat_file_name_list,i,tab[i].file_name)
  360. end
  361. function check(file_name,list)
  362. for i = 1, #list, 1 do
  363. if list[i]==file_name then
  364. return false
  365. end
  366. end
  367. return true
  368. end
  369. local count = 1
  370. for i = 1, #msg_body.file_name_list, 1 do
  371. if check(msg_body.file_name_list[i],repeat_file_name_list) then
  372. table.insert(normal_file_name_list,count,msg_body.file_name_list[i])
  373. count = count+1
  374. end
  375. end
  376. call_back({normal_file_name_list=normal_file_name_list,repeat_file_name_list=repeat_file_name_list})
  377. end)
  378. return true
  379. end
  380. --根据id获取文件夹
  381. s.resp.get_res_folder_by_id = function(folder_id)
  382. local sql = string.format("select * from public_folder_list_tab where id = %d ",folder_id)
  383. skynet.error("sql",sql)
  384. local res = db:query(sql)
  385. -- 判断是否找到数据
  386. if res and #res > 0 then
  387. local tab = tools.getDbResData(res)
  388. skynet.error("Found data:",tab)
  389. return true,tab
  390. else
  391. skynet.error("No data found.")
  392. return false,nil
  393. end
  394. end
  395. --根据id获取资源
  396. s.resp.get_res_file_by_id = function(file_id)
  397. local sql = string.format("select * from public_res_list_tab where id = %d AND is_delete = 0",file_id)
  398. skynet.error("sql",sql)
  399. local res = db:query(sql)
  400. -- 判断是否找到数据
  401. if res and #res > 0 then
  402. local tab = tools.getDbResData(res)
  403. skynet.error("Found data:",tab)
  404. return true,tab
  405. else
  406. skynet.error("No data found.")
  407. return false,nil
  408. end
  409. end
  410. s.resp.tools_create_folder = function(source,fd,msg_body)
  411. skynet.fork(function(_fd)
  412. local sql = string.format("INSERT INTO public_folder_list_tab (user_id,folder_name, folder_type,upload_user_id,classification_id) VALUES (%d, '%s',%d,%d,%d)",-1,msg_body.folder_name,msg_body.stype,msg_body.user_id,msg_body.classification_id)
  413. local res = db:query(sql)
  414. skynet.sleep(10)
  415. sql = string.format("select *from public_folder_list_tab where user_id = %d AND folder_name = '%s' AND folder_type = %d AND upload_user_id = %d AND classification_id = %d ",-1,msg_body.folder_name,msg_body.stype,msg_body.user_id,msg_body.classification_id)
  416. res = db:query(sql)
  417. if res and #res > 0 then
  418. local tab = {}
  419. if #res == 1 then
  420. tab[1] = tools.getDbResData(res)
  421. else
  422. tab = tools.getDbResData(res)
  423. end
  424. response_db_tools_tools_create_folder(_fd,true,tab)
  425. else
  426. response_db_tools_tools_create_folder(_fd,false,nil)
  427. skynet.error("No data found.")
  428. end
  429. end,fd)
  430. return true
  431. end
  432. s.resp.tools_add_public_res_info = function(source,fd,msg_body)
  433. skynet.fork(function()
  434. local temp = '{"width": 100, "height": 100, "duration": 123}'
  435. temp = cjson.encode(msg_body)
  436. local duration = 0
  437. if msg_body.duration~=nil then
  438. duration = tonumber(msg_body.duration)
  439. end
  440. local sql = string.format("INSERT INTO public_res_list_tab (user_id, folder_id, file_type, file_name,file_info,duration,classification_id,upload_user_id) VALUES (%d, %d, %d, '%s','%s',%f,%d,%d)",-1,msg_body.folder_id,msg_body.stype,msg_body.file_name,temp,duration,msg_body.classification_id,msg_body.user_id)
  441. skynet.error("add_res_file",sql)
  442. local res = db:query(sql)
  443. end)
  444. return true
  445. end
  446. s.resp.db_add_public_res_info = function(msg_body,fd)
  447. skynet.fork(function()
  448. local temp = '{"width": 100, "height": 100, "duration": 123}'
  449. temp = cjson.encode(msg_body)
  450. local duration = 0
  451. if msg_body.duration~=nil then
  452. duration = tonumber(msg_body.duration)
  453. end
  454. local sql = string.format("INSERT INTO public_res_list_tab (user_id, folder_id, file_type, file_name,file_info,duration,classification_id,upload_user_id) VALUES (%d, %d, %d, '%s','%s',%f,%d)",-1,msg_body.folder_id,msg_body.stype,msg_body.file_name,temp,duration,msg_body.classification_id,msg_body.user_id)
  455. -- skynet.error("add_res_file",sql)
  456. local res = db:query(sql)
  457. end)
  458. return true
  459. end
  460. local get_res_list_for_folder = function(user_id,folder_id,call_back)
  461. local sql = string.format("select * from public_res_list_tab where user_id = %d AND folder_id = %d AND is_delete = 0",-1,folder_id)
  462. local res = db:query(sql)
  463. skynet.error('folder_res_list',sql)
  464. -- 判断是否找到数据
  465. if res and #res > 0 then
  466. local tab = {}
  467. if #res == 1 then
  468. tab[1] = tools.getDbResData(res)
  469. else
  470. tab = tools.getDbResData(res)
  471. end
  472. skynet.error("Found data:")
  473. if call_back ~=nil then
  474. call_back(tab)
  475. end
  476. else
  477. if call_back ~=nil then
  478. call_back(nil)
  479. end
  480. end
  481. end
  482. s.resp.db_public_add_folder_list_to_private = function(msg_body,fd,call_back)
  483. local count = 0
  484. local check_all_finish = true
  485. for i = 1, #msg_body.folder_id_list, 1 do
  486. local public_folder_id = msg_body.folder_id_list[i]
  487. local isOk = s.resp.db_public_add_folder_to_private({
  488. public_folder_id = public_folder_id,
  489. user_id = msg_body.user_id},fd,function(isFinish)
  490. count = count+1;
  491. if isFinish == false then
  492. check_all_finish = false
  493. end
  494. if count>=#msg_body.folder_id_list then
  495. call_back(check_all_finish)
  496. end
  497. end)
  498. end
  499. end
  500. s.resp.db_public_add_folder_to_private = function(msg_body,fd,call_back)
  501. skynet.fork(function()
  502. --获取公共文件夹
  503. local isOk,public_folder_info = s.resp.get_res_folder_by_id(msg_body.public_folder_id)
  504. if not isOk then
  505. call_back(false)
  506. end
  507. --检测用户是否有此公共文件夹
  508. local self_folder_info = nil
  509. isOk,self_folder_info = skynet.call("dbmgr","lua","on_recv","check_have_folder",{
  510. folder_id = public_folder_info.id,
  511. user_id = msg_body.user_id}
  512. )
  513. if not isOk then
  514. --如果没有创建用户私人文件夹
  515. local sql = string.format("INSERT INTO folder_list_tab (user_id,folder_name, folder_type,classification_id,ref_id) VALUES (%d, '%s',%d,%d,%d)",msg_body.user_id,public_folder_info.folder_name,public_folder_info.folder_type,public_folder_info.classification_id,public_folder_info.id)
  516. local res = db:query(sql)
  517. --获取刚刚创建的私人文件夹
  518. sql = string.format("select *from folder_list_tab where user_id = %d AND folder_name = '%s' AND folder_type = %d AND classification_id = %d limit 1",msg_body.user_id,public_folder_info.folder_name,public_folder_info.folder_type,public_folder_info.classification_id)
  519. res = db:query(sql)
  520. skynet.error(sql)
  521. if res and #res > 0 then
  522. local tab = {}
  523. tab = tools.getDbResData(res)
  524. local user_id = tab.user_id
  525. local private_folder_id = tab.id
  526. local public_folder_id = msg_body.public_folder_id
  527. skynet.error("刚刚创建的文件夹",private_folder_id)
  528. --获取公共文件夹下的所有文件
  529. skynet.fork(get_res_list_for_folder,user_id,public_folder_id,function(list)
  530. --将所有文件也拷贝到私人
  531. skynet.error("获取公共文件夹下的所有文件")
  532. -- tools.dump(list)
  533. if list ~=nil then
  534. for i = 1, #list, 1 do
  535. local temp = {}
  536. temp = list[i].file_info
  537. sql = string.format("INSERT INTO res_list_tab (user_id, folder_id, file_type, file_name, file_info, duration, classification_id, ref_id) VALUES (%d, %d, %d, '%s','%s',%f,%d,%d)",user_id,private_folder_id,list[i].file_type,list[i].file_name,temp,list[i].duration,list[i].classification_id,list[i].id)
  538. db:query(sql)
  539. end
  540. end
  541. call_back(true)
  542. end)
  543. else
  544. call_back(false)
  545. skynet.error("No data found.")
  546. end
  547. else --如果用户已存此文件夹在
  548. skynet.fork(get_res_list_for_folder,msg_body.user_id, public_folder_info.id,function(list)
  549. --将所有文件也拷贝到私人
  550. -- skynet.error("将所有文件也拷贝到私人")
  551. -- tools.dump(list)
  552. if list ~=nil then
  553. for i = 1, #list, 1 do
  554. local file_id = list[i].id
  555. local self_file_info = nil
  556. isOk,self_file_info = skynet.call("dbmgr","lua","on_recv","check_have_file",{
  557. file_id = file_id,
  558. folder_id = public_folder_info.id,
  559. user_id = msg_body.user_id}
  560. )
  561. if not isOk then --如果用户没有此文件
  562. local temp = {}
  563. temp = list[i].file_info
  564. local sql = string.format("INSERT INTO res_list_tab (user_id, folder_id, file_type, file_name, file_info, duration, classification_id, ref_id) VALUES (%d, %d, %d, '%s','%s',%f,%d,%d)",self_folder_info.user_id,self_folder_info.id,list[i].file_type,list[i].file_name,temp,list[i].duration,list[i].classification_id,list[i].id)
  565. db:query(sql)
  566. end
  567. end
  568. end
  569. call_back(true)
  570. end)
  571. end
  572. end)
  573. return true
  574. end
  575. s.resp.db_public_add_file_list_to_private = function(msg_body,fd,call_back)
  576. local count = 0
  577. local check_all_finish = true
  578. for i = 1, #msg_body.file_id_list, 1 do
  579. local public_file_id = msg_body.file_id_list[i]
  580. local isOk = s.resp.db_public_add_file_to_private({
  581. file_id = public_file_id,
  582. user_id = msg_body.user_id},fd,function(isFinish)
  583. count = count+1;
  584. if isFinish == false then
  585. check_all_finish = false
  586. end
  587. skynet.error("count",count)
  588. skynet.error("file_id_list",#msg_body.file_id_list)
  589. if count>=#msg_body.file_id_list then
  590. call_back(check_all_finish)
  591. end
  592. end)
  593. end
  594. return true
  595. end
  596. s.resp.db_public_add_file_to_private = function(msg_body,fd,call_back)
  597. local isOk,public_file_info = s.resp.get_res_file_by_id(msg_body.file_id)
  598. if not isOk then
  599. return false
  600. end
  601. local self_file_info = nil
  602. local file_id = public_file_info.id
  603. isOk,self_file_info = skynet.call("dbmgr","lua","on_recv","check_have_file",{
  604. file_id = file_id,
  605. user_id = msg_body.user_id}
  606. )
  607. --检测是否有此文件夹
  608. --如果没有创建用户私人文件夹
  609. -- local sql = string.format("INSERT INTO folder_list_tab (user_id,folder_name, folder_type,classification_id,ref_id) VALUES (%d, '%s',%d,%d,%d)",msg_body.user_id,public_folder_info.folder_name,public_folder_info.folder_type,public_folder_info.classification_id,public_folder_info.id)
  610. -- local res = db:query(sql)
  611. -- --获取刚刚创建的私人文件夹
  612. -- sql = string.format("select *from folder_list_tab where user_id = %d AND folder_name = '%s' AND folder_type = %d AND classification_id = %d limit 1",msg_body.user_id,public_folder_info.folder_name,public_folder_info.folder_type,public_folder_info.classification_id)
  613. -- res = db:query(sql)
  614. if not isOk then
  615. --检测用户是否有此公共文件夹
  616. local self_folder_info = nil
  617. isOk,self_folder_info = skynet.call("dbmgr","lua","on_recv","check_have_folder",{
  618. folder_id = public_file_info.folder_id,
  619. user_id = msg_body.user_id}
  620. )
  621. if not isOk then
  622. local isOk,public_folder_info = s.resp.get_res_folder_by_id(public_file_info.folder_id)
  623. if not isOk then
  624. call_back(false)
  625. end
  626. --如果没有创建用户私人文件夹
  627. local sql = string.format("INSERT INTO folder_list_tab (user_id,folder_name, folder_type,classification_id,ref_id) VALUES (%d, '%s',%d,%d,%d)",msg_body.user_id,public_folder_info.folder_name,public_folder_info.folder_type,public_folder_info.classification_id,public_folder_info.id)
  628. local res = db:query(sql)
  629. --获取刚刚创建的私人文件夹
  630. sql = string.format("select *from folder_list_tab where user_id = %d AND folder_name = '%s' AND folder_type = %d AND classification_id = %d AND ref_id = %d limit 1",msg_body.user_id,public_folder_info.folder_name,public_folder_info.folder_type,public_folder_info.classification_id,public_folder_info.id)
  631. res = db:query(sql)
  632. if res and #res > 0 then
  633. local tab = {}
  634. tab = tools.getDbResData(res)
  635. local private_folder_id = tab.id
  636. skynet.error("添加一个文件到用户")
  637. local temp = {}
  638. temp = public_file_info.file_info
  639. local sql = string.format("INSERT INTO res_list_tab (user_id, folder_id, file_type, file_name,file_info,duration,classification_id,ref_id) VALUES (%d, %d, %d, '%s','%s',%f,%d,%d)",msg_body.user_id,private_folder_id,public_file_info.file_type,public_file_info.file_name,temp,public_file_info.duration,public_file_info.classification_id,public_file_info.id)
  640. skynet.error(sql)
  641. db:query(sql)
  642. end
  643. else
  644. local private_folder_id = self_folder_info.id
  645. local temp = {}
  646. temp = public_file_info.file_info
  647. local sql = string.format("INSERT INTO res_list_tab (user_id, folder_id, file_type, file_name,file_info,duration,classification_id,ref_id) VALUES (%d, %d, %d, '%s','%s',%f,%d,%d)",msg_body.user_id,private_folder_id,public_file_info.file_type,public_file_info.file_name,temp,public_file_info.duration,public_file_info.classification_id,public_file_info.id)
  648. skynet.error("用户已有此文件夹,直接添加")
  649. db:query(sql)
  650. end
  651. end
  652. call_back(true)
  653. return true
  654. end
  655. --@@@@@@@@@@@@@@@@@@@@@@@@@@数据库 end
  656. -----------------------------------------
  657. -----------------------------------------
  658. -----------------------------------------
  659. -----------------------------------------
  660. -----------------------------------------
  661. -----------------------------------------
  662. -----------------------------------------
  663. -----------------------------------------
  664. -----------------------------------------
  665. -----------------------------------------
  666. -----------------------------------------
  667. --==========================逻辑 start
  668. --- 搜索文件
  669. s.resp.search_res = function(msg_body,fd,user_data)
  670. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  671. {"search_content",
  672. "type",
  673. "page",
  674. "count",
  675. "classification_id"})
  676. if isOk then
  677. s.resp.db_public_search_res({
  678. type = msg_body.type,
  679. search_content = msg_body.search_content,
  680. page = msg_body.page,
  681. count = msg_body.count,
  682. classification_id = msg_body.classification_id,
  683. user_id = user_id},fd)
  684. else
  685. response_error(isOk,err_code,key,fd,"search_res")
  686. end
  687. end
  688. --- 文件夹删除
  689. s.resp.delete_folder = function(msg_body,fd,user_data)
  690. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  691. {"folder_id_list"})
  692. skynet.error("公共文件夹删除")
  693. if isOk then
  694. isOk = s.resp.db_public_delete_folder({
  695. folder_id_list = msg_body.folder_id_list,
  696. user_id = user_id},fd)
  697. if not isOk then
  698. return tools.response(fd,200,cjson.encode({code=10000,msg = "删除失败!",data={}}))
  699. end
  700. isOk = skynet.call("dbmgr","lua","on_recv","recv_ref_folder_list_del_info",{folder_id_list= msg_body.folder_id_list})
  701. return tools.response(fd,200,cjson.encode({code=10000,msg = "删除成功!",data={}}))
  702. else
  703. skynet.error("err_code:",err_code)
  704. response_error(isOk,err_code,key,fd,"delete_folder")
  705. end
  706. end
  707. --- 删除文件
  708. s.resp.delete_res = function(msg_body,fd,user_data)
  709. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  710. {"file_id_list"})
  711. if isOk then
  712. isOk = s.resp.db_public_delete_res({
  713. file_id_list = msg_body.file_id_list,
  714. user_id = user_id},fd)
  715. if not isOk then
  716. return tools.response(fd,200,cjson.encode({code=10000,msg = "删除失败!",data={}}))
  717. end
  718. isOk = skynet.call("dbmgr","lua","on_recv","recv_ref_file_list_del_info",{file_id_list= msg_body.file_id_list})
  719. return tools.response(fd,200,cjson.encode({code=10000,msg = "删除成功!",data={}}))
  720. else
  721. response_error(isOk,err_code,key,fd,"delete_res")
  722. end
  723. end
  724. --- 新建文件夹
  725. s.resp.new_folder = function(msg_body,fd,user_data)
  726. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  727. {"category_name","type","classification_id"})
  728. if isOk then
  729. isOk = s.resp.db_public_new_folder({
  730. user_id=user_id,
  731. folder_name=msg_body.category_name,
  732. folder_type=msg_body.type,
  733. classification_id=msg_body.classification_id},fd)
  734. return
  735. else
  736. response_error(isOk,err_code,key,fd,"new_folder")
  737. end
  738. end
  739. --- 获取资源文件夹列表
  740. s.resp.folder_list = function(msg_body,fd,user_data)
  741. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  742. {"type","page","count","classification_id"})
  743. if isOk then
  744. isOk = s.resp.db_public_folder_list({user_id = user_id,
  745. type=msg_body.type,
  746. page=msg_body.page,
  747. count = msg_body.count,
  748. classification_id = msg_body.classification_id },fd)
  749. return
  750. else
  751. response_error(isOk,err_code,key,fd,"folder_list")
  752. end
  753. end
  754. --- 获取文件夹内所有资源
  755. s.resp.folder_res_list = function(msg_body,fd,user_data,call_back)
  756. if call_back~=nil then
  757. local duration = nil
  758. if msg_body.duration ~=nil then
  759. duration = msg_body.duration
  760. end
  761. s.resp.db_public_folder_res_list({
  762. folder_id = msg_body.folder_id,
  763. user_id = msg_body.user_id,
  764. page=msg_body.page,
  765. count = msg_body.count,
  766. classification_id = msg_body.classification_id,
  767. duration = duration},fd,call_back)
  768. return
  769. end
  770. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  771. {"folder_id","page","count"})
  772. if isOk then
  773. local duration = nil
  774. if msg_body.duration ~=nil then
  775. duration = msg_body.duration
  776. end
  777. isOk = s.resp.db_public_folder_res_list({
  778. folder_id = msg_body.folder_id,
  779. user_id = user_id,
  780. page=msg_body.page,
  781. count = msg_body.count,
  782. classification_id = msg_body.classification_id,
  783. duration = duration},fd)
  784. return
  785. else
  786. response_error(isOk,err_code,key,fd,"folder_res_list")
  787. end
  788. end
  789. --- 重命名资源
  790. s.resp.reset_res_name = function(msg_body,fd,user_data)
  791. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  792. {"file_id","name","file_id"})
  793. if isOk then
  794. isOk = s.resp.db_public_reset_res_name({
  795. folder_id = msg_body.folder_id,
  796. file_id = msg_body.file_id,
  797. name = msg_body.name,
  798. user_id = user_id},fd)
  799. if not isOk then
  800. return tools.response(fd,200,cjson.encode({code=10001,msg = "--重命名资源失败"}))
  801. end
  802. isOk = skynet.call("dbmgr","lua","on_recv","recv_ref_file_modify_info",{file_id= msg_body.file_id,name=msg_body.name})
  803. return tools.response(fd,200,cjson.encode({code=10000,msg = "重命名资源成功"}))
  804. else
  805. response_error(isOk,err_code,key,fd,"reset_res_name")
  806. end
  807. end
  808. --- 重命名文件夹
  809. s.resp.reset_folder_name = function(msg_body,fd,user_data)
  810. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  811. {"folder_id","name"})
  812. if isOk then
  813. isOk = s.resp.db_public_reset_folder_name({
  814. folder_id = msg_body.folder_id,
  815. name = msg_body.name,
  816. user_id = user_id},fd)
  817. if not isOk then
  818. return tools.response(fd,200,cjson.encode({code=10001,msg = "--重命名文件夹失败"}))
  819. end
  820. isOk = skynet.call("dbmgr","lua","on_recv","recv_ref_folder_modify_info",{folder_id= msg_body.folder_id,name=msg_body.name})
  821. return tools.response(fd,200,cjson.encode({code=10000,msg = "重命名文件夹成功"}))
  822. else
  823. response_error(isOk,err_code,key,fd,"reset_folder_name")
  824. end
  825. end
  826. s.resp.add_public_res_info = function(msg_body,fd,user_data)
  827. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  828. {"classification_id","stype","folder_id", "height", "width","duration", "user_id", "ratio", "category_name","file_name","size","surl","path"})
  829. if isOk then
  830. isOk = s.resp.db_add_public_res_info(msg_body,fd)
  831. if not isOk then
  832. return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加文件失败"}))
  833. end
  834. return tools.response(fd,200,cjson.encode({code=10000,msg = "添加文件成功"}))
  835. else
  836. response_error(isOk,err_code,key,fd,"add_public_res_info")
  837. end
  838. end
  839. --添加公共文件列表到个人
  840. s.resp.add_file_list_to_private = function(msg_body,fd,user_data)
  841. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  842. {"file_id_list"})
  843. if isOk then
  844. isOk = s.resp.db_public_add_file_list_to_private({
  845. file_id_list = msg_body.file_id_list,
  846. user_id = user_id},fd,function(isFinish)
  847. if isFinish == true then
  848. return tools.response(fd,200,cjson.encode({code=10000,msg = "--添加公共文件到个人成功!"}))
  849. else
  850. return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加公共文件到个人失败!"}))
  851. end
  852. end)
  853. return
  854. else
  855. response_error(isOk,err_code,key,fd,"add_file_to_private")
  856. end
  857. end
  858. --添加公共文件到个人
  859. s.resp.add_file_to_private = function(msg_body,fd,user_data)
  860. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  861. {"file_id"})
  862. if isOk then
  863. isOk = s.resp.db_public_add_file_to_private({
  864. file_id = msg_body.file_id,
  865. user_id = user_id},fd)
  866. if isOk == true then
  867. return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加公共文件到个人失败!"}))
  868. else
  869. return tools.response(fd,200,cjson.encode({code=10000,msg = "--添加公共文件到个人成功!"}))
  870. end
  871. else
  872. response_error(isOk,err_code,key,fd,"add_file_to_private")
  873. end
  874. end
  875. --添加公共文件夹列表到个人
  876. s.resp.add_folder_list_to_private = function(msg_body,fd,user_data)
  877. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  878. {"folder_id_list"})
  879. if isOk then
  880. isOk = s.resp.db_public_add_folder_list_to_private({
  881. folder_id_list = msg_body.folder_id_list,
  882. user_id = user_id},fd,function(isFinish)
  883. if isFinish then
  884. return tools.response(fd,200,cjson.encode({code=10000,msg = "--添加公共文件夹到个人成功!"}))
  885. else
  886. return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加公共文件夹到个人失败!"}))
  887. end
  888. end)
  889. return
  890. else
  891. response_error(isOk,err_code,key,fd,"add_folder_to_private")
  892. end
  893. end
  894. --添加公共文件夹到个人
  895. s.resp.add_folder_to_private = function(msg_body,fd,user_data)
  896. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  897. {"folder_id"})
  898. if isOk then
  899. isOk = s.resp.db_public_add_folder_to_private({
  900. public_folder_id = msg_body.folder_id,
  901. user_id = user_id},fd,function(isFinish)
  902. if isFinish == true then
  903. return tools.response(fd,200,cjson.encode({code=10000,msg = "--添加公共文件夹到个人成功!"}))
  904. else
  905. return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加公共文件夹到个人失败!"}))
  906. end
  907. end)
  908. return
  909. else
  910. response_error(isOk,err_code,key,fd,"add_folder_to_private")
  911. end
  912. end
  913. s.resp.check_public_file_name_list_is_repeat_by_folder_id = function(msg_body,fd,user_data)
  914. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  915. {"folder_id","file_name_list"})
  916. if isOk then
  917. isOk = s.resp.db_public_check_file_name_list_is_repeat_by_folder_id({
  918. public_folder_id = msg_body.folder_id,
  919. file_name_list = msg_body.file_name_list,
  920. user_id = user_id},fd,function(tab)
  921. tools.response(fd,200,cjson.encode({code=10000,msg = "--检测成功!",data=tab}))
  922. end)
  923. return
  924. else
  925. response_error(isOk,err_code,key,fd,"check_public_file_name_list_is_repeat_by_folder_id")
  926. end
  927. end
  928. --==========================逻辑 end
  929. -----------------------------------------
  930. -----------------------------------------
  931. -----------------------------------------
  932. -----------------------------------------
  933. -----------------------------------------
  934. -----------------------------------------
  935. -----------------------------------------
  936. -----------------------------------------
  937. -----------------------------------------
  938. -----------------------------------------
  939. -----------------------------------------
  940. s.resp.on_recv = function (source, fd, msg_id, msg_body,user_data)
  941. skynet.error("接收一条客户端public消息 ",msg_id,user_data)
  942. tools.dump(msg_body)
  943. local func = string.gsub(msg_id, '/public/', '')
  944. local isKick = skynet.call("agentmgr","lua","account_is_other_user_login",user_data.user_id,user_data.index)
  945. if not isKick then
  946. else
  947. skynet.error("用户被挤掉",user_data.user_id)
  948. return tools.response(fd,200,cjson.encode({code=9999,msg = "您的账号被其他用户登录!"}))
  949. end
  950. if s.resp[func] ~=nil then
  951. msg_body = cjson.decode(msg_body)
  952. return s.resp[func](msg_body,fd,user_data)
  953. end
  954. return tools.response(fd,200,string.format("接口 %s 不存在",func))
  955. end
  956. s.init = function()
  957. db=mysql.connect({
  958. host=runconfig.db_tost,
  959. port=runconfig.db_port,
  960. database=runconfig.db_name,
  961. user="root",
  962. password=runconfig.db_pw,
  963. max_packet_size = 1024 * 1024,
  964. on_connect = nil
  965. })
  966. if not db then
  967. skynet.error("failed to connect")
  968. skynet.exit()
  969. else
  970. skynet.error(" config success to connect to mysql server")
  971. skynet.error(" 初始化公共服务成功!")
  972. end
  973. end
  974. s.start(...)