Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support to 3.4 version of the api #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 32 additions & 44 deletions vimeo-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
| @link http://websemantics.ca
| @author Web Semantics, Inc. Dev Team <[email protected]>
| @author Adnan M.Sagar, PhD. <[email protected]>
| @author Sadashiv K Dalvi, <[email protected]>
| @credits Built on cors-upload-sample, https://github.com/googledrive/cors-upload-sample
*/

Expand Down Expand Up @@ -156,12 +157,12 @@
}

this.contentType = opts.contentType || this.file.type || defaults.contentType
this.httpMethod = opts.fileId ? 'PUT' : 'POST'
this.httpMethod = opts.fileId ? 'PATCH' : 'POST'

this.videoData = {
name: (opts.name > '') ? opts.name : defaults.name,
description: (opts.description > '') ? opts.description : defaults.description,
'privacy.view': opts.private ? 'nobody' : 'anybody'
'privacy': {'view': opts.private ? opts.private : 'anybody'}
}

if (!(this.url = opts.url)) {
Expand Down Expand Up @@ -193,25 +194,26 @@
xhr.open(this.httpMethod, this.url, true)
xhr.setRequestHeader('Authorization', 'Bearer ' + this.token)
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.setRequestHeader('Accept', 'application/vnd.vimeo.*+json;version=3.4')

xhr.onload = function(e) {
// get vimeo upload url, user (for available quote), ticket id and complete url
if (e.target.status < 400) {
var response = JSON.parse(e.target.responseText)
this.url = response.upload_link_secure
this.url = response.upload.upload_link
this.user = response.user
this.ticket_id = response.ticket_id
this.complete_url = defaults.api_url + response.complete_uri
this.video_url = response.uri
this.sendFile_()
} else {
this.onUploadError_(e)
}
}.bind(this)

xhr.onerror = this.onUploadError_.bind(this)
xhr.send(JSON.stringify({
type: 'streaming',
upgrade_to_1080: this.upgrade_to_1080
xhr.send(JSON.stringify({"upload": {
"approach": "tus",
"size": this.file.size
}
}))
}

Expand All @@ -227,22 +229,24 @@
var content = this.file
var end = this.file.size

var xhr = new XMLHttpRequest()
xhr.open('PATCH', this.url, true)
if (this.offset || this.chunkSize) {
// Only bother to slice the file if we're either resuming or uploading in chunks
if (this.chunkSize) {
end = Math.min(this.offset + this.chunkSize, this.file.size)
}
content = content.slice(this.offset, end)
xhr.setRequestHeader('Upload-Offset', this.offset)
}

var xhr = new XMLHttpRequest()
xhr.open('PUT', this.url, true)
xhr.setRequestHeader('Content-Type', this.contentType)
// xhr.setRequestHeader('Content-Length', this.file.size)
xhr.setRequestHeader('Content-Range', 'bytes ' + this.offset + '-' + (end - 1) + '/' + this.file.size)
else {
xhr.setRequestHeader('Upload-Offset', 0)
}
xhr.setRequestHeader('Content-Type', 'application/offset+octet-stream')
xhr.setRequestHeader('Tus-Resumable', '1.0.0')

if (xhr.upload) {
xhr.upload.addEventListener('progress', this.onProgress)
xhr.upload.addEventListener('progress', this.onProgress)
}
xhr.onload = this.onContentUploadSuccess_.bind(this)
xhr.onerror = this.onContentUploadError_.bind(this)
Expand All @@ -256,9 +260,10 @@
*/
me.prototype.resume_ = function() {
var xhr = new XMLHttpRequest()
xhr.open('PUT', this.url, true)
xhr.setRequestHeader('Content-Range', 'bytes */' + this.file.size)
xhr.setRequestHeader('X-Upload-Content-Type', this.file.type)
xhr.open('PATCH', this.url, true)
xhr.setRequestHeader('Content-Type', 'application/offset+octet-stream')
xhr.setRequestHeader('Tus-Resumable', '1.0.0')
xhr.setRequestHeader('Upload-Offset', this.offset)
if (xhr.upload) {
xhr.upload.addEventListener('progress', this.onProgress)
}
Expand All @@ -280,35 +285,18 @@
}

/**
* The final step is to call vimeo.videos.upload.complete to queue up
* the video for transcoding.
* The final step is to call vimeo.videos.upload.complete
*
* If successful call 'onUpdateVideoData_'
*
* @private
*/
me.prototype.complete_ = function(xhr) {
var xhr = new XMLHttpRequest()
xhr.open('DELETE', this.complete_url, true)
xhr.setRequestHeader('Authorization', 'Bearer ' + this.token)

xhr.onload = function(e) {

// Get the video location (videoId)
if (e.target.status < 400) {
var location = e.target.getResponseHeader('Location')

// Example of location: ' /videos/115365719', extract the video id only
var video_id = location.split('/').pop()
// Update the video metadata
this.onUpdateVideoData_(video_id)
} else {
this.onCompleteError_(e)
}
}.bind(this)

xhr.onerror = this.onCompleteError_.bind(this)
xhr.send()
me.prototype.complete_ = function(e) {
var location = this.video_url;
// Example of location: ' /videos/115365719', extract the video id only
var video_id = location.split('/').pop()
// Update the video metadata
this.onUpdateVideoData_(video_id)
}

/**
Expand Down Expand Up @@ -366,8 +354,8 @@
* @param {object} e XHR event
*/
me.prototype.onContentUploadSuccess_ = function(e) {
if (e.target.status == 200 || e.target.status == 201) {
this.complete_()
if (e.target.status == 200 || e.target.status == 201 || e.target.status == 204) {
this.complete_(e)
} else if (e.target.status == 308) {
this.extractRange_(e.target)
this.retryHandler.reset()
Expand Down