提交 b16e35d0 authored 作者: zhanrongzhen's avatar zhanrongzhen
上级 9a68a4b2
...@@ -20,6 +20,10 @@ http { ...@@ -20,6 +20,10 @@ http {
server_name localhost; server_name localhost;
access_log logs/access.log main_with_time; access_log logs/access.log main_with_time;
error_log logs/error.log warn; error_log logs/error.log warn;
client_max_body_size 1g;
client_body_buffer_size 20m;
gzip on; gzip on;
gzip_comp_level 6; gzip_comp_level 6;
gzip_types gzip_types
...@@ -160,16 +164,120 @@ http { ...@@ -160,16 +164,120 @@ http {
} }
location /download/ { location /download/ {
internal; # 标记为内部 location,不允许外部直接访问 internal;
alias /var/www/html/resources/default/; # 设置文件根目录 alias /var/www/html/resources/default/;
expires 1d; expires 1d;
add_header Accept-Ranges bytes; add_header Accept-Ranges bytes;
sendfile on; sendfile on;
} }
# 未匹配的请求
location /fileserver/upload {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
content_by_lua_block {
ngx.header["Access-Control-Allow-Origin"] = "*"
ngx.header["Access-Control-Allow-Methods"] = "POST, OPTIONS"
ngx.header["Access-Control-Allow-Headers"] = "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range"
local upload = require "resty.upload"
local cjson = require "cjson"
local client_id = ngx.var.arg_clientId
local chunk_size = 4096
local form, err = upload:new(chunk_size)
if not form then
ngx.log(ngx.ERR, "failed to new upload: ", err)
ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR
ngx.say("upload failed")
return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end
form:set_timeout(60000)
local filename = nil
local file = nil
local filepath = nil
local file_size = 0
local upload_dir =/var/www/html/upload
while true do
local typ, res, err = form:read()
if not typ then
ngx.log(ngx.ERR, "failed to read: ", err)
break
end
if typ == "header" then
local header_name = res[1]
local header_value = res[2]
if header_name == "Content-Disposition" then
local filename_match = string.match(header_value, 'filename="([^"]+)"')
if filename_match then
filename = filename_match
end
end
elseif typ == "body" then
if filename and not file then
if client_id then
upload_dir = upload_dir .. client_id .. "/"
end
filepath = upload_dir .. filename
file = io.open(filepath, "wb")
if not file then
ngx.log(ngx.ERR, "failed to open file: ", filepath)
ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR
ngx.say("failed to create file")
return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end
end
if file then
file:write(res)
file_size = file_size + #res
end
elseif typ == "part_end" then
if file then
file:close()
file = nil
end
filename = nil
elseif typ == "eof" then
break
end
end
if filepath and file_size > 0 then
ngx.header["Content-Type"] = "application/json"
ngx.say(cjson.encode({
success = true,
message = "file uploaded successfully",
client_id = client_id,
filepath = filepath,
filesize = file_size
}))
else
ngx.status = ngx.HTTP_BAD_REQUEST
ngx.header["Content-Type"] = "application/json"
ngx.say(cjson.encode({
success = false,
message = "no file uploaded"
}))
end
}
}
location / { location / {
return 404; return 404;
} }
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论