vedio-upload.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. function UserFactory(name, file, client, res, callback) {
  2. function OSS() {
  3. var suffix = file.name.substr(file.name.indexOf("."));
  4. var obj = uploadName();
  5. var storeAs = obj + suffix;
  6. client.multipartUpload(storeAs, file).then(function (result) {
  7. var url = result.res.requestUrls[0];
  8. var length = url.lastIndexOf('?');
  9. var imgUrl = url.substr(0, length);
  10. callback(imgUrl, file, 3);
  11. }).catch(function (err) {
  12. console.log(err);
  13. });
  14. }
  15. function COS() {
  16. var selectedFile = file;
  17. filename = selectedFile.name;
  18. filename = uploadName(filename);
  19. client.putObject({
  20. Bucket: res.bucket,
  21. Region: res.region,
  22. Key: filename,
  23. StorageClass: 'STANDARD',
  24. Body: selectedFile,
  25. }, function (err, data) {
  26. callback("http://" + data.Location, file, 4);
  27. });
  28. }
  29. function QINIU() {
  30. var files = file;
  31. var key = uploadName();
  32. var token = client; //从服务器拿的并存在本地data里
  33. var putExtra = {
  34. fname: '',
  35. params: {},
  36. mimeType: null,
  37. };
  38. var config = {
  39. useCdnDomain: true, //使用cdn加速
  40. };
  41. var observable = qiniu.upload(files, key, token, putExtra, config);
  42. observable.subscribe({
  43. next: (result) => {
  44. // 主要用来展示进度
  45. console.warn(result);
  46. },
  47. error: (err) => {
  48. console.log(err);
  49. },
  50. complete: (re) => {
  51. callback("http://" + res.region + "/" + re.key, file, 2);
  52. },
  53. });
  54. }
  55. function uploadName() {
  56. const data =
  57. ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
  58. "s", "t", "u", "v", "w", "x", "y", "z"];
  59. let nums = "";
  60. for (let i = 0; i < 32; i++) {
  61. const r = parseInt(Math.random() * 35);
  62. nums += data[r];
  63. }
  64. return nums;
  65. }
  66. switch (name) {
  67. case 'OSS':
  68. return new OSS();
  69. break;
  70. case 'COS':
  71. return new COS();
  72. break;
  73. case 'QINIU':
  74. return new QINIU();
  75. break;
  76. }
  77. }