1

I have the class below based on the code found at https://github.com/transloadit/uppy-vimeo-thing/blob/master/Vimeo.js.

Resuming an upload doesn't work, it always restarts upload from scratch. If I remove uploadUrl from the Tus options and set the endpoint value to https://master.tus.io/files/ (thus not uploading to Vimeo), resuming an upload works as expected. This issue only happens when sending uploads to Vimeo.

Looking at the Network tab of my Developers Tools, I see that upload-offset is always 0 when sending the PATCH request to Vimeo, even when resuming it (subsequent requests).

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

Michael
  • 95
  • 1
  • 5
  • same is happening to my hosted tus server, uploading is restarting from 0, have you found anything related to this issue? – medBouzid Jan 16 '21 at 13:33

1 Answers1

0

See this comment it will solve it I had to set the chunkSize variable within my 'new tus.Upload()'. By default, chunkSize is infinite, and it seems Vimeo's 'upload-offset' response is only for completed chunks, and so there's no completed chunks until the upload has completed. By setting it to 5000000 (5mb), I'm able to pause and resume the upload successfully. Upon resuming, it restarts at the last completed chunk.

Mohamed ِRadwan
  • 477
  • 2
  • 5
  • 15