123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071 |
- 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()
- if runconfig.is_debug then
- db=mysql.connect({
- host=runconfig.db_debug_config.db_tost,
- port=runconfig.db_debug_config.db_port,
- database=runconfig.db_debug_config.db_name,
- user=runconfig.db_debug_config.db_user,
- password=runconfig.db_debug_config.db_pw,
- max_packet_size = 1024 * 1024,
- on_connect = nil
- })
- else
- db=mysql.connect({
- host=runconfig.db_release_config.db_tost,
- port=runconfig.db_release_config.db_port,
- database=runconfig.db_release_config.db_name,
- user=runconfig.db_release_config.db_user,
- password=runconfig.db_release_config.db_pw,
- max_packet_size = 1024 * 1024,
- on_connect = nil
- })
- end
- 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(...)
|