cos.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. local tencent_cos = require("tencent_cos")
  2. local fw = require("fastweb")
  3. local utils = require("utils")
  4. local M = {}
  5. -- 文件扩展名与目录的映射
  6. M.ext_dirpath = {
  7. images = {
  8. "jpg",
  9. "jpeg",
  10. "png",
  11. "gif",
  12. "bmp",
  13. "webp",
  14. "svg",
  15. "ico",
  16. "tif",
  17. "tiff",
  18. },
  19. videos = {
  20. "mp4",
  21. "avi",
  22. "mov",
  23. "wmv",
  24. "flv"
  25. },
  26. audio = {
  27. "mp3",
  28. "wav",
  29. "ogg",
  30. "aac",
  31. "m4a",
  32. "wma"
  33. },
  34. documents = {
  35. "pdf",
  36. "doc",
  37. "docx",
  38. "xls",
  39. "xlsx",
  40. "ppt",
  41. "pptx"
  42. }
  43. }
  44. function M.upload_data(config,remote_dir,data,ext)
  45. local filename = fw.make_software_guid().."."..ext
  46. local dirpath = ""
  47. for dir, ext_list in pairs(M.ext_dirpath) do
  48. for _, ext_type_ext in ipairs(ext_list) do
  49. if ext_type_ext == ext then
  50. dirpath = dir
  51. break
  52. end
  53. end
  54. if dirpath ~= "" then
  55. break
  56. end
  57. end
  58. if dirpath == "" then
  59. return false,"不支持的文件格式,ext: "..ext
  60. end
  61. if remote_dir == nil or remote_dir == "" then
  62. remote_dir = ""
  63. else
  64. remote_dir = remote_dir.."/"
  65. end
  66. local cos = tencent_cos.new()
  67. local cos_filepath = remote_dir..dirpath.."/"..filename
  68. local result = nil
  69. for _, cfg in ipairs(config) do
  70. result = cos:updata(
  71. cfg.appid,
  72. cfg.endpoint,
  73. cfg.secret_id,
  74. cfg.secret_key,
  75. cfg.bucket_name,
  76. cos_filepath,
  77. data
  78. )
  79. if result ~= "" then
  80. -- 如果不是第一个失败,那么就循环删除之前已上传的文件
  81. if _ > 1 then
  82. for i = 1, _ - 1 do
  83. local prev_cfg = config[i]
  84. cos:del(
  85. prev_cfg.appid,
  86. prev_cfg.endpoint,
  87. prev_cfg.secret_id,
  88. prev_cfg.secret_key,
  89. prev_cfg.bucket_name,
  90. cos_filepath
  91. )
  92. end
  93. end
  94. break
  95. end
  96. end
  97. if result == "" then
  98. return true,cos_filepath
  99. end
  100. return false,result
  101. end
  102. function M.upload_file(config,remote_dir,local_filepath,auto_remove)
  103. if not utils.exists_file(local_filepath) then
  104. return false,"文件不存在,local_filepath: "..local_filepath
  105. end
  106. local ext = utils.ext(local_filepath)
  107. local filename = fw.make_software_guid().."."..ext
  108. local dirpath = ""
  109. for dir, ext_list in pairs(M.ext_dirpath) do
  110. for _, ext_type_ext in ipairs(ext_list) do
  111. if ext_type_ext == ext then
  112. dirpath = dir
  113. break
  114. end
  115. end
  116. if dirpath ~= "" then
  117. break
  118. end
  119. end
  120. if dirpath == "" then
  121. return false,"不支持的文件格式,ext: "..ext
  122. end
  123. if remote_dir == nil or remote_dir == "" then
  124. remote_dir = ""
  125. else
  126. remote_dir = remote_dir.."/"
  127. end
  128. local cos_filepath = remote_dir .. dirpath.."/"..filename
  129. local cos = tencent_cos.new()
  130. local result = nil
  131. for _, cfg in ipairs(config) do
  132. result = cos:upfile(
  133. cfg.appid,
  134. cfg.endpoint,
  135. cfg.secret_id,
  136. cfg.secret_key,
  137. cfg.bucket_name,
  138. cos_filepath,
  139. local_filepath
  140. )
  141. if result ~= "" then
  142. -- 如果不是第一个失败,那么就循环删除之前已上传的文件
  143. if _ > 1 then
  144. for i = 1, _ - 1 do
  145. local prev_cfg = config[i]
  146. -- 删除之前上传的文件
  147. cos:del(
  148. prev_cfg.appid,
  149. prev_cfg.endpoint,
  150. prev_cfg.secret_id,
  151. prev_cfg.secret_key,
  152. prev_cfg.bucket_name,
  153. cos_filepath
  154. )
  155. end
  156. end
  157. break
  158. end
  159. end
  160. if result == "" then
  161. if auto_remove then
  162. utils.delete_file(local_filepath)
  163. end
  164. return true,cos_filepath
  165. end
  166. return false,result
  167. end
  168. return M