diff --git a/prod/conf.d/music-room.conf b/prod/conf.d/music-room.conf index 505485b..cf3d4e2 100644 --- a/prod/conf.d/music-room.conf +++ b/prod/conf.d/music-room.conf @@ -2,7 +2,7 @@ server { server_name music-room.alchemy-studio.cn; listen 443 ssl; - set $tmp_file_dir "/file_upload/"; # 文件存储路径 + set $tmp_file_dir "/file_upload"; # 文件存储路径 set $task_server "http://127.0.0.1:8080"; # task server host location / { diff --git a/prod/upload/combine.lua b/prod/upload/combine.lua index 3f0a36c..96353d2 100644 --- a/prod/upload/combine.lua +++ b/prod/upload/combine.lua @@ -1,5 +1,32 @@ ---- ---- Generated by EmmyLua(https://github.com/EmmyLua) ---- Created by moicen. ---- DateTime: 2021/9/4 12:28 ---- \ No newline at end of file +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()) + +local data = req_body["data"] +local combined = file_dir .. "/" .. uuid.generate_v4() .. ".jpeg"; +local len = table.getn(data) +local origin = {} +for i = 1, len do + origin[i] = file_dir .. "/" .. data[i] .. " " +end +-- in linux +local magick = "/usr/local/ImageMagick/bin/magick" +local tmp = io.open(magick,"r") +if tmp == nil then + -- in mac os + magick = "/usr/local/bin/magick" +else + io.close(tmp) +end +local file = io.popen(magick .. " convert -append " .. table.concat(origin, " ") .. " " .. combined) +file:read("*all") +file:close() +file = io.open(combined) +local content = file:read("*all") +file:close() +os.remove(combined) +ngx.say(content) \ No newline at end of file diff --git a/prod/upload/upload.lua b/prod/upload/upload.lua index ab6e81d..dc488f0 100644 --- a/prod/upload/upload.lua +++ b/prod/upload/upload.lua @@ -21,8 +21,15 @@ while true do end if typ == "header" then - file_name = string.format("%s/%s.jpeg", file_dir, uuid()) - ngx.log(ngx.ERR, "file name", file_name) + + --"Content-Disposition","form-data; name=\"files[]\"; filename=\"Song-of-joy.png\"" + --"Content-Disposition","form-data; name=\"files[]\"; filename=\"rhythm-recognition-1.png\"" + local key, val = res + if (key == "Content-Disposition") then + local ext = ngx.re.match(val, [[filename=".*\.(\w+)"]], "jo") + file_name = string.format("%s/%s.%s", file_dir, uuid(), ext) + end + if file_name then file = io.open(file_name, "w+") if not file then @@ -45,13 +52,16 @@ while true do elseif typ == "eof" then local http = require "resty.http" local httpc = http.new() - local remote_url = string.format("%s/api/v1/ts/task_server/create_task", task_server) + local remote_url = string.format("%s/api/v1/ts/create_task", task_server) local res, err = httpc:request_uri( remote_url, { method = "POST", + headers = { + host = "ngx" + }, body = { - cjson.encode({task_type = 1, data = files, created_by = 'openresty'}) + cjson.encode({task_type = 1, data = files, created_by = 'ngx', updated_by = 'ngx'}) }, } ) diff --git a/test/test_ngx.lua b/test/test_ngx.lua new file mode 100644 index 0000000..529e108 --- /dev/null +++ b/test/test_ngx.lua @@ -0,0 +1,25 @@ +--local str = "form-data; name=\"files[]\"; filename=\"Song-of-joy.png\"" +--local ext = ngx.re.match(str, [[filename=".*\.(\w+)"]], "jo") +--print(ext) + +local magick = "/usr/local/ImageMagick/bin/magick" +local tmp = io.open(magick,"r") +if tmp == nil then + -- in mac os + magick = "/usr/local/bin/magick" +else + io.close(tmp) +end +print(magick) +-- +--local origin = { +-- "/Users/moicen/Music/alchemy/Song-of-joy.png", +-- "/Users/moicen/Music/alchemy/You-and-me.png" +--} +--origin = table.concat(origin, " ") +--ngx.log(ngx.ERR, origin) +--local file = io.popen("/usr/local/bin/magick convert -append " .. origin .. " /tmp/combined.jpeg" ) +--local output = file:read("*all") +--file:close() +--ngx.say(output) + diff --git a/test/test_var.lua b/test/test_var.lua index 40c8fee..238657c 100644 --- a/test/test_var.lua +++ b/test/test_var.lua @@ -1,8 +1,2 @@ ---- ---- Generated by EmmyLua(https://github.com/EmmyLua) ---- Created by moicen. ---- DateTime: 2021/9/4 12:20 ---- - ngx.say('file dir...', ngx.var.tmp_file_dir) ngx.say('task server...', ngx.var.task_server) \ No newline at end of file