Ver código fonte

增加上传功能

a158 8 meses atrás
pai
commit
060d5a988d

+ 35 - 0
target/fwutils/fwutils/controll/upload.lua

@@ -0,0 +1,35 @@
+
+local fwutils_upload = require("fwutils.fwutils.function.upload")
+local fwutils_config = require("fwutils.config")
+local request = require("fastweb.request")
+local M = {}
+
+
+
+M.up = function()
+    
+    local result = {}
+    local multipart = request.multipart()
+    for i,v in ipairs(multipart) do
+        local filename = v.param.filename
+        -- 取扩展名
+        local ext = utils.ext(filename)
+        local temp_filepath = fwutils_config.path.temp_path .."/".. fw.make_software_guid().."."..ext
+        utils.save_file(temp_filepath,v.param.data)
+        local line = {
+            name = v.param.name,
+            url =  temp_filepath
+        }
+        request.multipart_content_save(v.id,fw.website_dir().."/"..temp_filepath) 
+        table.insert(result,line)
+    end
+    succ(result)
+end
+
+M.exec = function()
+    exec({
+        up = M.up
+    })
+end
+
+return M

+ 44 - 0
target/fwutils/fwutils/function/files.lua

@@ -0,0 +1,44 @@
+require("fwutils.webapi")
+local M = {}
+
+ M.add = function(file_size,filepath,desc,conn)
+    local ppst = conn:setsql("INSERT INTO fw_file (role_id,custom_id,file_size,filepath,`desc`) VALUES (?,?,?,?,?)")
+    local user_data = cjson.decode(request.get("user_data"))
+    ppst:set_i32(1,user_data.role_id)
+    ppst:set_i32(2,user_data.id)
+    ppst:set_i64(3,file_size)
+    ppst:set_str(4,filepath)
+    ppst:set_str(5,desc)
+    if ppst:update() == 1 then
+        return true
+    end
+    return false
+end
+M.use = function(id,conn)
+    local ppst = conn:setsql("UPDATE fw_file SET use_time = NOW() WHERE id = ?")
+    ppst:set_i32(1,id)
+    if ppst:update() == 1 then
+        return true
+    end
+    return false
+end
+M.delete_by_id = function(id,conn)
+    local ppst = conn:setsql("UPDATE fw_file SET delete_time = NOW() WHERE id = ?")
+    ppst:set_i32(1,id)
+    if ppst:update() == 1 then
+        return true
+    end
+    return false
+end
+M.delete_by_filepath = function(filepath,conn)
+    local ppst = conn:setsql("UPDATE fw_file SET delete_time = NOW() WHERE role_id = ? AND custom_id = ? AND filepath = ?")
+    local user_data = cjson.decode(request.get("user_data"))
+    ppst:set_i32(1,user_data.role_id)
+    ppst:set_i32(2,user_data.id)
+    ppst:set_str(3,filepath)
+    if ppst:update() == 1 then
+        return true
+    end
+    return false
+end
+return M

+ 38 - 0
target/fwutils/fwutils/function/upload.lua

@@ -0,0 +1,38 @@
+require("fwutils.webapi")
+local oss = require("aliyun.oss")
+local fw_files = require("fwutils.fwutils.function.files")
+local M = {}
+
+M.up_file = function(config,remote_dir,local_filepath,desc,conn)
+    local result,filepath = oss.upload_file(config,remote_dir,local_filepath,false)
+    if result == false then
+        return false,filepath
+    end
+    result = fw_files.add(
+        utils.file_size(local_filepath),
+        filepath,
+        desc,
+        conn
+    )
+    if result == false then
+        return false,"添加文件失败"
+    end
+    return true,filepath
+end
+M.up_data = function(config,remote_dir,data,ext,desc,conn)
+    local result,filepath = oss.upload_data(config,remote_dir,data,ext)
+    if result == false then
+        return false,filepath
+    end
+    result = fw_files.add(
+        #data,
+        filepath,
+        desc,
+        conn
+    )
+    if result == false then
+        return false,"添加文件失败"
+    end
+    return true,filepath
+end
+return M