diff --git a/scripts/lib/upyun.lua b/scripts/lib/upyun.lua index 9647e84..031e3b2 100644 --- a/scripts/lib/upyun.lua +++ b/scripts/lib/upyun.lua @@ -574,6 +574,87 @@ function _M.new(self, config) }, mt) end +function _M.new2(self, config) + local user = config.user + local passwd = config.passwd + local endpoint = config.endpoint and tonumber(config.endpoint) + 1 or 1 + local author = config.author and lower(config.author) or nil + local localFilePath = config.localFilePath or nil -- add config para for upload file directly + + if not user or type(user) ~= "string" or user == "" then + return nil, "invalid user" + end + + if not passwd or type(passwd) ~= "string" or passwd == "" then + return nil, "invalid passwd" + end + + if endpoint > 4 then + return nil, "invalid endpoint" + end + + -- explicit "basic" sets author_mode to Basic + local author_mode = "U" + if author == "basic" then + author_mode = "B" + author = "Basic " .. base64(user .. ":" .. passwd) + else + author = "UpYun " .. user .. ":" + end + + ---- file to be uploaded is stored in the request body + --print("Upyun module new config para local file path : ", localFilePath) + --local file = nil + --local content = nil + --if localFilePath == nil then + -- read_body() + -- content = get_body_data() + -- file = ngx.req.get_body_file() + --else + -- file = localFilePath; + --end + -- + --print("Upyun module new config file : ", file) + --if file then + -- local f, err = io.open(file, "r") + -- if not f then + -- return nil, err + -- end + -- + -- content = f:read("*a") + -- f:close() + --end + + --if not content then + -- return nil, "request body is expected" + --end + + --TODO ngx.updatetime? + local date = http_time(time_sec()) + if not date then + return nil, "failed to get current time" + end + + local sock = tcp() + if not sock then + return nil, "failed to create a TCP socket" + end + + return setmetatable({ + sock = sock, + user = user, + passwd = passwd, + author_mode = author_mode, + headers = { + Authorization = author, + Host = host_list[endpoint], + Date = date, + ["Content-Length"] = "0" + }, + body = { content = content }, + }, mt) +end + function _M.upload_file(self, path, gmkerl, option) -- return info, err local headers = self.headers diff --git a/scripts/lib/upyun_remove.lua b/scripts/lib/upyun_remove.lua index 82788bc..be1f550 100644 --- a/scripts/lib/upyun_remove.lua +++ b/scripts/lib/upyun_remove.lua @@ -19,27 +19,28 @@ function upyun_remove.remove(filepath, filename, retry) ngx.log(ngx.ERR, "failed to remove file : reach max retries") else - -- 使用 os.tmpname 获取临时文件名 - local tempFileName = "upyun_remove_temp_file" - -- 拼接路径 - local tempFilePath = "/tmp/" .. tempFileName - -- 使用 io.open 创建临时文件 - local tempFile = io.open(tempFilePath, "w") - -- 写入内容到临时文件 - tempFile:write("upyun new config local temp file") - -- 关闭文件 - tempFile:close() - -- 使用 os.execute 执行 chmod 命令 - os.execute("chmod 777 " .. tempFilePath) - --print("Upyun new config local temp file path : ", tempFilePath) + ---- 使用 os.tmpname 获取临时文件名 + --local tempFileName = "upyun_remove_temp_file" + ---- 拼接路径 + --local tempFilePath = "/tmp/" .. tempFileName + ---- 使用 io.open 创建临时文件 + --local tempFile = io.open(tempFilePath, "w") + ---- 写入内容到临时文件 + --tempFile:write("upyun new config local temp file") + ---- 关闭文件 + --tempFile:close() + ---- 使用 os.execute 执行 chmod 命令 + --os.execute("chmod 777 " .. tempFilePath) + ----print("Upyun new config local temp file path : ", tempFilePath) - local upyun, err = Upyun:new({ + local upyun, err = Upyun:new2({ user = ngx.var.upyun_operator, passwd = ngx.var.upyun_password, --localFilePath = filepath -- TODO : Add a fake file for create new upyun entry --localFilePath = "/Users/liyong/Code/AlchemyStudio/resty_functions/test_files_dir" - localFilePath = tempFilePath + --localFilePath = tempFilePath + localFilePath = nil }) if not upyun then diff --git a/scripts/test_upyun_upload.lua b/scripts/test_upyun_upload.lua index c88e5cc..f5843b0 100644 --- a/scripts/test_upyun_upload.lua +++ b/scripts/test_upyun_upload.lua @@ -34,19 +34,19 @@ end local function upyun_remove_file() - -- 使用 os.tmpname 获取临时文件名 - local tempFileName = "upyun_remove_temp_file" - -- 拼接路径 - local tempFilePath = "/tmp/" .. tempFileName - -- 使用 io.open 创建临时文件 - local tempFile = io.open(tempFilePath, "w") - -- 写入内容到临时文件 - tempFile:write("upyun new config local temp file") - -- 关闭文件 - tempFile:close() - -- 使用 os.execute 执行 chmod 命令 - os.execute("chmod 777 " .. tempFilePath) - --print("Upyun new config local temp file path : ", tempFilePath) + ---- 使用 os.tmpname 获取临时文件名 + --local tempFileName = "upyun_remove_temp_file" + ---- 拼接路径 + --local tempFilePath = "/tmp/" .. tempFileName + ---- 使用 io.open 创建临时文件 + --local tempFile = io.open(tempFilePath, "w") + ---- 写入内容到临时文件 + --tempFile:write("upyun new config local temp file") + ---- 关闭文件 + --tempFile:close() + ---- 使用 os.execute 执行 chmod 命令 + --os.execute("chmod 777 " .. tempFilePath) + ----print("Upyun new config local temp file path : ", tempFilePath) local config = { --user = ngx.var.upyun_operator, @@ -55,11 +55,11 @@ local function upyun_remove_file() passwd = "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN", --localFilePath = "/file_upload/abc.mp3" --localFilePath = "/Users/liyong/Code/AlchemyStudio/resty_functions/test_files_dir/r2d2.jpeg" - localFilePath = tempFilePath - --localFilePath = nil + --localFilePath = tempFilePath + localFilePath = nil } - local upyun = yun:new(config) + local upyun = yun:new2(config) --local bucket = ngx.var.upyun_bucket local bucket = "moicen" local directory = 'test' @@ -75,6 +75,6 @@ local function upyun_remove_file() end -upyun_upload_file() +--upyun_upload_file() ---upyun_remove_file() \ No newline at end of file +upyun_remove_file() \ No newline at end of file