UNPKG

1.98 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright 2013 Vimeo
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18var Vimeo = require('../index').Vimeo;
19
20try {
21 var config = require('./config.json');
22} catch (error) {
23 console.error('ERROR: For this example to run properly you must create an api app at developer.vimeo.com/apps/new and set your callback url to http://localhost:8080/oauth_callback');
24 console.error('ERROR: Once you have your app, make a copy of config.json.example named "config.json" and add your client id, client secret and access token');
25 return;
26}
27
28var lib = new Vimeo(config.client_id, config.client_secret);
29if (!config.access_token) {
30 throw new Error('You can not upload a video without configuring an access token');
31}
32
33// The file to upload should be passed in as the first argument to this script
34var file_path = process.argv[2];
35
36// This variable, and the logic related to it helps ensure we only log the percentage
37// if the percentage has changed.
38var prev_percentage = -1;
39
40lib.access_token = config.access_token;
41lib.streamingUpload(file_path,
42 function (err, body, status, headers) {
43 if (err) {
44 return console.log(err);
45 }
46 console.log(status);
47 console.log(headers.location);
48 },
49 function (uploaded_size, file_size) {
50 var percentage = Math.round((uploaded_size/file_size) * 100);
51
52 if (percentage != prev_percentage) {
53 console.log(percentage + '%' + ' uploaded\n');
54 prev_percentage = percentage;
55 }
56 }
57);