]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/peertube-upload.ts
Add test to ensure files are deleted
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-upload.ts
CommitLineData
12152aa0 1import { program } from 'commander'
c9d5c64f 2import { access, constants } from 'fs-extra'
8df87ce7 3import { isAbsolute } from 'path'
1a73a7dc 4import { assignToken, buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './shared'
8df87ce7 5
1205823f 6let command = program
8704acf4 7 .name('upload')
1205823f
C
8
9command = buildCommonVideoOptions(command)
10
11command
8df87ce7 12 .option('-u, --url <url>', 'Server url')
a87d467a
C
13 .option('-U, --username <username>', 'Username')
14 .option('-p, --password <token>', 'Password')
2422c46b 15 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
02988fdc 16 .option('-v, --preview <previewPath>', 'Preview path')
8df87ce7
C
17 .option('-f, --file <file>', 'Video absolute file path')
18 .parse(process.argv)
19
ba5a8d89
C
20const options = command.opts()
21
8d2be0ed
C
22getServerCredentials(command)
23 .then(({ url, username, password }) => {
ba5a8d89
C
24 if (!options.videoName || !options.file) {
25 if (!options.videoName) console.error('--video-name is required.')
26 if (!options.file) console.error('--file is required.')
8704acf4 27
8d2be0ed
C
28 process.exit(-1)
29 }
8704acf4 30
ba5a8d89 31 if (isAbsolute(options.file) === false) {
8d2be0ed
C
32 console.error('File path should be absolute.')
33 process.exit(-1)
34 }
8704acf4 35
8d2be0ed
C
36 run(url, username, password).catch(err => {
37 console.error(err)
38 process.exit(-1)
39 })
40 })
a1587156 41 .catch(err => console.error(err))
8df87ce7 42
2b4dd7e2 43async function run (url: string, username: string, password: string) {
d0a0fa42
C
44 const server = buildServer(url)
45 await assignToken(server, username, password)
8df87ce7 46
ba5a8d89 47 await access(options.file, constants.F_OK)
a87d467a 48
ba5a8d89 49 console.log('Uploading %s video...', options.videoName)
8df87ce7 50
d23dd9fb
C
51 const baseAttributes = await buildVideoAttributesFromCommander(server, program)
52
53 const attributes = {
54 ...baseAttributes,
1205823f 55
ba5a8d89
C
56 fixture: options.file,
57 thumbnailfile: options.thumbnail,
58 previewfile: options.preview
d23dd9fb 59 }
a87d467a 60
2b4dd7e2 61 try {
89d241a7 62 await server.videos.upload({ attributes })
ba5a8d89 63 console.log(`Video ${options.videoName} uploaded.`)
2b4dd7e2
C
64 process.exit(0)
65 } catch (err) {
743dab55
C
66 const message = err.message || ''
67 if (message.includes('413')) {
52435e46 68 console.error('Aborted: user quota is exceeded or video file is too big for this PeerTube instance.')
743dab55
C
69 } else {
70 console.error(require('util').inspect(err))
71 }
72
2b4dd7e2
C
73 process.exit(-1)
74 }
a87d467a
C
75}
76
77// ----------------------------------------------------------------------------