This commit is contained in:
2021-11-18 20:02:01 +08:00
parent 14406d5cc3
commit d3252249c8
15 changed files with 13 additions and 771 deletions
+13 -244
View File
@@ -1,264 +1,33 @@
# Resty Function
* [1.图片上传](#1)
* [2.图片拼接](#2)
* [4.图片下载](#3)
------
## 前置安装
- 安装opm
```bash
sudo dnf -f install dnf -y install openresty-opm
```
- 安装 lua-resty-jit-uuid https://github.com/thibaultcha/lua-resty-jit-uuid
```bash
opm get thibaultcha/lua-resty-jit-uuid
```
- 通用命令 重启服务
```bash
$ sudo openresty -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
#首先要openresty nginx配置文件无误
$ sudo systemctl restart openresty
#重启openresty服务不报错
```
## nginx根配置
- 配置openresty
```nginx configuration
# 配置文件路径
# /usr/local/openresty/nginx/conf/nginx.conf
# http contex 中添加lua脚本的路径
lua_package_path "/usr/local/openresty/resty_funcs/?.lua;;";
```
<h2 id="1">1.图片上传</h2>
- music-room配置
```nginx configuration
# 子域名配置文件路径
# /usr/local/openresty/nginx/conf.d/music-room.conf
# server contex 中添加文件存储路径变量
set $store_dir "/file_upload/"; # 文件存储路径
# server contex 中添加location
# Upload image file
location /upload_image {
content_by_lua_file /usr/local/openresty/resty_funcs/upload_image.lua;
}
```
- 添加lua脚本
将file_upload文件夹下的upload_image.lua 拷贝到如下路径
put `upload.lua` into:
```bash
/usr/local/openresty/resty_funcs
.
└── upload_image.lua
/usr/local/opt/openresty/nginx/resty_funcs
```
- 测试curl命令
post file:
```bash
curl -v -F 'up_load_json=@./test_files/tile-0.jpg' -F 'up_load_json=@./test_files/tile-1.jpg' -F 'up_load_json=@./test_files/tile-2.jpg' https://music-room.alchemy-studio.cn/upload_image
$ curl --location --request POST 'localhost/upload' \
--form '=@"/Users/weli/works/task_server/src/test/resources/file_example_JPG_100kB.jpeg"'
```
⚠️ **./test_files/test0.jpg ./test_files/test1.jpg ./test_files/test2.jpg 为三张上传拼接的图片的路径和文件名**
nginx config
```bash
* Trying 152.136.103.69...
* TCP_NODELAY set
* Connected to music-room.alchemy-studio.cn (152.136.103.69) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/cert.pem
CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: CN=music-room.alchemy-studio.cn
* start date: May 16 05:38:01 2021 GMT
* expire date: Aug 14 05:38:01 2021 GMT
* subjectAltName: host "music-room.alchemy-studio.cn" matched cert's "music-room.alchemy-studio.cn"
* issuer: C=US; O=Let's Encrypt; CN=R3
* SSL certificate verify ok.
> POST /upload_image HTTP/1.1
> Host: music-room.alchemy-studio.cn
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Length: 299590
> Content-Type: multipart/form-data; boundary=------------------------67148dd99aa78bdc
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< Server: openresty/1.19.3.2
< Date: Thu, 22 Jul 2021 12:01:51 GMT
< Content-Type: application/octet-stream
< Transfer-Encoding: chunked
< Connection: keep-alive
<
-->> POST : Upload origin image is test0.jpg
Open file ok 82e3830b-c6ee-45cd-85c6-246682c087aa.jpg
Insert upload image table 1 with /file_upload/82e3830b-c6ee-45cd-85c6-246682c087aa.jpg
-->> POST : Upload origin image is test1.jpg
Open file ok f67ece02-cb5c-48f8-9ff9-e56c69239b15.jpg
Insert upload image table 2 with /file_upload/f67ece02-cb5c-48f8-9ff9-e56c69239b15.jpg
-->> POST : Upload origin image is test2.jpg
Open file ok 992da1f2-9a9e-41e2-a14d-9d57aa07dcd2.jpg
Insert upload image table 3 with /file_upload/992da1f2-9a9e-41e2-a14d-9d57aa07dcd2.jpg
Show all upload files
Save upload file is : /file_upload/82e3830b-c6ee-45cd-85c6-246682c087aa.jpg
Save upload file is : /file_upload/f67ece02-cb5c-48f8-9ff9-e56c69239b15.jpg
Save upload file is : /file_upload/992da1f2-9a9e-41e2-a14d-9d57aa07dcd2.jpg
Input images list : /file_upload/82e3830b-c6ee-45cd-85c6-246682c087aa.jpg /file_upload/f67ece02-cb5c-48f8-9ff9-e56c69239b15.jpg /file_upload/992da1f2-9a9e-41e2-a14d-9d57aa07dcd2.jpg
Output image is : /file_upload/553120d6-879d-4c9f-a74a-8319b7903642.jpg
/usr/local/ImageMagick/bin/magick convert -append /file_upload/82e3830b-c6ee-45cd-85c6-246682c087aa.jpg /file_upload/f67ece02-cb5c-48f8-9ff9-e56c69239b15.jpg /file_upload/992da1f2-9a9e-41e2-a14d-9d57aa07dcd2.jpg /file_upload/553120d6-879d-4c9f-a74a-8319b7903642.jpg
* Connection #0 to host music-room.alchemy-studio.cn left intact
* Closing connection 0
```
/usr/local/etc/openresty/nginx.conf
```
- 查看上传文件和拼接文件
```bash
$ pwd
/file_upload
$ ls -l
总用量 1440
-rw-rw-rw- 1 nobody nobody 99699 7月 21 22:25 08916bcf-93f4-4a2a-8d39-0e11174fdb90.jpg
-rw-rw-rw- 1 nobody nobody 35979 7月 22 11:46 52b2a24d-da6c-41e8-8cdb-2eab371eb56f.jpg
-rw-rw-rw- 1 nobody nobody 298648 7月 22 20:01 553120d6-879d-4c9f-a74a-8319b7903642.jpg
-rw-rw-rw- 1 nobody nobody 298648 7月 21 22:25 6143c43c-e2ab-4002-b391-9f82f6008931.jpg
-rw-rw-rw- 1 nobody nobody 99699 7月 22 20:01 82e3830b-c6ee-45cd-85c6-246682c087aa.jpg
-rw-rw-rw- 1 nobody nobody 99699 7月 21 22:25 8f2cd5d0-09f6-4be7-9829-3a12e92b81d3.jpg
-rw-rw-rw- 1 nobody nobody 99699 7月 22 20:01 992da1f2-9a9e-41e2-a14d-9d57aa07dcd2.jpg
-rw-rw-rw- 1 nobody nobody 6109 7月 22 11:46 9c831637-7de6-4255-8878-f2069b8f4f08.jpg
-rw-rw-rw- 1 nobody nobody 99699 7月 21 22:25 9ec1a383-9678-4c16-834f-57b4d7f87a1d.jpg
```
<h2 id="2">2.图片拼接</h2>
- music-room配置
```nginx configuration
# 子域名配置文件路径
# /usr/local/openresty/nginx/conf.d/music-room.conf
# server contex 中添加文件存储路径变量
set $store_dir "/file_upload/"; # 文件存储路径
# server contex 中添加location
#Image append
location /image_append {
content_by_lua_file /usr/local/openresty/nginx/conf/lua/image_append.lua;
location /upload {
content_by_lua_file /usr/local/opt/openresty/nginx/resty_funcs/upload.lua;
}
```
- 添加lua脚本
enable log:
```bash
/usr/local/openresty/resty_funcs
.
└── image_append.lua
```
error_log /usr/local/etc/openresty/logs/error.log;
```
<h2 id="3">3.图片下载</h2>
- music-room配置-
```nginx configuration
# 子域名配置文件路径
# /usr/local/openresty/nginx/conf.d/music-room.conf
# server contex 中添加location
#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})
}
}
```
- wget测试
```bash
wget http://localhost:8080/download_image/{download_image_uuid}
```
-52
View File
@@ -1,52 +0,0 @@
server {
server_name music-room.alchemy-studio.cn;
listen 443 ssl;
set $store_dir "/file_upload/"; # 文件存储路径
location / {
try_files $uri $uri/ /index.html;
proxy_set_header Host music-room.alchemy-studio.cn;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $remote_addr;
}
root /usr/local/openresty/nginx/html/music-room;
index index.html;
location /test_hello_world {
#设置content type
default_type text/html ;
# HTTP Status Code 和 内容
return 200 "hello world! ";
}
#Upload dir
location /append_file_output {
default_type "";
alias /file_upload;
autoidex on;
autoindex_exact_size off;
autoindex_localtime on;
}
#Upload image file
location /upload_image {
content_by_lua_file /usr/local/openresty/resty_funcs/upload_image.lua;
}
location /ws/ {
proxy_pass http://127.0.0.1:3001/;
proxy_set_header Host music-room.alchemy-studio.cn;
}
location /uc/ {
proxy_pass http://127.0.0.1:3000/;
proxy_set_header Host music-room.alchemy-studio.cn;
}
ssl_certificate /etc/letsencrypt/live/music-room.alchemy-studio.cn/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/music-room.alchemy-studio.cn/privkey.pem; # managed by Certbot
}
-7
View File
@@ -1,7 +0,0 @@
---- 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)
-223
View File
@@ -1,223 +0,0 @@
-- Require lua modules
local upload = require "resty/upload"
local uuid = require "resty.jit-uuid"
local redis = require "resty.redis"
local cjson = require "cjson"
---------------------------------------------------------------------------------------
-- Functions : String split
string.split = function(s, p)
local rt= {}
string.gsub(s, '[^'..p..']+', function(w) table.insert(rt, w) end )
return rt
end
-- Functions : Get file trim
string.trim = function(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
---------------------------------------------------------------------------------------
-- Init
---- Upload init
local chunk_size = 5 -- should be set to 4096 or 8192
-- for real-world settings
local form, error = upload:new(chunk_size)
if not form then
ngx.log(ngx.ERR, "Fail to create new form: ", error)
ngx.exit(500)
end
---- Automatic seeding with os.time(), LuaSocket, or ngx.time()
uuid.seed()
uuid()
---- Redis init
------ Connect to redis
local redis_task_database = redis:new()
redis_task_database:set_timeouts(1000, 1000, 1000)
local ok, error = redis_task_database:connect("127.0.0.1", 6379)
if not ok then
ngx.log(ngx.ERR, "Fail to connect redis: ", error)
ngx.exit(500)
end
---------------------------------------------------------------------------------------
-- Save upload images
---- Upload file save path config in nginx.conf
local save_upload_file_path = ngx.var.store_dir
local file_to_save
local ret_save = false
local upload_image_filename
local save_image_filename
local upload_image_table = {}
local data_table = {}
local upload_image_index = 1
form:set_timeout(1000) -- 1 sec
---- Read image file from http packet and save it
while true do
local http_type, result, error = form:read()
if not http_type then
ngx.log(ngx.ERR, "Failed to read form: ", error)
ngx.exit(500)
end
if http_type == "header" then
-- Start to read http header
local key = result[1]
local value = result[2]
if key == "Content-Disposition" then
-- Analyze this time upload image filename , use ; as interval
-- Like: form-data; name="testFileName"; filename="testfile.txt"
local key_value_list = string.split(value, ';')
for _, key_value in ipairs(key_value_list) do
local segment = string.trim(key_value)
if segment:find("filename") then
local key_value_file = string.split(segment, "=")
upload_image_filename = string.sub(key_value_file[2], 2, -2)
ngx.log(ngx.INFO, "Upload origin filename: ", upload_image_filename)
-- Save upload file
if upload_image_filename then
-- Save file name with generate uuid
save_image_filename = uuid:generate_v4() -- Generate save filename uuid
save_image_filename = save_image_filename .. '.' .. upload_image_filename:match(".+%.(%w+)$")
file_to_save = io.open(save_upload_file_path .. save_image_filename, "w+")
if not file_to_save then
ngx.log(ngx.ERR , "Fail open file: ", save_image_filename)
ngx.exit(500)
else
ngx.log(ngx.INFO, "Success open file: ", save_image_filename)
-- Save all upload files as a table
data_table[upload_image_index] = save_image_filename
upload_image_table[upload_image_index] = save_upload_file_path .. save_image_filename .. " "
ngx.log(ngx.INFO, "Insert upload image table "..upload_image_index.." with "..upload_image_table[upload_image_index])
upload_image_index = upload_image_index + 1
end
break
end
end
end
end
elseif http_type == "body" then
-- Start to read http body
if file_to_save then
file_to_save:write(result)
end
elseif http_type == "part_end" then
-- Write to save file finish
if file_to_save then
file_to_save:close()
file_to_save = nil
end
ret_save = true
elseif http_type == "eof" then
-- Read file end
break
else
ngx.log(ngx.INFO, "Maybe do other things")
end
end
---------------------------------------------------------------------------------------
-- Show all input and may output files
if ret_save then
ngx.log(ngx.INFO, "Show all upload save files\n")
---- Show all upload save files
upload_image_index = upload_image_index -1
for i = 1, upload_image_index do
ngx.log(ngx.INFO, "Save upload file is : " .. upload_image_table[i])
end
---------------------------------------------------------------------------------------
-- Communicate with front end
---- Generate task uuid
local task_uuid = uuid.generate_v4();
---------------------------------------------------------------------------------------
-- Construct {key: task_id | value: [images] } in json
-- Example:
-- {"task_id":"488a8e6c556a-4271-adec--071e843c9856","data:"[\"2109aac0-556a-4271-adec-9eac494f622f.jpg\",\"488a8e6c-01e6-4cb3-af88-a1d90e04e6b8.jpg\",\"c3f76317-592f-4117-ab0d-071e843c9856.jpg\"]"}
local task_table = {}
task_table["task_id"] = task_uuid
task_table["data"] = data_table
local task_jason = cjson.encode(task_table)
ngx.log(ngx.INFO , task_jason) -- Response to front end
---------------------------------------------------------------------------------------
-- Redis control
---- Set json to redis
ok, error = redis_task_database:set(task_uuid, task_jason)
if not ok then
ngx.log(ngx.ERR , "Failed to set task json: ", error)
ngx.exit(500)
end
ngx.log(ngx.INFO , "Set redis task json result: ", ok)
---- Get data from redis
local result, error = redis_task_database:get(task_uuid)
if not result then
ngx.log(ngx.ERR , "Failed to get task json: ", error)
ngx.exit(500)
end
if result == ngx.null then
ngx.log(ngx.ERR , "Not found task id")
ngx.exit(500)
end
ngx.log(ngx.INFO, "Get redis task id: " .. task_uuid .. " | value :" .. result)
---------------------------------------------------------------------------------------
-- Create task
local result = ngx.location.capture(
'/mock_task_server',
{
method = ngx.HTTP_POST,
body = ngx.encode_args({task = task_jason})
}
)
ngx.say(result.body)
-- Response task ID to front end
ngx.say(task_uuid)
-- Mock a image append request from task from task server
local result = ngx.location.capture(
'/image_append',
{
method = ngx.HTTP_POST,
body = ngx.encode_args({task = task_jason})
}
)
ngx.say(result.body)
end
-58
View File
@@ -1,58 +0,0 @@
local uuid = require "resty.jit-uuid"
local cjson = require "cjson"
local json_string = nil
---- Automatic seeding with os.time(), LuaSocket, or ngx.time()
uuid.seed()
uuid()
-- Get post json argv
local arg = ngx.req.get_post_args()
for k,v in pairs(arg) do
ngx.log(ngx.INFO , "Image append [POST] key:", k .." | ".." v:", v)
json_string = v
end
-- Decode json string
local json = cjson.decode(json_string)
ngx.say("Image append input filename number is :" , table.getn(json.data))
local image_append_input_files_number = table.getn(json.data)
for i = 1, image_append_input_files_number do
ngx.log(ngx.INFO , "Image append input filename is :" , json.data[i])
end
-- Image append
---- Upload file save path config in nginx.conf
local save_upload_file_path = ngx.var.store_dir
local append_image_table = {}
local output_image_uuid = uuid.generate_v4();
-- Add path to input image filename
for i = 1, image_append_input_files_number do
append_image_table[i] = save_upload_file_path .. json.data[i] .. " "
ngx.log(ngx.INFO, "Insert append image table "..i.." with "..append_image_table[i])
end
-- Concat input image to a command string
local append_input_images = table.concat(append_image_table)
ngx.log(ngx.INFO , "Input images list : " , append_input_images)
-- Add path to output image filename
local append_output_image = save_upload_file_path .. output_image_uuid..".jpg ";
ngx.log(ngx.INFO , "Output image is : "..append_output_image)
-- Debug
ngx.log(ngx.INFO , '/usr/local/ImageMagick/bin/magick convert -append '..append_input_images..' '..append_output_image)
local call_imagemagick_cmd = io.popen('/usr/local/ImageMagick/bin/magick convert -append '..append_input_images..' '..append_output_image)
local imagemagick_cmd_replay = call_imagemagick_cmd:read("*all")
ngx.log(ngx.INFO , imagemagick_cmd_replay)
-74
View File
@@ -1,74 +0,0 @@
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
lua_package_path "/usr/local/openresty/resty_funcs/?.lua;;";
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
serer_name alchemy-studio.cn; # managed by Certbot
root /home/nginx/website;
index index.html;
location /wy7of6ofMw.txt {
alias /home/nginx/website/wx_verify/wy7of6ofMw.txt;
}
location / {
try_files $uri $uri/ /index.html =404;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/alchemy-studio.cn/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/alchemy-studio.cn/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
listen 80 ;
listen [::]:80 ;
server_name alchemy-studio.cn;
location / {
return 301 https://$host$request_uri;
}
}
include /usr/local/openresty/nginx/conf.d/music-room.conf;
#include /usr/local/openresty/nginx/conf.d/resty_funcs.conf;
}
-33
View File
@@ -1,33 +0,0 @@
put `upload.lua` into:
```bash
/usr/local/opt/openresty/nginx/resty_funcs
```
post file:
```bash
$ curl --location --request POST 'localhost/upload' \
--form '=@"/Users/weli/works/task_server/src/test/resources/file_example_JPG_100kB.jpeg"'
```
nginx config
```
/usr/local/etc/openresty/nginx.conf
```
```
location /upload {
content_by_lua_file /usr/local/opt/openresty/nginx/resty_funcs/upload.lua;
}
```
enable log:
```
error_log /usr/local/etc/openresty/logs/error.log;
```
-8
View File
@@ -1,8 +0,0 @@
local curl_task = [[curl --location --request POST 'http://localhost:8080/api/v1/ts/create_task' \
--header 'Content-Type: application/json' \
--data-raw '{
"data": ["abc", "def"],
"task_type": 1
}']]
os.execute(curl_task)
-29
View File
@@ -1,29 +0,0 @@
-- read from nginx var config
local magick = ngx.var.magick
ngx.log(ngx.ERR, 'var', magick)
-- or use which command to get
if not magick then
local exec = io.popen("which magick")
local tmp = exec:read("*a")
magick = string.gsub(tmp, "%s+", "")
exec:close()
end
if not magick then
ngx.status = 500
ngx.say("image magick not found.")
return
end
ngx.say(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)
-29
View File
@@ -1,29 +0,0 @@
local httpc = require("resty.http").new()
-- Single-shot requests use the `request_uri` interface.
local res, err = httpc:request_uri("http://127.0.0.1:8080/api/v1/ts/create_task", {
method = "POST",
headers = {
["Content-Type"] = "application/json",
},
body = [[
{
"created_at": "2021-08-30T11:34:25.845915",
"created_by": "quarkus_test",
"data": {
"pics": [
"23f64c00-14d3-4e8a-bc73-10bfe79cd172",
"66ea4c4f-6394-4f97-92ea-e5cde4061d05",
"4ea1df10-b3a9-4f3c-8687-7e12c029e17b"
]
},
"task_status": "PENDING",
"task_type": "UPLOAD_PICTURE",
"updated_at": "2021-08-30T11:34:25.845929",
"updated_by": "quarkus_test"
}
]]
})
ngx.say(res.status)
ngx.say(err)
-10
View File
@@ -1,10 +0,0 @@
--local str = "form-data; name=\"files[]\"; filename=\"Song-of-joy.png\""
--local ext = ngx.re.match(str, [[filename=".*\.(\w+)"]], "jo")
--print(ext)
local str = [[image\/png]]
local ext = ngx.re.match(str, [[image\/(\w+)]])
print(ext)
-4
View File
@@ -1,4 +0,0 @@
ngx.log(ngx.INFO, "test -> ", "info")
ngx.say('file dir...', ngx.var.tmp_file_dir)
ngx.say('task server...', ngx.var.task_server)