上传到Vimeo的Uppy + tus无法恢复,并且始终会重新启动

时间:2020-04-16 10:18:43

标签: vimeo-api tus uppy

基于https://github.com/transloadit/uppy-vimeo-thing/blob/master/Vimeo.js上的代码,我有下面的类。

恢复上传无效,它总是从头开始重新上传。如果我从Tus选项中删除了uploadUrl,并将endpoint的值设置为https://master.tus.io/files/(因此未上传到Vimeo),则恢复上传将按预期进行。仅在将上传内容发送到Vimeo时才会发生此问题。

查看我的开发人员工具的“网络”标签,我发现将PATCH请求发送到Vimeo时,upload-offset始终是0,即使恢复(后续请求)也是如此。

const { Plugin } = require('@uppy/core')
const mapLimit = require('promise-map-limit')

const VIMEO_API_ROOT = 'https://api.vimeo.com'

class Vimeo extends Plugin {
  constructor (uppy, opts) {
    super(uppy, opts)

    this.name = 'Vimeo'
    this.id = 'Vimeo'
    this.type = 'uploader'

    this.opts = Object.assign({
      limit: 100
    }, this.opts)

    this.prepareUpload = this.prepareUpload.bind(this)
    this.afterUpload = this.afterUpload.bind(this)
  }

  async prepareUpload (fileIDs) {
    const { videoTitle } = this.opts

    fileIDs.forEach((fileID) => {
      this.uppy.emit('preprocess-progress', fileID, {
        mode: 'indeterminate',
        message: 'Creating video...'
      })
    })

    await mapLimit(fileIDs, this.opts.limit, async (fileID) => {
      const file = this.uppy.getFile(fileID)
      const response = await fetch(`${VIMEO_API_ROOT}/me/videos`, {
        method: 'post',
        headers: {
          'authorization': `Bearer ${vimeoAccessToken}`,
          'content-type': 'application/json',
          'accept': 'application/vnd.vimeo.*+json;version=3.4'
        },
        body: JSON.stringify({
          upload: {
            approach: 'tus',
            size: file.size
          },
          name: videoTitle
        })
      })

      const { upload, link, uri } = await response.json()
      this.uppy.setFileState(fileID, {
        uploadURL: link,
        vimeo: {
          link,
          id: uri.split('/').pop()
        },
        tus: Object.assign({}, file.tus, {
          endpoint: 'https://files.tus.vimeo.com/files/', // HACK this is to appease tus-js-client
          // NOTE: This is uploadUrl instead of endpoint, different from what you might expect;
          // Vimeo pre-creates the Tus upload.
          uploadUrl: upload.upload_link,
          headers: {
            'Accept': 'application/vnd.vimeo.*+json;version=3.4'
          }
        }),
      })

      this.uppy.emit('preprocess-complete', fileID)
    })
  }

  async afterUpload (fileIDs) {
    fileIDs.forEach((fileID) => {
      const file = this.uppy.getFile(fileID)
      const video = file.vimeo

      this.uppy.setFileState(fileID, {
        uploadURL: video.link
      })
    })
  }

  install () {
    this.uppy.addPreProcessor(this.prepareUpload)
    this.uppy.addPostProcessor(this.afterUpload)
  }

  uninstall () {
    this.uppy.removePreProcessor(this.prepareUpload)
    this.uppy.removePostProcessor(this.afterUpload)
  }
}

module.exports = Vimeo

1 个答案:

答案 0 :(得分:0)

看到这个comment它会解决它 我必须在我的“新 tus.Upload()”中设置 chunkSize 变量。默认情况下,chunkSize 是无限的,而且 Vimeo 的“upload-offset”响应似乎仅适用于已完成的块,因此在上传完成之前没有已完成的块。 通过将其设置为 5000000 (5mb),我可以成功地暂停和恢复上传。恢复后,它会在最后一个完成的块处重新启动。