upload.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {
  2. toast
  3. } from "../utils/utils";
  4. import dayjs from "dayjs";
  5. import Api from "./api";
  6. const COS = require('../utils/cos-wx-sdk-v5')
  7. const Bucket = 'college-file-1302740389';
  8. const Region = 'ap-beijing';
  9. class Upload {
  10. static getCos() {
  11. return new COS({
  12. // 必选参数
  13. async getAuthorization(options, callback) {
  14. const res = await Api.getCosInfo();
  15. callback({
  16. TmpSecretId: res.data.credentials.tmpSecretId,
  17. TmpSecretKey: res.data.credentials.tmpSecretKey,
  18. XCosSecurityToken: res.data.credentials.sessionToken,
  19. StartTime: res.data.startTime,
  20. ExpiredTime: res.data.expiredTime,
  21. });
  22. }
  23. });
  24. }
  25. static async uploadRes(uploadPath = '', file) {
  26. let originalName = file.name || '';
  27. let filePath = file.path || "";
  28. let pathName = filePath ? filePath.substr(filePath.indexOf('//tmp/') + 6) : file.url.substr(file.url.lastIndexOf('/') + 1);
  29. //获取文件后缀(包含.)
  30. let ext = pathName.substr(pathName.lastIndexOf('.'));
  31. //文件原始名称
  32. let fileName = originalName.split(ext)[0];
  33. // 对象键的最大长度为850个字节(按照一个汉字两个字节计算,如果原始文件名超过200个汉字就截断)
  34. fileName = fileName.length > 200? fileName.substr(0, 200) : fileName;
  35. //时间戳
  36. let timestamp = dayjs().valueOf();
  37. //文件名称:文件原始名称 + 时间戳 + 文件在本地的临时路径
  38. let name = `${fileName? fileName + '_' : ''}${timestamp}_${pathName}`;
  39. // if (file.type == 'image') {
  40. // name = dayjs().valueOf() + '.png';
  41. // }
  42. // if (file.type == 'video') {
  43. // name = dayjs().valueOf() + '.mp4';
  44. // }
  45. // if (file.name) {
  46. // name = file.name;
  47. // }
  48. const cos = this.getCos();
  49. // name = name || file.url.substr(file.url.lastIndexOf('/') + 1);
  50. return new Promise((resolve, reject) => {
  51. cos.postObject({
  52. Bucket: Bucket,
  53. Region: Region,
  54. Key: uploadPath + name,
  55. FilePath: file.url
  56. }, async function (err, data) {
  57. if (!err) {
  58. // if (data.headers.location) {
  59. // file.url = data.headers.location;
  60. // } else {
  61. // file.url = `https://${data.Location}`;
  62. // }
  63. if(!data.Location) {
  64. let uploadErr = '上传失败!';
  65. toast(uploadErr);
  66. reject(uploadErr);
  67. return;
  68. }
  69. // console.log(data.Location);
  70. file.url = `https://${data.Location}`;
  71. file.fileExtension = file.url.substr(file.url.lastIndexOf('.') + 1);
  72. resolve()
  73. } else {
  74. toast(err.error.Message)
  75. reject(err.error.Message)
  76. }
  77. });
  78. });
  79. }
  80. static async deleteRes(uploadPath = '', url) {
  81. return new Promise((resolve, reject) => {
  82. const cos = this.getCos();
  83. const name = url.substr(url.lastIndexOf('/') + 1);
  84. cos.deleteObject({
  85. Bucket: Bucket,
  86. Region: Region,
  87. Key: uploadPath + name,
  88. }, function (err, data) {
  89. if (!err) {
  90. resolve()
  91. } else {
  92. toast(err.msg)
  93. }
  94. });
  95. });
  96. }
  97. }
  98. export default Upload