upload.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const tencentBucket = "daya-online-1303457149"
  2. const ossType = "tencent"
  3. function fileUpload({ host, token }, { fileName, file }) {
  4. // 上传名称加上时间戳
  5. fileName = addTimestampBeforeExtension(fileName)
  6. return new Promise((res, rej) => {
  7. getUploadSign(fileName, host, token)
  8. .then(resUploadSign => {
  9. if (resUploadSign.code === 200) {
  10. try {
  11. onOnlyFileUpload(resUploadSign.data.signature, {
  12. fileName,
  13. file
  14. })
  15. .then(resUpload => {
  16. if (resUpload.statusCode === 200) {
  17. res(resUpload.Location.includes("http") ? resUpload.Location : `https://${resUpload.Location}`)
  18. } else {
  19. rej("")
  20. }
  21. })
  22. .catch(() => {
  23. rej("")
  24. })
  25. } catch {
  26. rej("")
  27. }
  28. } else {
  29. rej("")
  30. }
  31. })
  32. .catch(() => {
  33. rej("")
  34. })
  35. })
  36. }
  37. const getUploadSign = (fileName, host, token) => {
  38. const fileUrl = `pptList/${filePath || "errPath/"}` + fileName
  39. return ajaxRequest(
  40. `${host}/edu-app/open/getUploadSign?pluginName=${ossType}`,
  41. "POST",
  42. {
  43. "Content-Type": "application/json",
  44. Authorization: token
  45. },
  46. {
  47. postData: {
  48. key: fileUrl
  49. },
  50. pluginName: ossType,
  51. bucketName: tencentBucket,
  52. filename: fileUrl
  53. }
  54. )
  55. }
  56. const onOnlyFileUpload = (signature, params) => {
  57. const cos = new COS({
  58. Domain: "https://oss.dayaedu.com",
  59. Protocol: "https",
  60. getAuthorization: async (options, callback) => {
  61. callback({ Authorization: signature })
  62. }
  63. })
  64. return cos.uploadFile({
  65. Bucket: tencentBucket /* 填写自己的 bucket,必须字段 */,
  66. Region: "ap-nanjing" /* 存储桶所在地域,必须字段 */,
  67. Key: `pptList/${filePath || "errPath/"}${params.fileName}`,
  68. /* 存储在桶里的对象键(例如:1.jpg,a/b/test.txt,图片.jpg)支持中文,必须字段 */
  69. Body: params.file, // 上传文件对象
  70. SliceSize: 1024 * 1024 * 500 /* 触发分块上传的阈值,超过5MB使用分块上传,小于5MB使用简单上传。可自行设置,非必须 */,
  71. onProgress: function (progressData) {
  72. // onProgress({ percent: Math.ceil((progressData.percent || 0) * 100) })
  73. }
  74. })
  75. }
  76. function ajaxRequest(url, method = "GET", headers = {}, body = null) {
  77. return new Promise((resolve, reject) => {
  78. const xhr = new XMLHttpRequest()
  79. xhr.open(method, url, true)
  80. // 设置请求头
  81. for (const [key, value] of Object.entries(headers)) {
  82. xhr.setRequestHeader(key, value)
  83. }
  84. xhr.onload = function () {
  85. if (xhr.status >= 200 && xhr.status < 300) {
  86. resolve(JSON.parse(xhr.responseText))
  87. } else {
  88. reject(new Error(`HTTP error! status: ${xhr.status}`))
  89. }
  90. }
  91. xhr.onerror = function () {
  92. reject(new Error("Network error occurred"))
  93. }
  94. xhr.send(method === "GET" ? null : JSON.stringify(body))
  95. })
  96. }
  97. function addTimestampBeforeExtension(filename) {
  98. const dotIndex = filename.lastIndexOf(".")
  99. const name = filename.substring(0, dotIndex)
  100. const extension = filename.substring(dotIndex)
  101. return `${name}_${Date.now()}${extension}`
  102. }