4 Star 4 Fork 3

winfan/openresty_resumablejs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
backend_file_storage_handler.lua 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
tanwenliang 提交于 2016-04-15 12:52 . init code
-- Copyright (C) 2013 Piotr Gaertig
-- Same as file_storage_handler but communicates with backend on file start and file end.
-- Result from file start is returned when backend returns status code greater than 299.
-- In case of file end the result from backend call is always returned.
local file_storage_handler = require "file_storage_handler"
local setmetatable = setmetatable
local ngx = ngx
local string = string
local concat = table.concat
local error = error
module(...)
local function end_backend(self, ctx)
-- last chunk commited?
if ctx.range_to + 1 == ctx.range_total then
ngx.req.set_header('Content-Type', 'application/x-www-form-urlencoded')
return ngx.location.capture(self.backend, {
method = ngx.HTTP_POST,
body = ngx.encode_args({
size = ctx.range_total,
id = ctx.id,
path = ctx.file_path,
name = ctx.get_name(),
checksum = ctx.checksum,
sha1 = ctx.sha1
})
})
end
end
-- override
local function on_body_start(self, ctx)
local file_path = concat({self.dir, ctx.id}, "/")
ctx.file_path = file_path
return self:init_file(ctx)
end
-- override
local function on_body_end(self, ctx)
self:close_file()
-- call backend if finished
local res = end_backend(self, ctx)
return {201, string.format("0-%d/%d", ctx.range_to, ctx.range_total), response = res }
end
function _M:new(dir, backend)
if not backend then
return nil, "Configuration error: no backend specified"
end
local inst = {
super = file_storage_handler:new(dir),
backend = backend,
on_body_start = on_body_start,
on_body_end = on_body_end
}
return setmetatable(inst, { __index = function(t,k) return t.super[k] end} )
end
setmetatable(_M, {
__newindex = function (_, n)
error("attempt to write to undeclared variable "..n, 2)
end,
__index = function (_, n)
error("attempt to read undeclared variable "..n, 2)
end,
})
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Lua
1
https://gitee.com/winfan/openresty_resumablejs.git
git@gitee.com:winfan/openresty_resumablejs.git
winfan
openresty_resumablejs
openresty_resumablejs
master

搜索帮助