aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--server/controllers/api/videos/index.ts2
-rw-r--r--server/helpers/custom-validators/users.ts5
-rw-r--r--server/tools/cli.ts72
-rw-r--r--server/tools/peertube-auth.ts16
-rw-r--r--server/tools/peertube-import-videos.ts84
-rw-r--r--server/tools/peertube-upload.ts96
6 files changed, 130 insertions, 145 deletions
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts
index 5bbce11b4..1a18a8ae8 100644
--- a/server/controllers/api/videos/index.ts
+++ b/server/controllers/api/videos/index.ts
@@ -182,7 +182,7 @@ async function addVideo (req: express.Request, res: express.Response) {
182 nsfw: videoInfo.nsfw || false, 182 nsfw: videoInfo.nsfw || false,
183 description: videoInfo.description, 183 description: videoInfo.description,
184 support: videoInfo.support, 184 support: videoInfo.support,
185 privacy: videoInfo.privacy, 185 privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
186 duration: videoPhysicalFile['duration'], // duration was added by a previous middleware 186 duration: videoPhysicalFile['duration'], // duration was added by a previous middleware
187 channelId: res.locals.videoChannel.id, 187 channelId: res.locals.videoChannel.id,
188 originallyPublishedAt: videoInfo.originallyPublishedAt 188 originallyPublishedAt: videoInfo.originallyPublishedAt
diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts
index e3ad9102a..56bc10b16 100644
--- a/server/helpers/custom-validators/users.ts
+++ b/server/helpers/custom-validators/users.ts
@@ -1,10 +1,9 @@
1import 'express-validator' 1import 'express-validator'
2import * as validator from 'validator' 2import * as validator from 'validator'
3import { UserNotificationSettingValue, UserRole } from '../../../shared' 3import { UserRole } from '../../../shared'
4import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES } from '../../initializers/constants' 4import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES } from '../../initializers/constants'
5import { exists, isFileValid, isBooleanValid } from './misc' 5import { exists, isBooleanValid, isFileValid } from './misc'
6import { values } from 'lodash' 6import { values } from 'lodash'
7import { UserAdminFlag } from '../../../shared/models/users/user-flag.model'
8 7
9const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS 8const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
10 9
diff --git a/server/tools/cli.ts b/server/tools/cli.ts
index 108c44218..59e9fcfc4 100644
--- a/server/tools/cli.ts
+++ b/server/tools/cli.ts
@@ -3,51 +3,83 @@ const netrc = require('netrc-parser').default
3 3
4const version = require('../../../package.json').version 4const version = require('../../../package.json').version
5 5
6let settings = {
7 remotes: [],
8 default: 0
9}
10
11interface Settings { 6interface Settings {
12 remotes: any[], 7 remotes: any[],
13 default: number 8 default: number
14} 9}
15 10
16async function getSettings () { 11function getSettings () {
17 return new Promise<Settings>((res, rej) => { 12 return new Promise<Settings>((res, rej) => {
18 let settings = { 13 const defaultSettings = {
19 remotes: [], 14 remotes: [],
20 default: 0 15 default: 0
21 } as Settings 16 }
17
22 config.read((err, data) => { 18 config.read((err, data) => {
23 if (err) { 19 if (err) return rej(err)
24 return rej(err) 20
25 } 21 return res(Object.keys(data).length === 0 ? defaultSettings : data)
26 return res(Object.keys(data).length === 0 ? settings : data)
27 }) 22 })
28 }) 23 })
29} 24}
30 25
31async function writeSettings (settings) { 26async function getNetrc () {
27 await netrc.load()
28
29 return netrc
30}
31
32function writeSettings (settings) {
32 return new Promise((res, rej) => { 33 return new Promise((res, rej) => {
33 config.write(settings, function (err) { 34 config.write(settings, function (err) {
34 if (err) { 35 if (err) return rej(err)
35 return rej(err) 36
36 }
37 return res() 37 return res()
38 }) 38 })
39 }) 39 })
40} 40}
41 41
42netrc.loadSync() 42function getRemoteObjectOrDie (program: any, settings: Settings) {
43 if (!program['url'] || !program['username'] || !program['password']) {
44 // No remote and we don't have program parameters: throw
45 if (settings.remotes.length === 0) {
46 if (!program[ 'url' ]) console.error('--url field is required.')
47 if (!program[ 'username' ]) console.error('--username field is required.')
48 if (!program[ 'password' ]) console.error('--password field is required.')
49
50 return process.exit(-1)
51 }
52
53 let url: string = program['url']
54 let username: string = program['username']
55 let password: string = program['password']
56
57 if (!url) {
58 url = settings.default !== -1
59 ? settings.remotes[settings.default]
60 : settings.remotes[0]
61 }
62
63 if (!username) username = netrc.machines[url].login
64 if (!password) password = netrc.machines[url].password
65
66 return { url, username, password }
67 }
68
69 return {
70 url: program[ 'url' ],
71 username: program[ 'username' ],
72 password: program[ 'password' ]
73 }
74}
43 75
44// --------------------------------------------------------------------------- 76// ---------------------------------------------------------------------------
45 77
46export { 78export {
47 version, 79 version,
48 config, 80 config,
49 settings,
50 getSettings, 81 getSettings,
51 writeSettings, 82 getNetrc,
52 netrc 83 getRemoteObjectOrDie,
84 writeSettings
53} 85}
diff --git a/server/tools/peertube-auth.ts b/server/tools/peertube-auth.ts
index a962944a4..8bc3d332c 100644
--- a/server/tools/peertube-auth.ts
+++ b/server/tools/peertube-auth.ts
@@ -1,22 +1,25 @@
1import * as program from 'commander' 1import * as program from 'commander'
2import * as prompt from 'prompt' 2import * as prompt from 'prompt'
3const Table = require('cli-table') 3import { getSettings, writeSettings, getNetrc } from './cli'
4import { getSettings, writeSettings, netrc } from './cli'
5import { isHostValid } from '../helpers/custom-validators/servers' 4import { isHostValid } from '../helpers/custom-validators/servers'
6import { isUserUsernameValid } from '../helpers/custom-validators/users' 5import { isUserUsernameValid } from '../helpers/custom-validators/users'
7 6
7const Table = require('cli-table')
8
8async function delInstance (url: string) { 9async function delInstance (url: string) {
9 const settings = await getSettings() 10 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
10 11
11 settings.remotes.splice(settings.remotes.indexOf(url)) 12 settings.remotes.splice(settings.remotes.indexOf(url))
12 await writeSettings(settings) 13 await writeSettings(settings)
13 14
14 delete netrc.machines[url] 15 delete netrc.machines[url]
16
15 await netrc.save() 17 await netrc.save()
16} 18}
17 19
18async function setInstance (url: string, username: string, password: string) { 20async function setInstance (url: string, username: string, password: string) {
19 const settings = await getSettings() 21 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
22
20 if (settings.remotes.indexOf(url) === -1) { 23 if (settings.remotes.indexOf(url) === -1) {
21 settings.remotes.push(url) 24 settings.remotes.push(url)
22 } 25 }
@@ -82,12 +85,13 @@ program
82 .command('list') 85 .command('list')
83 .description('lists registered remote instances') 86 .description('lists registered remote instances')
84 .action(async () => { 87 .action(async () => {
85 const settings = await getSettings() 88 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
89
86 const table = new Table({ 90 const table = new Table({
87 head: ['instance', 'login'], 91 head: ['instance', 'login'],
88 colWidths: [30, 30] 92 colWidths: [30, 30]
89 }) 93 })
90 netrc.loadSync() 94
91 settings.remotes.forEach(element => { 95 settings.remotes.forEach(element => {
92 table.push([ 96 table.push([
93 element, 97 element,
diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts
index 024f640a4..9a366dbbd 100644
--- a/server/tools/peertube-import-videos.ts
+++ b/server/tools/peertube-import-videos.ts
@@ -12,7 +12,7 @@ import * as prompt from 'prompt'
12import { remove } from 'fs-extra' 12import { remove } from 'fs-extra'
13import { sha256 } from '../helpers/core-utils' 13import { sha256 } from '../helpers/core-utils'
14import { buildOriginallyPublishedAt, safeGetYoutubeDL } from '../helpers/youtube-dl' 14import { buildOriginallyPublishedAt, safeGetYoutubeDL } from '../helpers/youtube-dl'
15import { getSettings, netrc } from './cli' 15import { getNetrc, getRemoteObjectOrDie, getSettings } from './cli'
16 16
17let accessToken: string 17let accessToken: string
18let client: { id: string, secret: string } 18let client: { id: string, secret: string }
@@ -32,48 +32,30 @@ program
32 .option('-v, --verbose', 'Verbose mode') 32 .option('-v, --verbose', 'Verbose mode')
33 .parse(process.argv) 33 .parse(process.argv)
34 34
35getSettings() 35Promise.all([ getSettings(), getNetrc() ])
36.then(settings => { 36 .then(([ settings, netrc ]) => {
37 if ((!program['url'] || !program['username'] || !program['targetUrl']) && settings.remotes.length === 0) { 37 const { url, username, password } = getRemoteObjectOrDie(program, settings)
38 if (!program['url']) console.error('--url field is required.')
39 if (!program['username']) console.error('--username field is required.')
40 if (!program['targetUrl']) console.error('--targetUrl field is required.')
41 38
42 process.exit(-1) 39 if (!program[ 'targetUrl' ]) {
43 } 40 console.error('--targetUrl field is required.')
44
45 if ((!program[ 'url' ] || !program[ 'username' ]) && settings.remotes.length > 0) {
46 if (!program[ 'url' ]) {
47 program[ 'url' ] = settings.default !== -1
48 ? settings.remotes[ settings.default ]
49 : settings.remotes[ 0 ]
50 }
51
52 if (!program['username']) program['username'] = netrc.machines[program['url']].login
53 if (!program['password']) program['password'] = netrc.machines[program['url']].password
54 }
55 41
56 if ( 42 process.exit(-1)
57 !program['targetUrl'] 43 }
58 ) {
59 if (!program['targetUrl']) console.error('--targetUrl field is required.')
60 process.exit(-1)
61 }
62 44
63 removeEndSlashes(program['url']) 45 removeEndSlashes(url)
64 removeEndSlashes(program['targetUrl']) 46 removeEndSlashes(program[ 'targetUrl' ])
65 47
66 const user = { 48 const user = {
67 username: program['username'], 49 username: username,
68 password: program['password'] 50 password: password
69 } 51 }
70 52
71 run(user, program['url']) 53 run(user, url)
72 .catch(err => { 54 .catch(err => {
73 console.error(err) 55 console.error(err)
74 process.exit(-1) 56 process.exit(-1)
75 }) 57 })
76}) 58 })
77 59
78async function promptPassword () { 60async function promptPassword () {
79 return new Promise((res, rej) => { 61 return new Promise((res, rej) => {
@@ -116,7 +98,7 @@ async function run (user, url: string) {
116 const youtubeDL = await safeGetYoutubeDL() 98 const youtubeDL = await safeGetYoutubeDL()
117 99
118 const options = [ '-j', '--flat-playlist', '--playlist-reverse' ] 100 const options = [ '-j', '--flat-playlist', '--playlist-reverse' ]
119 youtubeDL.getInfo(program['targetUrl'], options, processOptions, async (err, info) => { 101 youtubeDL.getInfo(program[ 'targetUrl' ], options, processOptions, async (err, info) => {
120 if (err) { 102 if (err) {
121 console.log(err.message) 103 console.log(err.message)
122 process.exit(1) 104 process.exit(1)
@@ -133,20 +115,20 @@ async function run (user, url: string) {
133 console.log('Will download and upload %d videos.\n', infoArray.length) 115 console.log('Will download and upload %d videos.\n', infoArray.length)
134 116
135 for (const info of infoArray) { 117 for (const info of infoArray) {
136 await processVideo(info, program['language'], processOptions.cwd, url, user) 118 await processVideo(info, program[ 'language' ], processOptions.cwd, url, user)
137 } 119 }
138 120
139 console.log('Video/s for user %s imported: %s', program['username'], program['targetUrl']) 121 console.log('Video/s for user %s imported: %s', program[ 'username' ], program[ 'targetUrl' ])
140 process.exit(0) 122 process.exit(0)
141 }) 123 })
142} 124}
143 125
144function processVideo (info: any, languageCode: string, cwd: string, url: string, user) { 126function processVideo (info: any, languageCode: string, cwd: string, url: string, user) {
145 return new Promise(async res => { 127 return new Promise(async res => {
146 if (program['verbose']) console.log('Fetching object.', info) 128 if (program[ 'verbose' ]) console.log('Fetching object.', info)
147 129
148 const videoInfo = await fetchObject(info) 130 const videoInfo = await fetchObject(info)
149 if (program['verbose']) console.log('Fetched object.', videoInfo) 131 if (program[ 'verbose' ]) console.log('Fetched object.', videoInfo)
150 132
151 const result = await searchVideoWithSort(url, videoInfo.title, '-match') 133 const result = await searchVideoWithSort(url, videoInfo.title, '-match')
152 134
@@ -187,9 +169,9 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st
187 let tags = [] 169 let tags = []
188 if (Array.isArray(videoInfo.tags)) { 170 if (Array.isArray(videoInfo.tags)) {
189 tags = videoInfo.tags 171 tags = videoInfo.tags
190 .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min) 172 .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
191 .map(t => t.normalize()) 173 .map(t => t.normalize())
192 .slice(0, 5) 174 .slice(0, 5)
193 } 175 }
194 176
195 let thumbnailfile 177 let thumbnailfile
@@ -253,7 +235,7 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st
253async function getCategory (categories: string[], url: string) { 235async function getCategory (categories: string[], url: string) {
254 if (!categories) return undefined 236 if (!categories) return undefined
255 237
256 const categoryString = categories[0] 238 const categoryString = categories[ 0 ]
257 239
258 if (categoryString === 'News & Politics') return 11 240 if (categoryString === 'News & Politics') return 11
259 241
@@ -261,7 +243,7 @@ async function getCategory (categories: string[], url: string) {
261 const categoriesServer = res.body 243 const categoriesServer = res.body
262 244
263 for (const key of Object.keys(categoriesServer)) { 245 for (const key of Object.keys(categoriesServer)) {
264 const categoryServer = categoriesServer[key] 246 const categoryServer = categoriesServer[ key ]
265 if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10) 247 if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10)
266 } 248 }
267 249
@@ -285,12 +267,12 @@ function normalizeObject (obj: any) {
285 // Deprecated key 267 // Deprecated key
286 if (key === 'resolution') continue 268 if (key === 'resolution') continue
287 269
288 const value = obj[key] 270 const value = obj[ key ]
289 271
290 if (typeof value === 'string') { 272 if (typeof value === 'string') {
291 newObj[key] = value.normalize() 273 newObj[ key ] = value.normalize()
292 } else { 274 } else {
293 newObj[key] = value 275 newObj[ key ] = value
294 } 276 }
295 } 277 }
296 278
diff --git a/server/tools/peertube-upload.ts b/server/tools/peertube-upload.ts
index 13ca94e51..687f2e60b 100644
--- a/server/tools/peertube-upload.ts
+++ b/server/tools/peertube-upload.ts
@@ -4,7 +4,7 @@ import { isAbsolute } from 'path'
4import { getClient, login } from '../../shared/extra-utils' 4import { getClient, login } from '../../shared/extra-utils'
5import { uploadVideo } from '../../shared/extra-utils/' 5import { uploadVideo } from '../../shared/extra-utils/'
6import { VideoPrivacy } from '../../shared/models/videos' 6import { VideoPrivacy } from '../../shared/models/videos'
7import { netrc, getSettings } from './cli' 7import { getRemoteObjectOrDie, getSettings } from './cli'
8 8
9program 9program
10 .name('upload') 10 .name('upload')
@@ -26,49 +26,15 @@ program
26 .option('-f, --file <file>', 'Video absolute file path') 26 .option('-f, --file <file>', 'Video absolute file path')
27 .parse(process.argv) 27 .parse(process.argv)
28 28
29if (!program['tags']) program['tags'] = []
30if (!program['nsfw']) program['nsfw'] = false
31if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC
32if (!program['commentsEnabled']) program['commentsEnabled'] = false
33if (!program['downloadEnabled']) program['downloadEnabled'] = true
34
35getSettings() 29getSettings()
36 .then(settings => { 30 .then(settings => {
37 if ( 31 const { url, username, password } = getRemoteObjectOrDie(program, settings)
38 (!program['url'] ||
39 !program['username'] ||
40 !program['password']) &&
41 (settings.remotes.length === 0)
42 ) {
43 if (!program['url']) console.error('--url field is required.')
44 if (!program['username']) console.error('--username field is required.')
45 if (!program['password']) console.error('--password field is required.')
46 if (!program['videoName']) console.error('--video-name field is required.')
47 if (!program['file']) console.error('--file field is required.')
48 process.exit(-1)
49 }
50 32
51 if ( 33 if (!program['videoName'] || !program['file'] || !program['channelId']) {
52 (!program['url'] || 34 if (!program['videoName']) console.error('--video-name is required.')
53 !program['username'] || 35 if (!program['file']) console.error('--file is required.')
54 !program['password']) && 36 if (!program['channelId']) console.error('--channel-id is required.')
55 (settings.remotes.length > 0)
56 ) {
57 if (!program['url']) {
58 program['url'] = (settings.default !== -1) ?
59 settings.remotes[settings.default] :
60 settings.remotes[0]
61 }
62 if (!program['username']) program['username'] = netrc.machines[program['url']].login
63 if (!program['password']) program['password'] = netrc.machines[program['url']].password
64 }
65 37
66 if (
67 !program['videoName'] ||
68 !program['file']
69 ) {
70 if (!program['videoName']) console.error('--video-name field is required.')
71 if (!program['file']) console.error('--file field is required.')
72 process.exit(-1) 38 process.exit(-1)
73 } 39 }
74 40
@@ -77,28 +43,25 @@ getSettings()
77 process.exit(-1) 43 process.exit(-1)
78 } 44 }
79 45
80 run().catch(err => { 46 run(url, username, password).catch(err => {
81 console.error(err) 47 console.error(err)
82 process.exit(-1) 48 process.exit(-1)
83 }) 49 })
84 }) 50 })
85 51
86async function run () { 52async function run (url: string, username: string, password: string) {
87 const res = await getClient(program[ 'url' ]) 53 const resClient = await getClient(program[ 'url' ])
88 const client = { 54 const client = {
89 id: res.body.client_id, 55 id: resClient.body.client_id,
90 secret: res.body.client_secret 56 secret: resClient.body.client_secret
91 } 57 }
92 58
93 const user = { 59 const user = { username, password }
94 username: program[ 'username' ],
95 password: program[ 'password' ]
96 }
97 60
98 let accessToken: string 61 let accessToken: string
99 try { 62 try {
100 const res2 = await login(program[ 'url' ], client, user) 63 const res = await login(url, client, user)
101 accessToken = res2.body.access_token 64 accessToken = res.body.access_token
102 } catch (err) { 65 } catch (err) {
103 throw new Error('Cannot authenticate. Please check your username/password.') 66 throw new Error('Cannot authenticate. Please check your username/password.')
104 } 67 }
@@ -109,27 +72,32 @@ async function run () {
109 72
110 const videoAttributes = { 73 const videoAttributes = {
111 name: program['videoName'], 74 name: program['videoName'],
112 category: program['category'], 75 category: program['category'] || undefined,
113 channelId: program['channelId'], 76 channelId: program['channelId'],
114 licence: program['licence'], 77 licence: program['licence'] || undefined,
115 language: program['language'], 78 language: program['language'] || undefined,
116 nsfw: program['nsfw'], 79 nsfw: program['nsfw'] !== undefined ? program['nsfw'] : false,
117 description: program['videoDescription'], 80 description: program['videoDescription'] || '',
118 tags: program['tags'], 81 tags: program['tags'] || [],
119 commentsEnabled: program['commentsEnabled'], 82 commentsEnabled: program['commentsEnabled'] !== undefined ? program['commentsEnabled'] : true,
120 downloadEnabled: program['downloadEnabled'], 83 downloadEnabled: program['downloadEnabled'] !== undefined ? program['downloadEnabled'] : true,
121 fixture: program['file'], 84 fixture: program['file'],
122 thumbnailfile: program['thumbnail'], 85 thumbnailfile: program['thumbnail'],
123 previewfile: program['preview'], 86 previewfile: program['preview'],
124 waitTranscoding: true, 87 waitTranscoding: true,
125 privacy: program['privacy'], 88 privacy: program['privacy'] || VideoPrivacy.PUBLIC,
126 support: undefined 89 support: undefined
127 } 90 }
128 91
129 await uploadVideo(program[ 'url' ], accessToken, videoAttributes) 92 try {
130 93 await uploadVideo(url, accessToken, videoAttributes)
131 console.log(`Video ${program['videoName']} uploaded.`) 94 console.log(`Video ${program['videoName']} uploaded.`)
132 process.exit(0) 95 process.exit(0)
96 } catch (err) {
97 console.log('coucou')
98 console.error(require('util').inspect(err))
99 process.exit(-1)
100 }
133} 101}
134 102
135// ---------------------------------------------------------------------------- 103// ----------------------------------------------------------------------------