| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- local tencent_cos = require("tencent_cos")
- local fw = require("fastweb")
- local utils = require("utils")
- local M = {}
- -- 文件扩展名与目录的映射
- M.ext_dirpath = {
- images = {
- "jpg",
- "jpeg",
- "png",
- "gif",
- "bmp",
- "webp",
- "svg",
- "ico",
- "tif",
- "tiff",
- },
- videos = {
- "mp4",
- "avi",
- "mov",
- "wmv",
- "flv"
- },
- audio = {
- "mp3",
- "wav",
- "ogg",
- "aac",
- "m4a",
- "wma"
- },
- documents = {
- "pdf",
- "doc",
- "docx",
- "xls",
- "xlsx",
- "ppt",
- "pptx"
- }
- }
- function M.upload_data(config,remote_dir,data,ext)
- local filename = fw.make_software_guid().."."..ext
- local dirpath = ""
-
- for dir, ext_list in pairs(M.ext_dirpath) do
- for _, ext_type_ext in ipairs(ext_list) do
- if ext_type_ext == ext then
- dirpath = dir
- break
- end
- end
- if dirpath ~= "" then
- break
- end
- end
- if dirpath == "" then
- return false,"不支持的文件格式,ext: "..ext
- end
- if remote_dir == nil or remote_dir == "" then
- remote_dir = ""
- else
- remote_dir = remote_dir.."/"
- end
- local cos = tencent_cos.new()
- local cos_filepath = remote_dir..dirpath.."/"..filename
- local result = nil
- for _, cfg in ipairs(config) do
- result = cos:updata(
- cfg.appid,
- cfg.endpoint,
- cfg.secret_id,
- cfg.secret_key,
- cfg.bucket_name,
- cos_filepath,
- data
- )
- if result ~= "" then
- -- 如果不是第一个失败,那么就循环删除之前已上传的文件
- if _ > 1 then
- for i = 1, _ - 1 do
- local prev_cfg = config[i]
- cos:del(
- prev_cfg.appid,
- prev_cfg.endpoint,
- prev_cfg.secret_id,
- prev_cfg.secret_key,
- prev_cfg.bucket_name,
- cos_filepath
- )
- end
- end
- break
- end
- end
- if result == "" then
- return true,cos_filepath
- end
- return false,result
- end
- function M.upload_file(config,remote_dir,local_filepath,auto_remove)
- if not utils.exists_file(local_filepath) then
- return false,"文件不存在,local_filepath: "..local_filepath
- end
- local ext = utils.ext(local_filepath)
- local filename = fw.make_software_guid().."."..ext
- local dirpath = ""
-
- for dir, ext_list in pairs(M.ext_dirpath) do
- for _, ext_type_ext in ipairs(ext_list) do
- if ext_type_ext == ext then
- dirpath = dir
- break
- end
- end
- if dirpath ~= "" then
- break
- end
- end
- if dirpath == "" then
- return false,"不支持的文件格式,ext: "..ext
- end
- if remote_dir == nil or remote_dir == "" then
- remote_dir = ""
- else
- remote_dir = remote_dir.."/"
- end
- local cos_filepath = remote_dir .. dirpath.."/"..filename
- local cos = tencent_cos.new()
- local result = nil
-
-
- for _, cfg in ipairs(config) do
- result = cos:upfile(
- cfg.appid,
- cfg.endpoint,
- cfg.secret_id,
- cfg.secret_key,
- cfg.bucket_name,
- cos_filepath,
- local_filepath
- )
- if result ~= "" then
- -- 如果不是第一个失败,那么就循环删除之前已上传的文件
- if _ > 1 then
- for i = 1, _ - 1 do
- local prev_cfg = config[i]
- -- 删除之前上传的文件
- cos:del(
- prev_cfg.appid,
- prev_cfg.endpoint,
- prev_cfg.secret_id,
- prev_cfg.secret_key,
- prev_cfg.bucket_name,
- cos_filepath
- )
- end
- end
- break
- end
- end
-
- if result == "" then
- if auto_remove then
- utils.delete_file(local_filepath)
- end
- return true,cos_filepath
- end
- return false,result
- end
- return M
|