From 49e4710203c8319605b649736b57831d7c95ceba Mon Sep 17 00:00:00 2001 From: XiaoLi <35646108+xiaolitongxue666@users.noreply.github.com> Date: Mon, 6 Sep 2021 19:33:30 +0800 Subject: [PATCH] complete download image (#15) --- README.md | 67 ++++++++++++--------------- dev/download_image/download_image.lua | 14 +++--- prod/conf.d/music-room.conf | 23 +++++++++ 3 files changed, 59 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 767f9a6..e53cb8d 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ curl -v -F 'up_load_json=@./test_files/tile-0.jpg' -F 'up_load_json=@./test_file ``` ⚠️ **./test_files/test0.jpg ./test_files/test1.jpg ./test_files/test2.jpg 为三张上传拼接的图片的路径和文件名** - + ```bash * Trying 152.136.103.69... * TCP_NODELAY set @@ -219,7 +219,7 @@ location /image_append {

3.图片下载

-- music-room配置 +- music-room配置- ```nginx configuration # 子域名配置文件路径 @@ -227,47 +227,38 @@ location /image_append { # server contex 中添加location -#Donwload image use ngx.req.set_uri -location /download_image { - rewrite_by_lua_block { - ngx.log(ngx.INFO , "location /download_image") - - -- TODO: jwt decoder - -- - - -- Functions : String split - string.split = function(s, p) - local rt= {} - string.gsub(s, '[^'..p..']+', function(w) table.insert(rt, w) end ) - return rt - end - - -- Get url and split by '/' save into a table - local sub_url_table = string.split(ngx.var.uri, '/') - local sub_url_number = table.getn(sub_url_table) - - for i = 1, sub_url_number do - ngx.log(ngx.INFO , "Sub url sub" .. i .. " : " .. sub_url_table[i]) - end - - local uri = "/internal_file_upload/"..sub_url_table[sub_url_number]..".jpg" - ngx.log(ngx.INFO , "uri : " .. uri) - return ngx.req.set_uri(uri , true) - } -} - #Static file server -location /internal_file_upload { - internal; - try_files $uri /file_upload/8493acab-0f7a-47e0-8fff-e96b843334dd.jpg; + location /file_upload { + default_type ""; + alias /file_upload/; + autoindex on; + autoindex_exact_size off; + autoindex_localtime on; + } +#Download image files +location /download_image { + rewrite_by_lua_block{ + -- Get download image id + local download_image_id = string.match(ngx.var.request_uri, ".*/(.*)$") + ngx.log(ngx.INFO , "Download image id: " .. download_image_id) + -- Set redirect uri + --local uri = "/internal_file_upload/" .. download_image_id .. ".jpg" + --return ngx.exec(uri) + + local uri = "/file_upload/" .. download_image_id .. ".jpg" + return ngx.exec(uri, {a = download_image_id}) + } } ``` -- 添加lua脚本 +- wget测试 ```bash -/usr/local/openresty/resty_funcs -. -└── download_image.lua +wget http://localhost:8080/download_image/{download_image_uuid} ``` + + + + + diff --git a/dev/download_image/download_image.lua b/dev/download_image/download_image.lua index 96b395b..30227d8 100644 --- a/dev/download_image/download_image.lua +++ b/dev/download_image/download_image.lua @@ -1,7 +1,7 @@ --- Get download image id -local download_image_id = string.match(ngx.var.request_uri, ".*/(.*)$") -ngx.log(ngx.INFO , "Download image id: " .. download_image_id) - --- Set redirect uri -local uri = "/internal_file_upload/" .. download_image_id .. ".jpg" -return ngx.exec(uri) \ No newline at end of file +---- Get download image id +--local download_image_id = string.match(ngx.var.request_uri, ".*/(.*)$") +--ngx.log(ngx.INFO , "Download image id: " .. download_image_id) +-- +---- Set redirect uri +--local uri = "/internal_file_upload/" .. download_image_id .. ".jpg" +--return ngx.exec(uri) \ No newline at end of file diff --git a/prod/conf.d/music-room.conf b/prod/conf.d/music-room.conf index cf3d4e2..d93c7ea 100644 --- a/prod/conf.d/music-room.conf +++ b/prod/conf.d/music-room.conf @@ -33,6 +33,29 @@ server { location /api/ngx/image/combine { content_by_lua_file /usr/local/openresty/resty_funcs/combine.lua; } + #Static file server + location /file_upload { + default_type ""; + alias /file_upload/; + autoindex on; + autoindex_exact_size off; + autoindex_localtime on; + } + #Download image files + location /download_image { + rewrite_by_lua_block{ + -- Get download image id + local download_image_id = string.match(ngx.var.request_uri, ".*/(.*)$") + ngx.log(ngx.INFO , "Download image id: " .. download_image_id) + + -- Set redirect uri + --local uri = "/internal_file_upload/" .. download_image_id .. ".jpg" + --return ngx.exec(uri) + + local uri = "/file_upload/" .. download_image_id .. ".jpg" + return ngx.exec(uri, {a = download_image_id}) + } + } location /api/v1/ws/ { proxy_pass http://127.0.0.1:3001/api/v1/ws/;