X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftools%2Fupload.ts;h=4d40c8c1a23824822a549cb38b567e9f9edadae3;hb=6104adc3e9d325e003ca1048e8ef35f6a451d0c3;hp=db59bbdffa9e424bd2bef9c631cd261136135818;hpb=a7fea183f0f69104b209e7bfdd6435be28165f22;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tools/upload.ts b/server/tools/upload.ts index db59bbdff..4d40c8c1a 100644 --- a/server/tools/upload.ts +++ b/server/tools/upload.ts @@ -2,84 +2,101 @@ import * as program from 'commander' import { access, constants } from 'fs' import { isAbsolute } from 'path' import { promisify } from 'util' +import { getClient, login } from '../tests/utils' +import { uploadVideo } from '../tests/utils/index' +import { VideoPrivacy } from '../../shared/models/videos' const accessPromise = promisify(access) -import { uploadVideo } from '../tests/utils/index' - program .option('-u, --url ', 'Server url') - .option('-a, --access-token ', 'Access token') - .option('-n, --name ', 'Video name') + .option('-U, --username ', 'Username') + .option('-p, --password ', 'Password') + .option('-n, --video-name ', 'Video name') + .option('-P, --privacy ', 'Privacy') .option('-N, --nsfw', 'Video is Not Safe For Work') .option('-c, --category ', 'Category number') + .option('-m, --comments-enabled', 'Enable comments') .option('-l, --licence ', 'Licence number') - .option('-L, --language ', 'Language number') - .option('-d, --description ', 'Video description') + .option('-L, --language ', 'Language ISO 639 code (fr or en...)') + .option('-d, --video-description ', 'Video description') .option('-t, --tags ', 'Video tags', list) + .option('-b, --thumbnail ', 'Thumbnail path') + .option('-v, --preview ', 'Preview path') .option('-f, --file ', 'Video absolute file path') .parse(process.argv) if (!program['tags']) program['tags'] = [] if (!program['nsfw']) program['nsfw'] = false +if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC +if (!program['commentsEnabled']) program['commentsEnabled'] = false if ( !program['url'] || - !program['accessToken'] || - !program['name'] || - !program['category'] || - !program['licence'] || - !program['description'] || + !program['username'] || + !program['password'] || + !program['videoName'] || !program['file'] ) { - throw new Error('All arguments but tags, language and nsfw are required.') + if (!program['url']) console.error('--url field is required.') + if (!program['username']) console.error('--username field is required.') + if (!program['password']) console.error('--password field is required.') + if (!program['videoName']) console.error('--video-name field is required.') + if (!program['file']) console.error('--file field is required.') + process.exit(-1) } if (isAbsolute(program['file']) === false) { - throw new Error('File path should be absolute.') + console.error('File path should be absolute.') + process.exit(-1) } -accessPromise(program['file'], constants.F_OK) - .then(() => { - return upload( - program['url'], - program['accessToken'], - program['name'], - program['category'], - program['licence'], - program['language'], - program['nsfw'], - program['description'], - program['tags'], - program['file'] - ) - }) - .then(() => process.exit(0)) - .catch(err => { - console.error(err) - process.exit(-1) - }) +run().catch(err => console.error(err)) -// ---------------------------------------------------------------------------- +async function run () { + const res = await getClient(program[ 'url' ]) + const client = { + id: res.body.client_id, + secret: res.body.client_secret + } -function list (val) { - return val.split(',') -} + const user = { + username: program[ 'username' ], + password: program[ 'password' ] + } + + const res2 = await login(program[ 'url' ], client, user) + const accessToken = res2.body.access_token -function upload (url, accessToken, name, category, licence, language, nsfw, description, tags, fixture) { - console.log('Uploading %s video...', program['name']) + await accessPromise(program[ 'file' ], constants.F_OK) + + console.log('Uploading %s video...', program[ 'videoName' ]) const videoAttributes = { - name, - category, - licence, - language, - nsfw, - description, - tags, - fixture + name: program['videoName'], + category: program['category'], + licence: program['licence'], + language: program['language'], + nsfw: program['nsfw'], + description: program['videoDescription'], + tags: program['tags'], + commentsEnabled: program['commentsEnabled'], + fixture: program['file'], + thumbnailfile: program['thumbnailPath'], + previewfile: program['previewPath'], + waitTranscoding: true, + privacy: program['privacy'], + support: undefined } - return uploadVideo(url, accessToken, videoAttributes).then(() => { - console.log(`Video ${name} uploaded.`) - }) + + await uploadVideo(program['url'], accessToken, videoAttributes) + + console.log(`Video ${program['videoName']} uploaded.`) + process.exit(0) +} + +// ---------------------------------------------------------------------------- + +function list (val) { + return val.split(',') }