cos.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. local tencent_cos = require("tencent_cos")
  2. local fw = require("fastweb")
  3. local utils = require("app.utils")
  4. local config = require("app.config")
  5. local M = {}
  6. -- 文件扩展名与目录的映射
  7. M.ext_dirpath = {
  8. images = {
  9. "jpg",
  10. "jpeg",
  11. "png",
  12. "gif",
  13. "bmp",
  14. "webp",
  15. "svg",
  16. "ico",
  17. "tif",
  18. "tiff",
  19. },
  20. videos = {
  21. "mp4",
  22. "avi",
  23. "mov",
  24. "wmv",
  25. "flv"
  26. },
  27. audio = {
  28. "mp3",
  29. "wav",
  30. "ogg",
  31. "aac",
  32. "m4a",
  33. "wma"
  34. },
  35. documents = {
  36. "pdf",
  37. "doc",
  38. "docx",
  39. "xls",
  40. "xlsx",
  41. "ppt",
  42. "pptx"
  43. }
  44. }
  45. function M.upload_data(config,object_name,remote_dir,data,ext)
  46. local filename = fw.make_software_guid().."."..ext
  47. local dirpath = ""
  48. for dir, ext_list in pairs(M.ext_dirpath) do
  49. for _, ext_type_ext in ipairs(ext_list) do
  50. if ext_type_ext == ext then
  51. dirpath = dir
  52. break
  53. end
  54. end
  55. if dirpath ~= "" then
  56. break
  57. end
  58. end
  59. if dirpath == "" then
  60. return false,"不支持的文件格式,ext: "..ext
  61. end
  62. if remote_dir == nil or remote_dir == "" then
  63. remote_dir = ""
  64. else
  65. remote_dir = remote_dir.."/"
  66. end
  67. local cos = tencent_cos.new()
  68. local local_file = fw.website_dir()..config.path.temp.."/"..filename
  69. utils.save_file(local_file,data)
  70. local cos_filepath = remote_dir..dirpath.."/"..filename
  71. local result = cos:upfile(
  72. config.appid,
  73. config.endpoint,
  74. config.secret_id,
  75. config.secret_key,
  76. object_name,
  77. cos_filepath,
  78. local_file
  79. )
  80. if result == "" then
  81. return true,cos_filepath
  82. end
  83. return false,result
  84. end
  85. function M.upload_file(config,object_name,remote_dir,local_filepath,auto_remove)
  86. if not utils.exists_file(local_filepath) then
  87. return false,"文件不存在,local_filepath: "..local_filepath
  88. end
  89. local ext = utils.ext(local_filepath)
  90. local filename = fw.make_software_guid().."."..ext
  91. local dirpath = ""
  92. for dir, ext_list in pairs(M.ext_dirpath) do
  93. for _, ext_type_ext in ipairs(ext_list) do
  94. if ext_type_ext == ext then
  95. dirpath = dir
  96. break
  97. end
  98. end
  99. if dirpath ~= "" then
  100. break
  101. end
  102. end
  103. if dirpath == "" then
  104. return false,"不支持的文件格式,ext: "..ext
  105. end
  106. if remote_dir == nil or remote_dir == "" then
  107. remote_dir = ""
  108. else
  109. remote_dir = remote_dir.."/"
  110. end
  111. local cos_filepath = remote_dir .. dirpath.."/"..filename
  112. local cos = tencent_cos.new()
  113. local result = cos:upfile(
  114. config.appid,
  115. config.endpoint,
  116. config.secret_id,
  117. config.secret_key,
  118. object_name,
  119. cos_filepath,
  120. local_filepath
  121. )
  122. if result == "" then
  123. if auto_remove then
  124. utils.delete_file(local_filepath)
  125. end
  126. return true,cos_filepath
  127. end
  128. return false,result
  129. end
  130. return M