YOLO v5 Series - Image & Video Storage ( Openresty + Lua)

c 复制代码
local os = require 'os'
local upload = require 'resty.upload'

get_os_type

c 复制代码
local function get_os_type()
    local os_type = 'unknown'
    if package.config:sub(1,1) == '\\' then
        os_type = 'windows'
    elseif package.config:sub(1,1) == '/' then
        os_type = 'unix'
    end
    return os_type
end

is_windows

c 复制代码
local function is_windows()
    if get_os_type() == 'windows' then
        return true
    end
    return false
end

get_file_name

c 复制代码
local function get_file_name(response)
    local _file_name = ngx.re.match(response, '(.+)filename="(.+)"(.*)')
    if _file_name then 
        return _file_name[2]
    end
end

get_timestamp

c 复制代码
local function get_timestamp_ss()
    local _timestamp = string.format('%d', os.time())
    return _timestamp
end
local function get_timestamp_sss()
    local _timestamp = string.format('%.3f', ngx.now())
    return _timestamp
end
local function get_timestamp_linux()
    local _pipe = io.popen('linux_date "+%s%3N"')
    local _timestamp = _pipe:read("*a")
    _pipe:close()
    _timestamp = _timestamp:gsub('\r\n$', '')
    _timestamp = _timestamp:gsub('\n$', '')
    return _timestamp
end
local function get_timestamp()
    return get_timestamp_sss()
end

get_file_path

c 复制代码
local function assert_path_file(filePath)
    local _file_path = filePath
    if (is_windows()) then
        _file_path = string.gsub(_file_path, '/', '\\')
    end
    _status = os.execute('if not exist ' .. _file_path .. ' mkdir ' .. _file_path)
end
local function get_file_ext(fileName)
    local _file_ext = string.match(fileName, '(%.[^.]+)$')
    return _file_ext
end
local function write_txt_file(filePath, context)
    local _txt_file = nil
    _txt_file = io.open(filePath, 'w+')
    if _txt_file then
        _txt_file:write(context)
        _txt_file:close()
    end
    _txt_file = nil
end
local function get_file_path(uploadFile)
    local _status = false
    local _file_path = ngx.var.client_upload_dir
    local timestamp = os.time()

    assert_path_file(_file_path)

    local dateTable = os.date('*t', timestamp)

    _file_path = string.format('%s/%s', _file_path, string.format('%04d', dateTable.year))
    assert_path_file(_file_path)

    _file_path = string.format('%s/%s', _file_path, string.format('%02d', dateTable.month))
    assert_path_file(_file_path)

    _file_path = string.format('%s/%s', _file_path, string.format('%02d', dateTable.day))
    assert_path_file(_file_path)

    _file_path = string.format('%s/%s', _file_path, string.format('%02d', dateTable.hour))
    assert_path_file(_file_path)

    _file_path = string.format('%s/%s', _file_path, uploadFile)
    if (is_windows()) then
        _file_path = string.gsub(_file_path, '/', '\\')
    end

    return _file_path
end

main

c 复制代码
local i = 0
local message = ''
while true do
    local form_type, form_response, form_error = form:read()
    if not form_type then
        ngx.say('failed to read: ', form_error)
        return
    end
    if form_type == 'header' then
        if form_response[1] ~= 'Content-Type' then
            file_name = get_file_name(form_response[2])
            if file_name then
                i = i + 1

                file_path = get_file_path(file_name)

                file = io.open(file_path, 'wb+')
                if not file then
                    message = string.format('{"code":1,"msg":"%s"}', 'Failed to open file')
                    ngx.say(message)
                    return
                end
            else
            end
        end
    elseif form_type == 'body' then
        if file then
            file_length = file_length + tonumber(string.len(form_response))    
            file:write(form_response)
        else
        end
    elseif form_type == 'part_end' then
        if file then
            file:close()
            file = nil

            local _upload_dir_length = string.len(ngx.var.client_upload_dir) + 1
            _file_path = '/upload' .. string.sub(file_path, _upload_dir_length)
            if (is_windows()) then
                _file_path = string.gsub(_file_path, '\\', '/')
            end
        
            message = string.format('{"code":0,"msg":"%s","data":{"path":"%s"}}', 'success', _file_path)
            ngx.say(message)
        end
    elseif form_type == 'eof' then
        break
    else
    end
end
if (i == 0) then
    message = string.format('{"code":1,"msg":"%s"}', 'Please upload at least one file!')
    ngx.say(message)
    return
end

conf

c 复制代码
location /api/sys/now {
    content_by_lua_block {
		ngx.header.content_type = 'text/html; charset=utf-8'
	    local result = string.format('{"code":0,"msg":"success","data":{"timestamp":"%s","now":"%s"}}', string.format('%.3f', ngx.now()), os.date("%Y-%m-%d %H:%M:%S"))
		ngx.say(result)
    }
}
c 复制代码
D:\msys64\home\unix2linux\lua2>curl "http://localhost:9999/api/sys/now" -s | jq
{
	"code": 0,
	"msg": "success",
	"data": {
		"timestamp": "1732241439.300",
		"now": "2024-11-22 10:10:39"
	}
}

demo

c 复制代码
D:\msys64\home\unix2linux\lua2>curl -X POST -F"file=@lua.png" "http://localhost:9999/api/file/upload"
{"code":0,"msg":"success","data":{"source":"lua.png","path":"/upload/2024/11/22/09/1732240131.092.png"}}

D:\msys64\home\unix2linux\lua2>curl http://localhost:9999/upload/2024/11/22/09/1732240131.092.png.txt
lua.png
D:\msys64\home\unix2linux\lua2>
相关推荐
黄名富1 分钟前
Redis 附加功能(二)— 自动过期、流水线与事务及Lua脚本
java·数据库·redis·lua
m0_748250745 分钟前
高性能Web网关:OpenResty 基础讲解
前端·openresty
KeepThinking!1 小时前
YOLO-World:Real-Time Open-Vocabulary Object Detection
人工智能·yolo·目标检测·多模态
染指11104 小时前
50.第二阶段x86游戏实战2-lua获取本地寻路,跨地图寻路和获取当前地图id
c++·windows·lua·游戏安全·反游戏外挂·游戏逆向·luastudio
前网易架构师-高司机4 小时前
游泳溺水识别数据集,对9984张原始图片进行YOLO,COCO JSON, VOC XML 格式的标注,平均识别率在91.7%以上
yolo·溺水·游泳溺水·游泳安全
发呆小天才O.oᯅ5 小时前
YOLOv8目标检测——详细记录使用OpenCV的DNN模块进行推理部署C++实现
c++·图像处理·人工智能·opencv·yolo·目标检测·dnn
深度学习lover6 小时前
<项目代码>YOLO Visdrone航拍目标识别<目标检测>
python·yolo·目标检测·计算机视觉·visdrone航拍目标识别
spencer_tseng13 小时前
WeakAuras NES Script(lua)
lua·wow·nes·weakauras
深度学习lover16 小时前
[项目代码] YOLOv8 遥感航拍飞机和船舶识别 [目标检测]
python·yolo·目标检测·计算机视觉·遥感航拍飞机和船舶识别
学习BigData18 小时前
【使用PyQt5和YOLOv11开发电脑屏幕区域的实时分类GUI】——选择检测区域
qt·yolo·分类