local httpd = require "http.httpd" local sockethelper = require "http.sockethelper" local skynet = require "skynet" local httpd = require "http.httpd" local cjson = require "cjson" local crypt = require "client.crypt" local runConfig = require "run_config" local M = { } M.read_request = function(id) -- limit request body size to 8192 (you can pass nil to unlimit) -- 一般的业务不需要处理大量上行数据,为了防止攻击,做了一个 8K 限制。这个限制可以去掉。 -- local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id), 8192) local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id)) return {code=code,url=url,method=method,header=header,body=body} end M.response = function(id, ...) local ok, err = httpd.write_response(sockethelper.writefunc(id), ...) if not ok then -- if err == sockethelper.socket_error , that means socket closed. skynet.error(string.format("fd = %d, %s", id, err)) end end --检查工具返回的字段是否缺失 M.checkToolsSyncVideo= function(table) local requiredFields = {"classification_id","stype","folder_id", "height", "width","duration", "user_id", "ratio", "category_name","file_name","size","surl","path"} -- Define the required fields here for _, field in pairs(requiredFields) do if not table[field] then return false,field end end return true end --检查工具返回的字段是否缺失 M.checkData= function(origin,table) for _, field in pairs(origin) do if not table[field] then return false,field end end return true end M.dump = function(res, tab) if runConfig.is_debug == nil then return end tab = tab or 0 if(tab == 0) then skynet.error("............dump...........") end if type(res) == "table" then skynet.error(string.rep("\t", tab).."{") for k,v in pairs(res) do if type(v) == "table" then skynet.error(k.."=") M.dump(v, tab + 1) else skynet.error(string.rep("\t", tab), k, "=", v, ",") end end skynet.error(string.rep("\t", tab).."}") else skynet.error(string.rep("\t", tab) , res) end end M.getDbResData = function(res) local tab = {} if #res >1 then local i = 1; for k, v in pairs(res) do -- skynet.error("Row " .. k) local tab_1 = {} for field, value in pairs(v) do tab_1[field] = value -- skynet.error(field .. ": " .. value) end tab[i] = tab_1; i=i+1; end else for k, v in pairs(res) do for field, value in pairs(v) do tab[field] = value end end end return tab end M.tokenEncode = function(user_data) return crypt.base64encode(user_data) end M.tokenDecode = function(base_str) return crypt.base64decode(base_str) end M.base64decode = function(base_str) return crypt.base64decode(base_str) end M.base64encode = function(base_str) return crypt.base64encode(base_str) end M.getPageData = function(page, count,list) local _start = (page-1)*count+1 local _end =page*count -- skynet.error("_start",_start) -- skynet.error("_end",_end) -- skynet.error("list",#list) local _temp_list = {} if _start <1 or _end <1 then return _temp_list end if #list <=0 then return _temp_list end if #list < _start then return _temp_list end -- local total_page = math.floor(#list/count) +1 -- if (#list%count)>0 then -- total_page = total_page+1 -- end if _end > #list then local index = 1 for i = _start, #list do _temp_list[index] = list[i] index= index+1 end else local index = 1 for i = _start, _end do _temp_list[index] = list[i] index= index+1 end end return _temp_list,#list end --回应db新建文件夹 M.response_db_new_folder = function(fd,is_ok,error_info) if not is_ok then if error_info~=nil then return M.response(fd,200,cjson.encode({code=10001,msg = error_info})) end return M.response(fd,200,cjson.encode({code=10001,msg = "新建文件夹失败!"})) end return M.response(fd,200,cjson.encode({code=10000,msg = "新建文件夹成功!"})) end --回应db获取资源文件夹列表 M.response_db_folder_list = function(fd,msg_body,isok,tab) if not isok then return M.response(fd,200, '{"code": 10000, "msg": "获取资源文件夹列表失败!", "data": [] }') end local total_count = 0 local list = tab list,total_count = M.getPageData(msg_body.page,msg_body.count,list) if #list <= 0 or list == nil then return M.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 skynet.call("dbmgr","lua","on_recv","folder_res_list_nums",list,fd,total_count) end M.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 M.response(fd,200,cjson.encode({code=10000,msg = "获取资源文件夹列表成功!",data=folder_list,total_count=total_count})) end --回应db获取指定文件夹内的资源列表 M.response_db_folder_res_list = function(fd,msg_body,isok,tab) if not isok then return M.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 = M.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 M.response(fd,200,'{"code": 10000, "msg": "获取文件夹资源失败", "data": [] }') end return M.response(fd,200,cjson.encode({code=10000,msg = "获取文件夹资源成功!",data=folder_list,total_count=total_count})) end M.response_db_search_res = function(fd,msg_body,isok,tab) local error_json = '{"code": 10000, "msg": "搜索失败", "data": [] }' if not isok then return M.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 = M.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 M.response(fd,200,cjson.encode({code=10000,msg = "搜索成功",data=file_list,total_count=total_count})) end M.response_db_get_file_list_by_type = function(fd,msg_body,isok,tab) local error_json = '{"code": 10000, "msg": "获取指定类型所有文件失败!", "data": [] }' if not isok then return M.response(fd,200,error_json) end local list = tab local file_list = {} local total_count = 0 list ,total_count = M.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 M.response(fd,200,cjson.encode({code=10000,msg = "获取指定类型所有文件成功",data=file_list,total_count=total_count})) end M.response_db_get_template_list = function(fd,msg_body,isok,tab) local error_json = '{"code": 10000, "msg": "获取模板列表失败", "data": [] }' if not isok then return M.response(fd,200,error_json) end local list = tab local template_list = {} local total_count = 0 list ,total_count = M.getPageData(msg_body.page,msg_body.count,list) for i = 1, #list, 1 do table.insert(template_list,i,{create_time=list[i].create_time, template_id=list[i].id, project_name=list[i].project_name, book_name=list[i].book_name, ratio=list[i].ratio, video_stype=list[i].video_stype, subtitles=list[i].subtitles, subtitles_audio=list[i].subtitles_audio, video_nums=list[i].video_nums, video_crop_time=list[i].video_crop_time, info=list[i].template_info}) end if #list<=0 then return M.response(fd,200,error_json) end return M.response(fd,200,cjson.encode({code=10000,msg = "获取模板列表成功",data=template_list,total_count=total_count})) end M.response_db_search_template = function(fd,msg_body,isok,tab) local error_json = '{"code": 10000, "msg": "搜索模板失败", "data": [] }' if not isok then return M.response(fd,200,error_json) end local list = tab local template_list = {} local total_count = 0 list ,total_count = M.getPageData(msg_body.page,msg_body.count,list) for i = 1, #list, 1 do table.insert(template_list,i,{create_time=list[i].create_time, template_id=list[i].id, project_name=list[i].project_name, book_name=list[i].book_name, ratio=list[i].ratio, video_stype=list[i].video_stype, subtitles=list[i].subtitles, subtitles_audio=list[i].subtitles_audio, video_nums=list[i].video_nums, video_crop_time=list[i].video_crop_time, info=list[i].template_info}) end if #list<=0 then return M.response(fd,200,error_json) end return M.response(fd,200,cjson.encode({code=10000,msg = "搜索模板成功",data=template_list,total_count=total_count})) end M.response_db_get_template_info_by_id = function(fd,msg_body,isok,tab) local error_json = '{"code": 10000, "msg": "获取模板信息失败!", "data": [] }' if not isok then return M.response(fd,200,error_json) end local info = {create_time=tab.create_time, template_id=tab.id, project_name=tab.project_name, book_name=tab.book_name, ratio=tab.ratio, video_stype=tab.video_stype, subtitles=tab.subtitles, subtitles_audio=tab.subtitles_audio, video_nums=tab.video_nums, video_crop_time=tab.video_crop_time, info=tab.template_info} return M.response(fd,200,cjson.encode({code=10000,msg = "获取模板信息成功!",data=info})) end M.response_db_generate_video_folder_list = function(fd,msg_body,isok,tab) local err_code = '{"code": 10000, "msg": "获取生成视频文件夹列表失败!", "data": [] }' if not isok then return M.response(fd,200, err_code) end local total_count = 0 local list = tab list,total_count = M.getPageData(msg_body.page,msg_body.count,list) if #list <= 0 or list == nil then return M.response(fd,200,err_code) -- return M.response(fd,200,cjson.encode({code=10000,msg = "获取资源文件夹列表成功!",data=folder_list,total_count=total_count})) end skynet.call("dbmgr","lua","on_recv","folder_gen_video_list_nums",list,fd,total_count) end M.response_db_folder_generate_video_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].book_name,folder_id=list[i].id,create_time=list[i].create_time,info=list[i].generate_video_info} end return M.response(fd,200,cjson.encode({code=10000,msg = "获取生成视频文件夹列表成功!",data=folder_list,total_count=total_count})) end --回应db获取文件夹内的生成视频 M.response_db_get_video_list_by_folder_id = function(fd,msg_body,isok,tab) if not isok then return M.response(fd,200,'{"code": 10000, "msg": "获取文件夹内的生成视频失败", "data": [] }') end local list = tab local folder_list = {} local total_count = 0 list,total_count = M.getPageData(msg_body.page,msg_body.count,list) for i = 1, #list, 1 do table.insert(folder_list,i,{template_id=list[i].template_id,video_state=list[i].video_state,video_url=list[i].video_url,create_time=list[i].create_time,file_id=list[i].id,info=list[i].generate_video_info,file_name=list[i].video_name,custom=list[i].custom,path=list[i].path}) end if #list<=0 then return M.response(fd,200,'{"code": 10000, "msg": "获取文件夹内的生成视频失败", "data": [] }') end return M.response(fd,200,cjson.encode({code=10000,msg = "获取文件夹内的生成视频成功!",data=folder_list,total_count=total_count})) end M.response_db_search_generate_video_file = function(fd,msg_body,isok,tab) local error_json = '{"code": 10000, "msg": "搜索失败", "data": [] }' if not isok then return M.response(fd,200,error_json) end local list = tab local file_list = {} local total_count = 0 list ,total_count = M.getPageData(msg_body.page,msg_body.count,list) for i = 1, #list, 1 do table.insert(file_list,i,{template_id=list[i].template_id,video_state=list[i].video_state,video_url=list[i].video_url,create_time=list[i].create_time,file_id=list[i].id,info=list[i].generate_video_info,file_name=list[i].video_name,custom=list[i].custom,path=list[i].path}) -- table.insert(file_list,i,{create_time=list[i].create_time,file_id=list[i].id,info=list[i].generate_video_info,file_name=list[i].video_name}) end if #list<=0 then return M.response(fd,200,error_json) end return M.response(fd,200,cjson.encode({code=10000,msg = "搜索成功",data=file_list,total_count=total_count})) end M.response_db_generate_video = function(fd,isok) if not isok then return M.response(fd,200,cjson.encode({code=10001,msg = "生成视频失败"})) end return M.response(fd,200,cjson.encode({code=10000,msg = "生成视频成功"})) end M.response_db_check_folder_repeat_file = function(fd,msg_body,isok,tab) -- skynet.error("response_db_check_folder_repeat_file",tab) -- M.dump(tab) if #tab.normal_file_name_list<=0 then tab.normal_file_name_list = '' end if #tab.repeat_file_name_list<=0 then tab.repeat_file_name_list = '' end return M.response(fd,200,cjson.encode({code=10000,msg = "检测完毕!",data=tab})) end M.response_db_get_share_user_list = function(fd,msg_body,isok,tab) local error_json = '{"code": 10000, "msg": "获取失败", "data": [] }' local list = tab local user_list = {} local total_count = 0 list ,total_count = M.getPageData(msg_body.page,msg_body.count,list) if #list<=0 then return M.response(fd,200,error_json) end local temp = {} for i = 1, #list, 1 do table.insert(temp,i,{user_id=list[i].id,name=list[i].name,group_id=list[i].group_id}) end return M.response(fd,200,cjson.encode({code=10000,msg = "获取成功!",data=temp,total_count=total_count})) end M.response_db_get_folder_info = function(fd,msg_body,isok,tab) local error_json = '{"code": 10000, "msg": "获取失败", "data": [] }' if not isok or #tab<=0 then return M.response(fd,200,error_json) end return M.response(fd,200,cjson.encode({code=10000,msg = "获取成功!",data=tab})) end M.response_db_get_file_info_by_id = function(fd,msg_body,isok,tab) local error_json = '{"code": 10001, "msg": "获取失败" }' if not isok or #tab<=0 then return M.response(fd,200,error_json) end return M.response(fd,200,cjson.encode({code=10000,msg = "获取成功!",data=tab})) end M.response_db_get_not_have_file_list_by_list = function(fd,msg_body,isok,tab) local error_json = '{"code": 10000, "msg": "没有被删除的文件","data": [] }' if not isok or #tab<=0 then return M.response(fd,200,error_json) end return M.response(fd,200,cjson.encode({code=10000,msg = "获取被删除的文件!",data=tab})) end M.response_setItem = function(fd,isok) local error_json = '{"code": 10001, "msg": "保存数据失败" }' if not isok then return M.response(fd,200,error_json) end return M.response(fd,200,cjson.encode({code=10000,msg = "保存数据成功"})) end M.response_getItem = function(fd,isok,tab) local error_json = '{"code": 10001, "msg": "获取数据失败" }' if not isok then return M.response(fd,200,error_json) end if tab==nil then return M.response(fd,200,error_json) end return M.response(fd,200,cjson.encode({code=10000,msg = "获取数据成功",data=tab})) end M.getRandomIndex = function(array) local count = #array local index = math.random(1,count) return index end M.getRandomArray = function(array) local numberTable = {} for i = 1, #array do table.insert(numberTable,i) end -- local count = #array function getRandom() local index = math.random(1,#numberTable) local random = numberTable[index] table.remove(numberTable, index) return random end local randomNumList = {} for i = 1, #array do local random = getRandom() table.insert(randomNumList,random) end return randomNumList end return M