12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058 |
- local skynet = require "skynet"
- local s = require "service"
- local mysql = require "skynet.db.mysql"
- local runconfig = require("run_config")
- local cjson = require "cjson"
- local tools = require "tools"
- local db = nil
- --通知 start
- local response_db_new_folder = function(fd,is_ok)
- if not is_ok then
- return tools.response(fd,200,cjson.encode({code=10001,msg = "新建文件夹失败!"}))
- end
- return tools.response(fd,200,cjson.encode({code=10000,msg = "新建文件夹成功!"}))
- end
- --回应db获取资源文件夹列表
- local response_db_folder_list = function(fd,msg_body,isok,tab)
- if not isok then
- return tools.response(fd,200, '{"code": 10000, "msg": "获取资源文件夹列表失败!", "data": [] }')
- end
- local total_count = 0
- local list = tab
- list,total_count = tools.getPageData(msg_body.page,msg_body.count,list)
- if #list <= 0 or list == nil then
- return tools.response(fd,200,'{"code": 10000, "msg": "获取资源文件夹列表失败!", "data": [] }')
- -- return M.response(fd,200,cjson.encode({code=10000,msg = "获取资源文件夹列表成功!",data=folder_list,total_count=total_count}))
- end
- s.resp.folder_res_list_nums(list,fd,total_count)
- end
- --回应db获取指定文件夹内的资源列表
- local response_db_folder_res_list = function(fd,msg_body,isok,tab)
- if not isok then
- return tools.response(fd,200,'{"code": 10000, "msg": "获取文件夹资源失败", "data": [] }')
- end
- local list = tab
- local folder_list = {}
- local total_count = 0
- -- M.dump(list)
- -- skynet.error("list",list)
- -- skynet.error("msg_body",msg_body)
- -- M.dump(msg_body)
- list,total_count = tools.getPageData(msg_body.page,msg_body.count,list)
- for i = 1, #list, 1 do
- -- skynet.error("create_time")
- -- M.dump(list[i])
- 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})
- end
- if #list<=0 then
- return tools.response(fd,200,'{"code": 10000, "msg": "获取文件夹资源失败", "data": [] }')
- end
- return tools.response(fd,200,cjson.encode({code=10000,msg = "获取文件夹资源成功!",data=folder_list,total_count=total_count}))
- end
- local response_db_search_res = function(fd,msg_body,isok,tab)
- local error_json = '{"code": 10000, "msg": "搜索失败", "data": [] }'
- if not isok then
- return tools.response(fd,200,error_json)
- end
- local list = tab
- local file_list = {}
- local total_count = 0
- -- M.dump(list)
- -- skynet.error("list",list)
- -- skynet.error("msg_body",msg_body)
- -- tools.dump(msg_body)
- list ,total_count = tools.getPageData(msg_body.page,msg_body.count,list)
- for i = 1, #list, 1 do
- 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})
- end
- if #list<=0 then
- return M.response(fd,200,error_json)
- end
- return tools.response(fd,200,cjson.encode({code=10000,msg = "搜索成功",data=file_list,total_count=total_count}))
- end
- local response_db_folder_list_nums = function(fd,list,count_list,total_count)
- local folder_list = {}
- for i = 1, #list, 1 do
- folder_list[i] = {file_nums=count_list[list[i].id],folder_name=list[i].folder_name,folder_id=list[i].id}
- end
- return tools.response(fd,200,cjson.encode({code=10000,msg = "获取资源文件夹列表成功!",data=folder_list,total_count=total_count}))
- end
- local response_db_tools_tools_create_folder = function(fd,isok,tab)
- if not isok then
- return tools.response(fd,200,cjson.encode({code=2,msg = "失败"}))
- end
- return tools.response(fd,200,cjson.encode({code=1,msg = "创建公共文件夹成功!",folder_id=tab[#tab].id}))
- end
- --通知 end
- local checkMsg = function(msg_body,user_data,orgin_body)
- local key = nil
- local user_id = nil
- if type(user_data) ~= "table" then
- return false,100,key,user_id
- end
- user_id = user_data.user_id
- if user_id==nil then
- return false,101,key,user_id
- end
- msg_body["user_id"] = user_id
- local isok ,key = tools.checkData(orgin_body,msg_body)
- if not isok then
- return false,102,key,user_id
- end
- return true,200,key,user_id
- end
- local response_error = function( isOk,err_code,key,fd,func_name)
- if err_code == 100 then
- tools.response(fd, 200, "token error!")
- elseif err_code == 101 then
- tools.response(fd,200,"user_id==nil "..func_name)
- elseif err_code == 102 then
- tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: %s.", key)}))
- end
- end
- --@@@@@@@@@@@@@@@@@@@@@@@@@@数据库 start
- --- 新建文件夹
- s.resp.db_public_new_folder = function(msg_body,fd)
- skynet.fork(function(_fd)
- 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)
- local res = db:query(sql)
- response_db_new_folder(_fd,true)
- end,fd)
- return true
- end
- --获取文件夹列表每个文件夹的文件数量
- s.resp.folder_res_list_nums = function(folder_list,fd,total_count)
- skynet.fork(function(_fd)
- local temp ={}
- for i = 1, #folder_list, 1 do
- local msg_body = folder_list[i]
- -- tools.dump(msg_body)
- 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)
- local res = db:query(sql)
- local count = 0
- if res and #res > 0 then
- count = #res
- skynet.error("Found data:")
- -- tools.dump(tab)
- -- return true,tab
- else
- skynet.error("No data found.",msg_body.id)
- end
- temp[msg_body.id] = count
- -- table.insert(temp,msg_body.id,count)
- end
- response_db_folder_list_nums(_fd,folder_list,temp,total_count)
- end,fd)
- end
- --- 获取资源文件夹列表
- s.resp.db_public_folder_list = function(msg_body,fd)
- skynet.fork(function(_fd)
- 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)
- local res = db:query(sql)
- skynet.error('db_public_folder_list',sql)
- -- 判断是否找到数据
- if res and #res > 0 then
- local tab = {}
- if #res == 1 then
- tab[1] = tools.getDbResData(res)
- else
- tab = tools.getDbResData(res)
- end
-
- skynet.error("Found data:")
- -- tools.dump(tab)
- -- return true,tab
- response_db_folder_list(_fd,msg_body,true,tab)
- else
- skynet.error("No data found.")
- response_db_folder_list(_fd,msg_body,false,nil)
- -- return false,nil
- end
- end,fd)
- return true
- end
- --- 获取文件夹内所有资源
- s.resp.db_public_folder_res_list = function(msg_body,fd,call_back)
- skynet.fork(function(_fd)
- local sql_duration = ""
- -- if msg_body.duration~=nil then
- -- sql_duration = string.format(" AND duration >= %d",msg_body.duration)
- -- end
- 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)
- local res = db:query(sql)
- -- skynet.error('db_public_folder_res_list',sql)
- -- 判断是否找到数据
- if res and #res > 0 then
- local tab = {}
- if #res == 1 then
- tab[1] = tools.getDbResData(res)
- else
- tab = tools.getDbResData(res)
- end
-
- skynet.error("Found data:")
- if not call_back and _fd~=nil then
- response_db_folder_res_list(_fd,msg_body,true,tab)
- else
- if call_back ~=nil then
- call_back(tab)
- end
- end
-
- -- tools.dump(tab)
- -- return true,tab
- else
- skynet.error("No data found.",call_back)
- if call_back == nil and _fd~=nil then
- response_db_folder_res_list(_fd,msg_body,false,nil)
- else
- if call_back ~=nil then
- call_back({})
- end
- end
-
- -- return false,nil
- end
- end,fd)
- return true
- end
- --- 重命名资源
- s.resp.db_public_reset_res_name = function(msg_body,fd)
- local isOk = s.resp.get_res_file_by_id(msg_body.file_id)
- if not isOk then
- return false
- end
- 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)
- db:query(sql)
- return true
- end
- --- 重命名文件夹
- s.resp.db_public_reset_folder_name = function(msg_body,fd)
- local isOk = s.resp.get_res_folder_by_id(msg_body.folder_id)
- if not isOk then
- return false
- end
- local sql = string.format("UPDATE public_folder_list_tab SET folder_name ='%s' WHERE id = %d ",msg_body.name,msg_body.folder_id)
- db:query(sql)
- return true
- end
- --根据所有文件id查到所有文件的url并删除
- local fork_get_file_list_url_and_delete = function(deleteString,call_back)
- local sql = string.format("SELECT file_info FROM public_res_list_tab WHERE id IN (%s)", deleteString)
- local res = db:query(sql)
- if res and #res > 0 then
- local tab = {}
- if #res == 1 then
- tab[1] = tools.getDbResData(res)
- else
- tab = tools.getDbResData(res)
- end
-
- skynet.error("Found data:fork_get_file_list_url_and_delete")
- local url_list = {}
- for i = 1, #tab, 1 do
- local file_info = cjson.decode(tab[i].file_info)
- table.insert(url_list,i,file_info.path)
- end
- skynet.send("tools_work","lua","delete_file",url_list)
- tools.dump(url_list)
- -- return true,tab
- else
- skynet.error("No data found.")
- -- return false,nil
- end
- if call_back~=nil then
- call_back()
- end
-
- end
- --执行删除文件操作
- local fork_delete_file = function(msg_body)
- -- tools.dump(msg_body)
- local listToDelete = {}
- for i = 1, #msg_body.file_id_list, 1 do
- table.insert(listToDelete,i,msg_body.file_id_list[i])
- end
- local deleteString = table.concat(listToDelete, ',')
- local sql = string.format("UPDATE public_res_list_tab SET is_delete = %d WHERE id IN (%s)",1,deleteString)
- db:query(sql)
- skynet.fork(fork_get_file_list_url_and_delete,deleteString,function()
- local delete_sql = string.format("DELETE FROM public_res_list_tab WHERE id IN (%s) AND user_id = %d", deleteString,-1)
- db:query(delete_sql)
- end)
- end
- --执行删除文件夹操作
- local fork_delete_folder = function(msg)
- for i = 1, #msg.folder_id_list, 1 do
- local isok = s.resp.folder_res_list({
- folder_id = msg.folder_id_list[i],
- user_id = -1},nil,nil,function(list)
- skynet.error("执行删除文件夹操作",#list)
- if #list>0 then
- local temp = {}
- for i = 1, #list, 1 do
- table.insert(temp,i,list[i].id)
- end
- skynet.fork(fork_delete_file,{file_id_list=temp,user_id=msg.user_id})
- end
- end)
- end
- end
- --- 文件夹删除
- s.resp.db_public_delete_folder = function(msg_body,fd)
- local listToDelete = {}
- for i = 1, #msg_body.folder_id_list, 1 do
- table.insert(listToDelete,i,msg_body.folder_id_list[i])
- end
- local deleteString = table.concat(listToDelete, ',')
- local sql = string.format("DELETE FROM public_folder_list_tab WHERE id IN (%s) AND user_id = %d", deleteString,-1)
- db:query(sql)
- skynet.fork(fork_delete_folder,msg_body)
- return true
- end
- --- 搜索文件
- s.resp.db_public_search_res = function(msg_body,fd)
- skynet.fork(function(_fd)
- 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)
- skynet.error("sql",sql)
- local res = db:query(sql)
- if res and #res > 0 then
- local tab = {}
- if #res == 1 then
- tab[1] = tools.getDbResData(res)
- else
- tab = tools.getDbResData(res)
- end
-
- skynet.error("Found data:")
- -- tools.dump(tab)
- -- return true,tab
- response_db_search_res(_fd,msg_body,true,tab)
- else
- skynet.error("No data found.")
- -- return false,nil
- response_db_search_res(_fd,msg_body,false,nil)
- end
- end,fd)
- return true
- end
- --- 删除文件
- s.resp.db_public_delete_res = function(msg_body,fd)
- skynet.fork(fork_delete_file,msg_body)
- return true
- end
- s.resp.db_public_check_file_name_list_is_repeat_by_folder_id = function(msg_body,fd,call_back)
- skynet.fork(function()
- local nameString = ""
- for i = 1, #msg_body.file_name_list, 1 do
- nameString = nameString.."'"..msg_body.file_name_list[i].."'"
- if i<#msg_body.file_name_list then
- nameString = nameString..","
- end
- end
- 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)
- local res = db:query(sql)
- local tab = {}
- skynet.error("sql",sql)
- if #res == 1 then
- tab[1] = tools.getDbResData(res)
- else
- tab = tools.getDbResData(res)
- end
- tools.dump(tab)
- local normal_file_name_list ={}
- local repeat_file_name_list ={}
- for i = 1, #tab, 1 do
- table.insert(repeat_file_name_list,i,tab[i].file_name)
- end
- function check(file_name,list)
- for i = 1, #list, 1 do
- if list[i]==file_name then
- return false
- end
- end
- return true
- end
- local count = 1
- for i = 1, #msg_body.file_name_list, 1 do
- if check(msg_body.file_name_list[i],repeat_file_name_list) then
- table.insert(normal_file_name_list,count,msg_body.file_name_list[i])
- count = count+1
- end
- end
- call_back({normal_file_name_list=normal_file_name_list,repeat_file_name_list=repeat_file_name_list})
- end)
- return true
- end
- --根据id获取文件夹
- s.resp.get_res_folder_by_id = function(folder_id)
- local sql = string.format("select * from public_folder_list_tab where id = %d ",folder_id)
- skynet.error("sql",sql)
- local res = db:query(sql)
- -- 判断是否找到数据
- if res and #res > 0 then
- local tab = tools.getDbResData(res)
- skynet.error("Found data:",tab)
- return true,tab
- else
- skynet.error("No data found.")
- return false,nil
- end
- end
- --根据id获取资源
- s.resp.get_res_file_by_id = function(file_id)
- local sql = string.format("select * from public_res_list_tab where id = %d AND is_delete = 0",file_id)
- skynet.error("sql",sql)
- local res = db:query(sql)
- -- 判断是否找到数据
- if res and #res > 0 then
- local tab = tools.getDbResData(res)
- skynet.error("Found data:",tab)
- return true,tab
- else
- skynet.error("No data found.")
- return false,nil
- end
- end
- s.resp.tools_create_folder = function(source,fd,msg_body)
- skynet.fork(function(_fd)
- 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)
- local res = db:query(sql)
- skynet.sleep(10)
- 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)
- res = db:query(sql)
- if res and #res > 0 then
- local tab = {}
- if #res == 1 then
- tab[1] = tools.getDbResData(res)
- else
- tab = tools.getDbResData(res)
- end
- response_db_tools_tools_create_folder(_fd,true,tab)
- else
- response_db_tools_tools_create_folder(_fd,false,nil)
- skynet.error("No data found.")
- end
- end,fd)
- return true
- end
- s.resp.tools_add_public_res_info = function(source,fd,msg_body)
- skynet.fork(function()
- local temp = '{"width": 100, "height": 100, "duration": 123}'
- temp = cjson.encode(msg_body)
- local duration = 0
- if msg_body.duration~=nil then
- duration = tonumber(msg_body.duration)
- end
- 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)
- skynet.error("add_res_file",sql)
- local res = db:query(sql)
- end)
- return true
- end
- s.resp.db_add_public_res_info = function(msg_body,fd)
- skynet.fork(function()
- local temp = '{"width": 100, "height": 100, "duration": 123}'
- temp = cjson.encode(msg_body)
- local duration = 0
- if msg_body.duration~=nil then
- duration = tonumber(msg_body.duration)
- end
- 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)
- -- skynet.error("add_res_file",sql)
- local res = db:query(sql)
- end)
- return true
- end
- local get_res_list_for_folder = function(user_id,folder_id,call_back)
- 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)
- local res = db:query(sql)
- skynet.error('folder_res_list',sql)
- -- 判断是否找到数据
- if res and #res > 0 then
- local tab = {}
- if #res == 1 then
- tab[1] = tools.getDbResData(res)
- else
- tab = tools.getDbResData(res)
- end
-
- skynet.error("Found data:")
- if call_back ~=nil then
- call_back(tab)
- end
- else
- if call_back ~=nil then
- call_back(nil)
- end
-
- end
- end
- s.resp.db_public_add_folder_list_to_private = function(msg_body,fd,call_back)
- local count = 0
- local check_all_finish = true
- for i = 1, #msg_body.folder_id_list, 1 do
- local public_folder_id = msg_body.folder_id_list[i]
- local isOk = s.resp.db_public_add_folder_to_private({
- public_folder_id = public_folder_id,
- user_id = msg_body.user_id},fd,function(isFinish)
- count = count+1;
- if isFinish == false then
- check_all_finish = false
- end
- if count>=#msg_body.folder_id_list then
- call_back(check_all_finish)
- end
- end)
- end
- end
- s.resp.db_public_add_folder_to_private = function(msg_body,fd,call_back)
- skynet.fork(function()
- --获取公共文件夹
- local isOk,public_folder_info = s.resp.get_res_folder_by_id(msg_body.public_folder_id)
- if not isOk then
- call_back(false)
- end
- --检测用户是否有此公共文件夹
- local self_folder_info = nil
- isOk,self_folder_info = skynet.call("dbmgr","lua","on_recv","check_have_folder",{
- folder_id = public_folder_info.id,
- user_id = msg_body.user_id}
- )
- if not isOk then
- --如果没有创建用户私人文件夹
- 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)
- local res = db:query(sql)
- --获取刚刚创建的私人文件夹
- 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)
- res = db:query(sql)
- skynet.error(sql)
- if res and #res > 0 then
- local tab = {}
- tab = tools.getDbResData(res)
- local user_id = tab.user_id
- local private_folder_id = tab.id
- local public_folder_id = msg_body.public_folder_id
- skynet.error("刚刚创建的文件夹",private_folder_id)
- --获取公共文件夹下的所有文件
- skynet.fork(get_res_list_for_folder,user_id,public_folder_id,function(list)
- --将所有文件也拷贝到私人
- skynet.error("获取公共文件夹下的所有文件")
- -- tools.dump(list)
- if list ~=nil then
- for i = 1, #list, 1 do
- local temp = {}
- temp = list[i].file_info
- 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)
- db:query(sql)
- end
- end
- call_back(true)
- end)
- else
- call_back(false)
- skynet.error("No data found.")
- end
- else --如果用户已存此文件夹在
- skynet.fork(get_res_list_for_folder,msg_body.user_id, public_folder_info.id,function(list)
- --将所有文件也拷贝到私人
- -- skynet.error("将所有文件也拷贝到私人")
- -- tools.dump(list)
- if list ~=nil then
- for i = 1, #list, 1 do
- local file_id = list[i].id
- local self_file_info = nil
- isOk,self_file_info = skynet.call("dbmgr","lua","on_recv","check_have_file",{
- file_id = file_id,
- folder_id = public_folder_info.id,
- user_id = msg_body.user_id}
- )
- if not isOk then --如果用户没有此文件
- local temp = {}
- temp = list[i].file_info
- 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)
- db:query(sql)
- end
- end
- end
- call_back(true)
- end)
-
- end
-
- end)
- return true
- end
- s.resp.db_public_add_file_list_to_private = function(msg_body,fd,call_back)
- local count = 0
- local check_all_finish = true
- for i = 1, #msg_body.file_id_list, 1 do
- local public_file_id = msg_body.file_id_list[i]
- local isOk = s.resp.db_public_add_file_to_private({
- file_id = public_file_id,
- user_id = msg_body.user_id},fd,function(isFinish)
- count = count+1;
- if isFinish == false then
- check_all_finish = false
- end
- skynet.error("count",count)
- skynet.error("file_id_list",#msg_body.file_id_list)
- if count>=#msg_body.file_id_list then
- call_back(check_all_finish)
- end
- end)
- end
- return true
- end
- s.resp.db_public_add_file_to_private = function(msg_body,fd,call_back)
- local isOk,public_file_info = s.resp.get_res_file_by_id(msg_body.file_id)
- if not isOk then
- return false
- end
- local self_file_info = nil
- local file_id = public_file_info.id
- isOk,self_file_info = skynet.call("dbmgr","lua","on_recv","check_have_file",{
- file_id = file_id,
- user_id = msg_body.user_id}
- )
- --检测是否有此文件夹
- --如果没有创建用户私人文件夹
- -- 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)
- -- local res = db:query(sql)
- -- --获取刚刚创建的私人文件夹
- -- 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)
- -- res = db:query(sql)
- if not isOk then
- --检测用户是否有此公共文件夹
- local self_folder_info = nil
- isOk,self_folder_info = skynet.call("dbmgr","lua","on_recv","check_have_folder",{
- folder_id = public_file_info.folder_id,
- user_id = msg_body.user_id}
- )
- if not isOk then
- local isOk,public_folder_info = s.resp.get_res_folder_by_id(public_file_info.folder_id)
- if not isOk then
- call_back(false)
- end
- --如果没有创建用户私人文件夹
- 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)
- local res = db:query(sql)
- --获取刚刚创建的私人文件夹
- 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)
- res = db:query(sql)
- if res and #res > 0 then
- local tab = {}
- tab = tools.getDbResData(res)
- local private_folder_id = tab.id
- skynet.error("添加一个文件到用户")
- local temp = {}
- temp = public_file_info.file_info
- 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)
- skynet.error(sql)
- db:query(sql)
- end
-
- else
- local private_folder_id = self_folder_info.id
- local temp = {}
- temp = public_file_info.file_info
- 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)
- skynet.error("用户已有此文件夹,直接添加")
- db:query(sql)
- end
- end
- call_back(true)
- return true
- end
- --@@@@@@@@@@@@@@@@@@@@@@@@@@数据库 end
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- --==========================逻辑 start
- --- 搜索文件
- s.resp.search_res = function(msg_body,fd,user_data)
- local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
- {"search_content",
- "type",
- "page",
- "count",
- "classification_id"})
- if isOk then
- s.resp.db_public_search_res({
- type = msg_body.type,
- search_content = msg_body.search_content,
- page = msg_body.page,
- count = msg_body.count,
- classification_id = msg_body.classification_id,
- user_id = user_id},fd)
- else
- response_error(isOk,err_code,key,fd,"search_res")
- end
- end
- --- 文件夹删除
- s.resp.delete_folder = function(msg_body,fd,user_data)
- local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
- {"folder_id_list"})
- skynet.error("公共文件夹删除")
- if isOk then
- isOk = s.resp.db_public_delete_folder({
- folder_id_list = msg_body.folder_id_list,
- user_id = user_id},fd)
- if not isOk then
- return tools.response(fd,200,cjson.encode({code=10000,msg = "删除失败!",data={}}))
- end
- isOk = skynet.call("dbmgr","lua","on_recv","recv_ref_folder_list_del_info",{folder_id_list= msg_body.folder_id_list})
- return tools.response(fd,200,cjson.encode({code=10000,msg = "删除成功!",data={}}))
- else
- skynet.error("err_code:",err_code)
- response_error(isOk,err_code,key,fd,"delete_folder")
- end
- end
- --- 删除文件
- s.resp.delete_res = function(msg_body,fd,user_data)
- local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
- {"file_id_list"})
- if isOk then
- isOk = s.resp.db_public_delete_res({
- file_id_list = msg_body.file_id_list,
- user_id = user_id},fd)
- if not isOk then
- return tools.response(fd,200,cjson.encode({code=10000,msg = "删除失败!",data={}}))
- end
- isOk = skynet.call("dbmgr","lua","on_recv","recv_ref_file_list_del_info",{file_id_list= msg_body.file_id_list})
- return tools.response(fd,200,cjson.encode({code=10000,msg = "删除成功!",data={}}))
-
- else
- response_error(isOk,err_code,key,fd,"delete_res")
- end
- end
- --- 新建文件夹
- s.resp.new_folder = function(msg_body,fd,user_data)
- local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
- {"category_name","type","classification_id"})
- if isOk then
- isOk = s.resp.db_public_new_folder({
- user_id=user_id,
- folder_name=msg_body.category_name,
- folder_type=msg_body.type,
- classification_id=msg_body.classification_id},fd)
- return
- else
- response_error(isOk,err_code,key,fd,"new_folder")
- end
- end
- --- 获取资源文件夹列表
- s.resp.folder_list = function(msg_body,fd,user_data)
- local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
- {"type","page","count","classification_id"})
- if isOk then
- isOk = s.resp.db_public_folder_list({user_id = user_id,
- type=msg_body.type,
- page=msg_body.page,
- count = msg_body.count,
- classification_id = msg_body.classification_id },fd)
- return
- else
- response_error(isOk,err_code,key,fd,"folder_list")
- end
- end
- --- 获取文件夹内所有资源
- s.resp.folder_res_list = function(msg_body,fd,user_data,call_back)
- if call_back~=nil then
- local duration = nil
- if msg_body.duration ~=nil then
- duration = msg_body.duration
- end
- s.resp.db_public_folder_res_list({
- folder_id = msg_body.folder_id,
- user_id = msg_body.user_id,
- page=msg_body.page,
- count = msg_body.count,
- classification_id = msg_body.classification_id,
- duration = duration},fd,call_back)
- return
- end
- local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
- {"folder_id","page","count"})
- if isOk then
- local duration = nil
- if msg_body.duration ~=nil then
- duration = msg_body.duration
- end
- isOk = s.resp.db_public_folder_res_list({
- folder_id = msg_body.folder_id,
- user_id = user_id,
- page=msg_body.page,
- count = msg_body.count,
- classification_id = msg_body.classification_id,
- duration = duration},fd)
- return
- else
- response_error(isOk,err_code,key,fd,"folder_res_list")
- end
- end
- --- 重命名资源
- s.resp.reset_res_name = function(msg_body,fd,user_data)
- local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
- {"file_id","name","file_id"})
- if isOk then
- isOk = s.resp.db_public_reset_res_name({
- folder_id = msg_body.folder_id,
- file_id = msg_body.file_id,
- name = msg_body.name,
- user_id = user_id},fd)
- if not isOk then
- return tools.response(fd,200,cjson.encode({code=10001,msg = "--重命名资源失败"}))
- end
- isOk = skynet.call("dbmgr","lua","on_recv","recv_ref_file_modify_info",{file_id= msg_body.file_id,name=msg_body.name})
- return tools.response(fd,200,cjson.encode({code=10000,msg = "重命名资源成功"}))
- else
- response_error(isOk,err_code,key,fd,"reset_res_name")
- end
- end
- --- 重命名文件夹
- s.resp.reset_folder_name = function(msg_body,fd,user_data)
- local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
- {"folder_id","name"})
- if isOk then
- isOk = s.resp.db_public_reset_folder_name({
- folder_id = msg_body.folder_id,
- name = msg_body.name,
- user_id = user_id},fd)
- if not isOk then
- return tools.response(fd,200,cjson.encode({code=10001,msg = "--重命名文件夹失败"}))
- end
- isOk = skynet.call("dbmgr","lua","on_recv","recv_ref_folder_modify_info",{folder_id= msg_body.folder_id,name=msg_body.name})
- return tools.response(fd,200,cjson.encode({code=10000,msg = "重命名文件夹成功"}))
- else
- response_error(isOk,err_code,key,fd,"reset_folder_name")
- end
- end
- s.resp.add_public_res_info = function(msg_body,fd,user_data)
- local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
- {"classification_id","stype","folder_id", "height", "width","duration", "user_id", "ratio", "category_name","file_name","size","surl","path"})
- if isOk then
- isOk = s.resp.db_add_public_res_info(msg_body,fd)
- if not isOk then
- return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加文件失败"}))
- end
- return tools.response(fd,200,cjson.encode({code=10000,msg = "添加文件成功"}))
- else
- response_error(isOk,err_code,key,fd,"add_public_res_info")
- end
- end
- --添加公共文件列表到个人
- s.resp.add_file_list_to_private = function(msg_body,fd,user_data)
- local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
- {"file_id_list"})
- if isOk then
- isOk = s.resp.db_public_add_file_list_to_private({
- file_id_list = msg_body.file_id_list,
- user_id = user_id},fd,function(isFinish)
- if isFinish == true then
- return tools.response(fd,200,cjson.encode({code=10000,msg = "--添加公共文件到个人成功!"}))
- else
- return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加公共文件到个人失败!"}))
- end
- end)
- return
- else
- response_error(isOk,err_code,key,fd,"add_file_to_private")
- end
- end
- --添加公共文件到个人
- s.resp.add_file_to_private = function(msg_body,fd,user_data)
- local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
- {"file_id"})
- if isOk then
- isOk = s.resp.db_public_add_file_to_private({
- file_id = msg_body.file_id,
- user_id = user_id},fd)
- if isOk == true then
- return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加公共文件到个人失败!"}))
- else
- return tools.response(fd,200,cjson.encode({code=10000,msg = "--添加公共文件到个人成功!"}))
- end
- else
- response_error(isOk,err_code,key,fd,"add_file_to_private")
- end
- end
- --添加公共文件夹列表到个人
- s.resp.add_folder_list_to_private = function(msg_body,fd,user_data)
- local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
- {"folder_id_list"})
- if isOk then
- isOk = s.resp.db_public_add_folder_list_to_private({
- folder_id_list = msg_body.folder_id_list,
- user_id = user_id},fd,function(isFinish)
- if isFinish then
- return tools.response(fd,200,cjson.encode({code=10000,msg = "--添加公共文件夹到个人成功!"}))
- else
- return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加公共文件夹到个人失败!"}))
- end
- end)
- return
- else
- response_error(isOk,err_code,key,fd,"add_folder_to_private")
- end
- end
- --添加公共文件夹到个人
- s.resp.add_folder_to_private = function(msg_body,fd,user_data)
- local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
- {"folder_id"})
- if isOk then
- isOk = s.resp.db_public_add_folder_to_private({
- public_folder_id = msg_body.folder_id,
- user_id = user_id},fd,function(isFinish)
- if isFinish == true then
- return tools.response(fd,200,cjson.encode({code=10000,msg = "--添加公共文件夹到个人成功!"}))
- else
- return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加公共文件夹到个人失败!"}))
- end
- end)
- return
- else
- response_error(isOk,err_code,key,fd,"add_folder_to_private")
- end
- end
- s.resp.check_public_file_name_list_is_repeat_by_folder_id = function(msg_body,fd,user_data)
- local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
- {"folder_id","file_name_list"})
- if isOk then
- isOk = s.resp.db_public_check_file_name_list_is_repeat_by_folder_id({
- public_folder_id = msg_body.folder_id,
- file_name_list = msg_body.file_name_list,
- user_id = user_id},fd,function(tab)
- tools.response(fd,200,cjson.encode({code=10000,msg = "--检测成功!",data=tab}))
- end)
- return
- else
- response_error(isOk,err_code,key,fd,"check_public_file_name_list_is_repeat_by_folder_id")
- end
- end
- --==========================逻辑 end
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- -----------------------------------------
- s.resp.on_recv = function (source, fd, msg_id, msg_body,user_data)
- skynet.error("接收一条客户端public消息 ",msg_id,user_data)
- tools.dump(msg_body)
- local func = string.gsub(msg_id, '/public/', '')
- local isKick = skynet.call("agentmgr","lua","account_is_other_user_login",user_data.user_id,user_data.index)
- if not isKick then
-
- else
- skynet.error("用户被挤掉",user_data.user_id)
- return tools.response(fd,200,cjson.encode({code=9999,msg = "您的账号被其他用户登录!"}))
- end
- if s.resp[func] ~=nil then
- msg_body = cjson.decode(msg_body)
- return s.resp[func](msg_body,fd,user_data)
- end
- return tools.response(fd,200,string.format("接口 %s 不存在",func))
- end
- s.init = function()
- db=mysql.connect({
- host=runconfig.db_tost,
- port=runconfig.db_port,
- database=runconfig.db_name,
- user="root",
- password=runconfig.db_pw,
- max_packet_size = 1024 * 1024,
- on_connect = nil
- })
- if not db then
- skynet.error("failed to connect")
- skynet.exit()
- else
- skynet.error(" config success to connect to mysql server")
- skynet.error(" 初始化公共服务成功!")
- end
- end
- s.start(...)
|