-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupload_api.lua
More file actions
109 lines (94 loc) · 3.25 KB
/
upload_api.lua
File metadata and controls
109 lines (94 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
local lfs = require("lfs")
local IO = require("tzj.io")
local encode = require("cjson.safe").encode
local stringy = require("tzj.stringy")
local len = string.len
local sub = string.sub
local http_host = ngx.var.http_host
local upload_root = ngx.var.upload_root
local save_tempdr = ngx.var.save_tempdr
local render_url = ngx.var.render_url
local function folder_exists(folder_name)
if lfs.attributes(folder_name:gsub("\\$",""),"mode") == "directory" then
return true
else
return false
end
end
local function render_folder_path(folder_name)
local folder_name_head_len = 1
local folder_name_tail_len = len(folder_name)
if stringy.startswith(folder_name, '/') then
folder_name_head_len = folder_name_head_len + 1
end
if stringy.endswith(folder_name, '/') then
folder_name_tail_len = folder_name_tail_len - 1
end
return upload_root .. '/' .. sub(folder_name, folder_name_head_len, folder_name_tail_len) .. '/'
end
local function render_http_path(folder_name, filename)
return render_url .. '/' .. folder_name .. '/' .. filename
end
local req_get, req_post, req_files = require "tzj.reqargs"({
tmp_dir = save_tempdr,
timeout = 1000,
chunk_size = 4096,
max_get_args = 100,
mas_post_args = 100,
max_line_size = 512,
max_file_uploads = 10
})
if upload_root then
if not folder_exists(upload_root) then
ngx.status = 400
ngx.say(encode({message="Bad upload_root!!!"}))
return
end
else
ngx.status = 500
ngx.say(encode({message="Upload_root Not Set.."}))
return
end
if not req_post['path'] or not req_files.file then
ngx.status = 400
ngx.say(encode({message="Bad Request..."}))
return
end
local file_path = render_folder_path(req_post['path'])
local full_file_path = file_path .. req_files.file['file']
if not folder_exists(file_path) then
local temp_path = ''
local temp_path_arr = stringy.split(req_post['path'], '/')
for i=1, #temp_path_arr do
temp_path = temp_path .. '/' .. temp_path_arr[i]
if not folder_exists(temp_path) then
lfs.mkdir(upload_root .. temp_path)
end
end
end
if not IO.file_exists(full_file_path) then
local command = "mv " .. req_files.file['temp'] .. " " .. full_file_path
local result, result_code = IO.os_execute(command)
if result_code ~= 0 then
ngx.log(ngx.ERR, "file:", req_files.file['temp'], "error:", result_code, "result:", result)
end
else
if req_post['force'] == 'true' then
local command = "\\mv " .. req_files.file['temp'] .. " " .. full_file_path
local result, result_code = IO.os_execute(command)
if result_code ~= 0 then
ngx.log(ngx.ERR, "file:", req_files.file['temp'], "error:", result_code, "result", result)
end
ngx.log(ngx.ERR, "forced write file:", req_files.file['temp'], "error:", result_code, "result:", result)
else
ngx.status = 400
ngx.say(encode({message='this file already exists...'}))
return
end
end
ngx.status = 200
ngx.say(encode({
size=req_files.file['size'],
file=req_files.file['file'],
path=render_http_path(req_post['path'], req_files.file['file'])
}))