oss-file-upload.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import COS from "cos-js-sdk-v5"
  2. import { getUploadSignApi } from "@/api/user"
  3. import { httpAjax } from "@/plugins/httpAjax"
  4. import loadingBar from "@/plugins/loadingBar"
  5. const tencentBucket = "daya-online-1303457149"
  6. const ossType = "tencent"
  7. export default async function fileUpload(
  8. fileName: string,
  9. file: Blob,
  10. filePath: string,
  11. isAddTimestamp = true,
  12. loadingBarOpt?: { isLoading: boolean; text?: string }
  13. ) {
  14. loadingBarOpt || (loadingBarOpt = { isLoading: true })
  15. loadingBarOpt.isLoading && loadingBar.loading(true, loadingBarOpt.text || "资源上传中")
  16. // 上传名称加上时间戳
  17. if (isAddTimestamp) {
  18. fileName = addTimestampBeforeExtension(fileName)
  19. }
  20. const resUploadSign = await getUploadSign(fileName, filePath)
  21. if (resUploadSign.code === 200) {
  22. try {
  23. const resUpload = await onOnlyFileUpload(resUploadSign.data.signature, {
  24. fileName,
  25. file,
  26. filePath
  27. })
  28. loadingBarOpt.isLoading && loadingBar.loading(false)
  29. if (resUpload.statusCode === 200) {
  30. return resUpload.Location.includes("http") ? resUpload.Location : `https://${resUpload.Location}`
  31. } else {
  32. return Promise.reject("")
  33. }
  34. } catch {
  35. loadingBarOpt.isLoading && loadingBar.loading(false)
  36. return Promise.reject("")
  37. }
  38. } else {
  39. loadingBarOpt.isLoading && loadingBar.loading(false)
  40. return Promise.reject("")
  41. }
  42. }
  43. const getUploadSign = async (fileName: string, filePath: string) => {
  44. const fileUrl = `pptList/${filePath || "errPath/"}` + fileName
  45. return httpAjax(
  46. getUploadSignApi,
  47. {
  48. postData: {
  49. key: fileUrl
  50. },
  51. pluginName: ossType,
  52. bucketName: tencentBucket,
  53. filename: fileUrl
  54. },
  55. { pluginName: ossType }
  56. )
  57. }
  58. const onOnlyFileUpload = (signature: string, params: { fileName: string; file: Blob; filePath: string }) => {
  59. const cos = new COS({
  60. Domain: "https://oss.dayaedu.com",
  61. Protocol: "https",
  62. getAuthorization: async (options, callback: any) => {
  63. callback({ Authorization: signature })
  64. }
  65. })
  66. return cos.uploadFile({
  67. Bucket: tencentBucket /* 填写自己的 bucket,必须字段 */,
  68. Region: "ap-nanjing" /* 存储桶所在地域,必须字段 */,
  69. Key: `pptList/${params.filePath || "errPath/"}${params.fileName}`,
  70. /* 存储在桶里的对象键(例如:1.jpg,a/b/test.txt,图片.jpg)支持中文,必须字段 */
  71. Body: params.file, // 上传文件对象
  72. SliceSize: 1024 * 1024 * 500 /* 触发分块上传的阈值,超过5MB使用分块上传,小于5MB使用简单上传。可自行设置,非必须 */,
  73. onProgress: function (progressData) {
  74. // onProgress({ percent: Math.ceil((progressData.percent || 0) * 100) })
  75. }
  76. })
  77. }
  78. function addTimestampBeforeExtension(filename: string) {
  79. const dotIndex = filename.lastIndexOf(".")
  80. const name = filename.substring(0, dotIndex)
  81. const extension = filename.substring(dotIndex)
  82. return `${name}_${Date.now()}${extension}`
  83. }