This commit is contained in:
2021-08-28 22:01:00 +08:00
parent 2737641454
commit 6c9e6bd1ec
2 changed files with 88 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
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;
```
+55
View File
@@ -0,0 +1,55 @@
-- local resty_sha1 = require "resty.sha1"
local upload = require "resty.upload"
-- opm get thibaultcha/lua-resty-jit-uuid
local uuid = require "resty.jit-uuid"
local cjson = require "cjson"
local chunk_size = 4096
local form = upload:new(chunk_size)
-- local sha1 = resty_sha1:new()
ngx.say("...FILE UPLOAD...")
local file
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
-- local file_name = my_get_file_name(res)
local file_name = "/tmp/foo.jpeg"
ngx.say("...CREATE FILE....")
if file_name then
file = io.open(file_name, "w+")
if not file then
ngx.say("failed to open file ", file_name)
return
end
end
elseif typ == "body" then
if file then
ngx.say("...WRITE DATA...")
file:write(res)
-- sha1:update(res)
end
elseif typ == "part_end" then
ngx.say("...FINSIHED WRITTING FILE...")
file:close()
file = nil
-- local sha1_sum = sha1:final()
-- sha1:reset()
-- my_save_sha1_sum(sha1_sum)
elseif typ == "eof" then
ngx.say("...NO MORE FILE...")
break
else
-- do nothing
end
end