|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2023 Vimeo |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +const Vimeo = require('../index').Vimeo |
| 20 | + |
| 21 | +let config = {} |
| 22 | + |
| 23 | +try { |
| 24 | + config = require('./config.json') |
| 25 | +} catch (error) { |
| 26 | + console.error('ERROR: For this example to run properly you must create an API app at ' + |
| 27 | + 'https://developer.vimeo.com/apps/new and set your callback url to ' + |
| 28 | + '`http://localhost:8080/oauth_callback`.') |
| 29 | + console.error('ERROR: Once you have your app, make a copy of `config.json.example` named ' + |
| 30 | + '`config.json` and add your client ID, client secret and access token.') |
| 31 | + process.exit() |
| 32 | +} |
| 33 | + |
| 34 | +if (!config.access_token) { |
| 35 | + throw new Error('You can not upload a video without configuring an access token.') |
| 36 | +} |
| 37 | + |
| 38 | +// Instantiate the library with your client id, secret and access token (pulled from dev site) |
| 39 | +const client = new Vimeo(config.client_id, config.client_secret, config.access_token) |
| 40 | + |
| 41 | +// Create a variable with a hard coded path to your file system |
| 42 | +const filePath = 'path' |
| 43 | + |
| 44 | +const uploadVideo = async (filePath) => { |
| 45 | + console.log('Uploading: ' + filePath) |
| 46 | + |
| 47 | + const params = { |
| 48 | + name: 'Vimeo API SDK test upload', |
| 49 | + description: "This video was uploaded through the Vimeo API's NodeJS SDK." |
| 50 | + } |
| 51 | + |
| 52 | + const progressCallback = (bytesUploaded, bytesTotal) => { |
| 53 | + const percentage = (bytesUploaded / bytesTotal * 100).toFixed(2) |
| 54 | + console.log(bytesUploaded, bytesTotal, percentage + '%') |
| 55 | + } |
| 56 | + |
| 57 | + const uri = await client.upload( |
| 58 | + filePath, |
| 59 | + params, |
| 60 | + progressCallback |
| 61 | + ).catch(e => console.log(e)) |
| 62 | + |
| 63 | + // Get the metadata response from the upload and log out the Vimeo.com url |
| 64 | + const metadata = await client.request(uri + '?fields=link').catch(e => console.log(e)) |
| 65 | + |
| 66 | + console.log('The file has been uploaded to ' + metadata.link) |
| 67 | + |
| 68 | + // Make an API call to edit the title and description of the video. |
| 69 | + const editRes = await client.request({ |
| 70 | + method: 'PATCH', |
| 71 | + path: uri, |
| 72 | + params: { |
| 73 | + name: 'Vimeo API SDK test edit', |
| 74 | + description: "This video was edited through the Vimeo API's NodeJS SDK." |
| 75 | + } |
| 76 | + }).catch(e => console.log(e)) |
| 77 | + |
| 78 | + console.log(editRes.statusCode) |
| 79 | + console.log('The title and description for ' + uri + ' has been edited') |
| 80 | + |
| 81 | + // Make an API call to see if the video is finished transcoding. |
| 82 | + const transcodeStatus = await client.request(uri + '?fields=transcode.status').catch(e => console.log(e)) |
| 83 | + console.log('The transcode status for ' + uri + ' is: ' + transcodeStatus.body.transcode.status) |
| 84 | +} |
| 85 | + |
| 86 | +uploadVideo(filePath) |
0 commit comments