上传插件,不包含上传的前端实现,只提供后端接口等,其他地方接入插件上传。包括上传进度、断点续传等

index.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { getFilemd5sum } from './testutils'
  2. const IMAGE_MULTIPART = '/multipart/upload/'
  3. const VIDEO_MULTIPART = '/upload/'
  4. const IMAGE_TYPE_ERROR = 101
  5. const VIDOE_TYPE_ERROR = 102
  6. const UPLOAD_SUCCESS_CODE = 200
  7. const UPLOAD_FAILED_CODE = 400
  8. function FileFactory(file) {
  9. this.offset = 0; //用于断点续传,默认为 0
  10. this.BYTES_PER_CHUNK = 1024 * 1024
  11. this.file = file
  12. this.fileSize = file.size
  13. this.fileType = (file.name.split('.')[1]).toLowerCase()
  14. this.chunkNum = this.BYTES_PER_CHUNK > this.fileSize ? Math.ceil(this.fileSize / this.BYTES_PER_CHUNK):1
  15. this.chunkSize = this.BYTES_PER_CHUNK > this.fileSize ? this.fileSize:this.BYTES_PER_CHUNK
  16. }
  17. FileFactory.prototype.setOffset = function(offset) {
  18. this.offset = offset
  19. }
  20. export default function UploadSdk(host, origin, token, file) {
  21. this.host = host
  22. this.origin = origin
  23. this.token = token
  24. this.file = file
  25. this.generateMd5 = function (callback) {
  26. getFilemd5sum(this.file).then(result => {callback(result)})
  27. }
  28. this.imageUploadAction = function () {
  29. return new Promise( (resolve, reject) => {
  30. let fileFactory = new FileFactory(this.file)
  31. if( !fileFactory.fileType.match('[(jpg)|(png)|(gif)]+$')) {
  32. reject(IMAGE_TYPE_ERROR)
  33. }
  34. this.generateMd5( (md5) => {
  35. this.md5 = md5
  36. this.postImage(this.md5,fileFactory).then( res => {
  37. resolve(res)
  38. }).catch( (err) => {
  39. reject(err)
  40. })
  41. })
  42. })
  43. }
  44. this.videoUploadAction = function () {
  45. return new Promise( (resolve, reject) => {
  46. if( !this.file.type.match('[(mp4)|(rmvb)|(flv)|(mpeg)|(avi)]+$')) {
  47. reject(VIDOE_TYPE_ERROR)
  48. }
  49. this.postVideo().then( res => {
  50. resolve(res)
  51. }).catch( (err) => {
  52. reject(err)
  53. })
  54. })
  55. }
  56. this.postVideo = function() {
  57. return new Promise( (resolve, reject) => {
  58. let xhr = new XMLHttpRequest()
  59. xhr.open('POST', 'http:'+this.host+VIDEO_MULTIPART+this.origin, true)
  60. xhr.withCredentials = true;
  61. xhr.setRequestHeader('Authorization', `Bearer ${this.token}`)
  62. xhr.setRequestHeader('Content-type','application/json')
  63. let fd = new FormData()
  64. fd.append('file',this.file)
  65. fd.append('code_rate','ld,sd,hd')
  66. xhr.send(fd)
  67. xhr.onreadystatechange = function () {
  68. if (xhr.readyState === 4) {
  69. let obj = JSON.parse(xhr.responseText)
  70. if (xhr.status === 304 || (xhr.status >= 200 && xhr.status < 300)) {
  71. resolve({
  72. code: UPLOAD_SUCCESS_CODE,
  73. url: obj.url,
  74. msg: 'success'
  75. })
  76. } else {
  77. reject({
  78. code: UPLOAD_FAILED_CODE,
  79. url:'',
  80. msg:obj.message
  81. })
  82. }
  83. }
  84. }
  85. })
  86. }
  87. this.postImage = function(md5,fileFactory) {
  88. const self = this
  89. return new Promise((resolve, reject) => {
  90. let xhr = new XMLHttpRequest()
  91. xhr.open('POSt', 'http:'+this.host+IMAGE_MULTIPART+this.origin, true)
  92. xhr.withCredentials = true
  93. xhr.setRequestHeader('Authorization', `Bearer ${this.token}`)
  94. xhr.setRequestHeader('X-Upload-File-Size',fileFactory.fileSize)
  95. xhr.setRequestHeader('X-Upload-Chunk-Index',1+'')
  96. xhr.setRequestHeader('X-Upload-Chunk-Num',fileFactory.chunkNum)
  97. xhr.setRequestHeader('X-Upload-Chunk-Size',fileFactory.chunkSize)
  98. xhr.setRequestHeader('X-Upload-Offset',fileFactory.offset+'')
  99. xhr.setRequestHeader('X-Upload-File-Md5',md5)
  100. xhr.setRequestHeader('X-Upload-File-Type',fileFactory.fileType)
  101. let fd = new FormData()
  102. fd.append('file',this.file)
  103. xhr.send(fd)
  104. xhr.onreadystatechange = function () {
  105. if (xhr.readyState === 4) {
  106. let obj = JSON.parse(xhr.responseText)
  107. if (xhr.status === 304 || (xhr.status >= 200 && xhr.status < 300)) {
  108. if(obj.status === 1) {
  109. resolve({
  110. code: UPLOAD_SUCCESS_CODE,
  111. url: obj.url,
  112. msg: 'success'
  113. })
  114. } else if (obj.status === 0) {
  115. fileFactory.setOffset(obj.msg)
  116. this.getAllResponseHeaders.postImage(md5,fileFactory)
  117. }
  118. } else {
  119. reject({
  120. code: UPLOAD_FAILED_CODE,
  121. url:'',
  122. msg: obj.message
  123. })
  124. }
  125. }
  126. }
  127. })
  128. }
  129. }