From c966f7714244d115b45d800f29370f30bf6313a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E9=80=B8=E8=BE=B0?= Date: Fri, 24 Sep 2021 20:18:39 +0800 Subject: [PATCH] add image check when combine (#26) --- prod/combine.lua | 42 ------------------- prod/upload.lua | 89 ----------------------------------------- prod/upload/combine.lua | 16 ++++++-- prod/upload/upload.lua | 4 +- 4 files changed, 14 insertions(+), 137 deletions(-) delete mode 100644 prod/combine.lua delete mode 100644 prod/upload.lua diff --git a/prod/combine.lua b/prod/combine.lua deleted file mode 100644 index d295fd7..0000000 --- a/prod/combine.lua +++ /dev/null @@ -1,42 +0,0 @@ -local uuid = require "resty.jit-uuid" -local cjson = require "cjson" -uuid.seed() - -local file_dir = ngx.var.tmp_file_dir -ngx.req.read_body() -local req_body = cjson.decode(ngx.req.get_body_data()) -ngx.log(ngx.INFO, 'REQ_BODY -> ', ngx.req.get_body_data()) -local data = req_body["data"]["images"] -local combined = file_dir .. "/" .. uuid.generate_v4() .. ".jpeg"; -local len = table.getn(data) -local origin = {} -for i = 1, len do - ngx.log(ngx.INFO, "COMBINE FILE ->", data[i]) - origin[i] = file_dir .. "/" .. data[i] .. " " -end --- 从 nginx 变量取 -local magick = ngx.var.magick -local convert = ngx.var.convert - -if not magick then - if not convert then - ngx.status = 500 - ngx.say("image magick not found.") - return - end - magick = "" -end - -local cmd = magick .. " convert -append " .. table.concat(origin, " ") .. " " .. combined -ngx.log(ngx.INFO, 'CMD -> ', cmd) -local result, _, code = os.execute(cmd) -if result and code == 0 then - local file = io.open(combined) - local content = file:read("*all") - file:close() - os.remove(combined) - ngx.say(content) -else - ngx.status = 500 - ngx.say("image combine failed.") -end diff --git a/prod/upload.lua b/prod/upload.lua deleted file mode 100644 index 0d08a4a..0000000 --- a/prod/upload.lua +++ /dev/null @@ -1,89 +0,0 @@ -local upload = require "resty.upload" -local uuid = require "resty.jit-uuid" -local cjson = require "cjson" - -local chunk_size = 4096 -local form = upload:new(chunk_size) -uuid.seed() - -TaskTypes = { - NOOP = 'NOOP', - UPLOAD_PICTURE = 'UPLOAD_PICTURE' -} - -local task_server = ngx.var.task_server -local file_dir = ngx.var.tmp_file_dir -local file -local file_name -local files = {} - -while true do - local typ, res, err = form:read() - - if not typ then - ngx.say("failed to read: ", err) - return - end - - if typ == "header" then - - --"Content-Disposition","form-data; name=\"files[]\"; filename=\"Song-of-joy.png\"" - --"Content-Type","image\/png" - local key = res[1] - local val = res[2] - if key == "Content-Type" then - local ext = ngx.re.match(val, [[(\w+)\/(\w+)]], "jo")[2] - file_name = uuid() .. "." .. ext - end - - if file_name then - file = io.open(file_dir .. "/" .. file_name, "w+") - ngx.log(ngx.INFO, "FILENAME -> ", file_name) - if not file then - ngx.say("failed to open file ", file_name) - return - end - end - - elseif typ == "body" then - if file then - file:write(res) - -- sha1:update(res) - end - elseif typ == "part_end" then - if file then - file:close() - table.insert(files, file_name) - end - -- 这里要重置一下file_name,否则后面的文件保存时会导致前面已保存的文件变成空文件 - -- file:flush() 和 io.flush() 都没效果 - file_name = nil - file = nil - elseif typ == "eof" then - local http = require "resty.http" - local httpc = http.new() - local remote_url = string.format("%s/api/v1/ts/create_task", task_server) - ngx.log(ngx.INFO, 'remote_url -> ', remote_url) - ngx.log(ngx.INFO, 'authorization -> ', ngx.req.get_headers().Authorization) - local res, err = httpc:request_uri( - remote_url, - { - method = "POST", - headers = { - ["Content-Type"] = "application/json", - ["Authorization"] = ngx.req.get_headers().Authorization - }, - body = cjson.encode({task_type = TaskTypes.UPLOAD_PICTURE, data = {images = files}}), - } - ) - if 201 ~= res.status then - ngx.log(ngx.ERR, "create task failed", err) - ngx.say(err) - ngx.exit(res.status) - end - ngx.say(res.body) - break - else - -- do nothing - end -end diff --git a/prod/upload/combine.lua b/prod/upload/combine.lua index 4fb4971..5ee8725 100644 --- a/prod/upload/combine.lua +++ b/prod/upload/combine.lua @@ -5,12 +5,18 @@ uuid.seed() local file_dir = ngx.var.tmp_file_dir ngx.req.read_body() local req_body = cjson.decode(ngx.req.get_body_data()) - -local data = req_body["data"] +ngx.log(ngx.INFO, 'REQ_BODY -> ', ngx.req.get_body_data()) +local data = req_body["data"]["images"] local combined = file_dir .. "/" .. uuid.generate_v4() .. ".jpeg"; local len = table.getn(data) +if len == 0 then + ngx.status = 500 + ngx.say("No images to combine!") + return; +end local origin = {} for i = 1, len do + ngx.log(ngx.INFO, "COMBINE FILE ->", data[i]) origin[i] = file_dir .. "/" .. data[i] .. " " end -- 从 nginx 变量取 @@ -26,7 +32,9 @@ if not magick then magick = "" end -local result, _, code = os.execute(magick .. " convert -append " .. table.concat(origin, " ") .. " " .. combined) +local cmd = magick .. " convert -append " .. table.concat(origin, " ") .. " " .. combined +ngx.log(ngx.INFO, 'CMD -> ', cmd) +local result, _, code = os.execute(cmd) if result and code == 0 then local file = io.open(combined) local content = file:read("*all") @@ -36,4 +44,4 @@ if result and code == 0 then else ngx.status = 500 ngx.say("image combine failed.") -end \ No newline at end of file +end diff --git a/prod/upload/upload.lua b/prod/upload/upload.lua index b216640..654c98a 100644 --- a/prod/upload/upload.lua +++ b/prod/upload/upload.lua @@ -38,6 +38,7 @@ while true do if file_name then file = io.open(file_dir .. "/" .. file_name, "w+") + ngx.log(ngx.INFO, "FILENAME -> ", file_name) if not file then ngx.say("failed to open file ", file_name) return @@ -49,7 +50,6 @@ while true do file:write(res) -- sha1:update(res) end - elseif typ == "part_end" then if file then file:close() @@ -86,4 +86,4 @@ while true do else -- do nothing end -end \ No newline at end of file +end