美文网首页
关于Buffer 和 stream 的转换,以及tus使用流上传

关于Buffer 和 stream 的转换,以及tus使用流上传

作者: 申_9a33 | 来源:发表于2021-12-19 16:39 被阅读0次

    写electron 遇到一个问题,在主进程中需要使用tus协议上传base64图片到服务器中,可以使用File,Blob,stream,但是node 中没有定义File,Blob,所以方案只剩下stream

    具体思路就是一个将base64转换为Buffer,然后创建一个Readable,重新定义内部的_read,开始撸代码

    const {Upload} = require('tus-js-client')
    const { Readable } = require('stream')
    // const fs = require('fs')
    
    const b64 = 'iVBORw0KGgoAAAANSUhEUgAAABcAAAAUCAMAAABRYFY8AAABvFBMVEUAAAAA//+A//9V//9t2/9g3/9q1f9i2P9b2/9m3f9k3v9m1v9i2P9e2f9h3P9e3f9j1v9g1/di2vhf2/hi2Phg2flk2vlh2/lf2/9j3Ple2Plk2P9j2fpj2vth2/ti3Ptg3Pti2fxg2fxj2vxh2vxi2/xg2fxg2/xh2fxg2fxg2vxi2/xh2/xi2fxh2fxg2/pi2/ph2fpg2fpi2vph2vpi2vph2/pg2/pi2fph2fpg2vph2vpi2/pg2/tf2vtg2v1i2Pth2/ti2fth2/tg2vti2vxh2vxi2vxg2/xh2/xg2fxg2/xi2/xh2/pi2fpi2/pi2/xh2fph2/xh2vpi2vph2/pi2/th2vtg2vtg2/th2fth2vtg2fth2fth2vth2vth2vti2vth2vtg2vti2vth2/th2/tg2vth2vth2fth2/th2vth2vth2vth2vth2/th2vth2vtg2vth2/th2vth2vth2vxh2vxg2vxh2fph2fxi2fph2vxi2vxh2vth2fth2vtg2vti2vth2fth2vth2vth2vxg2vth2fth2vxh2vti2vth2vth2vth2vxh2vxh2vth2/th2vv////4XwlDAAAAknRSTlMAAQIDBwgMDQ4PFxkaGx0eHyAiIycoKSorLC4uNj4/QUhJSktMTlBVV1haW1xeX2JjZGVmZ2hpamtsbW5went9fX5/gY+SmZmam5ycnKGioqKjo6WnqKqrrKytrq+wsbKztLW8vL2+wcPFxcbHyMrLz9DR0tPU3N3e4ODg4uPk5eXm5ufo6enq6+vt7vP09PX9/WqOKMQAAAABYktHRJPhA9+2AAABT0lEQVQY02NgAAGdhu4QMSAtHtrdoM0AB9wtQuxWzaYM5s3mbEKtXHBxLV8gwV+cWcgHpIM14OIWLiDSuc8RRLmawMU1A4GEWplEuSpIvTpcXKqYT9mhJ8AtqMdeha9UEiYs7z2xJrYqwdTYNLE6rnaCjzxYVDy3wDzKUDOPGchmztM0DjMvyBEHCjcCHayUWSHDGt0bwSJTkaXAwKjTKMrgZQZUx9SRzGA0adIkQ4bUNiaQAz0ZvCxA4p0pDIZAcQOGtHaQuKUng1ijLiODckaFDEtEbzjIHEUGRj2gOQxi2UWWMfpQe/M1jCKtijJFwS6S85hYH1+daG5snlSZ0DTBXRburyJeRbsef7fAHltFnhJJ1HBQLZeuUEENB0i4OfXboYWblh+QECxNLxZADWdgvHDaNBszmDRbcwgjxQuDdl1XoAiQFg3uqtMCiwAAuedLYTKQiTIAAAAASUVORK5CYII='
    const b64Buf = Buffer.from(b64,"base64")
    
    class MyReadable extends Readable {
        
        constructor(options) {
          // Calls the stream.Readable(options) constructor
          super(options);
    
          this.b64Count = 0
        }
        _read(size) {
            const end = this.b64Count + size
            const buf = b64Buf.subarray(this.b64Count,end)
    
            console.log(this.b64Count,buf.length,size,'_read')
            if(buf.length !== 0){
                this.push(buf)
            }else{
                this.push(null)
            }
    
            this.b64Count = end
        }
    }
    
    const rs = new MyReadable()
    
    // const writer = fs.createWriteStream('./test/test.text')
    // rs.pipe(writer)
    
    
    var options = {
        endpoint: 'http://localhost:8080/files/files/',
        metadata: {
          filename: 'test.png',
          filetype: 'image/png',
        },
        chunkSize: 16384,
        uploadSize: b64Buf.length,
        onError (error) {
          throw error
        },
        onProgress (bytesUploaded, bytesTotal) {
          var percentage = (bytesUploaded / bytesTotal * 100).toFixed(2)
          console.log(bytesUploaded, bytesTotal, `${percentage}%`)
        },
        onSuccess () {
          console.log('Upload finished:', upload.url)
        },
      }
    
      var upload = new Upload(rs, options)
      upload.start()
    
    

    相关文章

      网友评论

          本文标题:关于Buffer 和 stream 的转换,以及tus使用流上传

          本文链接:https://www.haomeiwen.com/subject/fzrofrtx.html