Fix optional privacy in upload endpoint

pull/1787/head
Chocobozzz 2019-04-25 13:55:28 +02:00
parent 47f6409bb8
commit 2b4dd7e26d
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
6 changed files with 130 additions and 145 deletions

View File

@ -182,7 +182,7 @@ async function addVideo (req: express.Request, res: express.Response) {
nsfw: videoInfo.nsfw || false,
description: videoInfo.description,
support: videoInfo.support,
privacy: videoInfo.privacy,
privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
duration: videoPhysicalFile['duration'], // duration was added by a previous middleware
channelId: res.locals.videoChannel.id,
originallyPublishedAt: videoInfo.originallyPublishedAt

View File

@ -1,10 +1,9 @@
import 'express-validator'
import * as validator from 'validator'
import { UserNotificationSettingValue, UserRole } from '../../../shared'
import { UserRole } from '../../../shared'
import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES } from '../../initializers/constants'
import { exists, isFileValid, isBooleanValid } from './misc'
import { exists, isBooleanValid, isFileValid } from './misc'
import { values } from 'lodash'
import { UserAdminFlag } from '../../../shared/models/users/user-flag.model'
const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS

View File

@ -3,51 +3,83 @@ const netrc = require('netrc-parser').default
const version = require('../../../package.json').version
let settings = {
remotes: [],
default: 0
}
interface Settings {
remotes: any[],
default: number
}
async function getSettings () {
function getSettings () {
return new Promise<Settings>((res, rej) => {
let settings = {
const defaultSettings = {
remotes: [],
default: 0
} as Settings
}
config.read((err, data) => {
if (err) {
return rej(err)
}
return res(Object.keys(data).length === 0 ? settings : data)
if (err) return rej(err)
return res(Object.keys(data).length === 0 ? defaultSettings : data)
})
})
}
async function writeSettings (settings) {
async function getNetrc () {
await netrc.load()
return netrc
}
function writeSettings (settings) {
return new Promise((res, rej) => {
config.write(settings, function (err) {
if (err) {
return rej(err)
}
if (err) return rej(err)
return res()
})
})
}
netrc.loadSync()
function getRemoteObjectOrDie (program: any, settings: Settings) {
if (!program['url'] || !program['username'] || !program['password']) {
// No remote and we don't have program parameters: throw
if (settings.remotes.length === 0) {
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.')
return process.exit(-1)
}
let url: string = program['url']
let username: string = program['username']
let password: string = program['password']
if (!url) {
url = settings.default !== -1
? settings.remotes[settings.default]
: settings.remotes[0]
}
if (!username) username = netrc.machines[url].login
if (!password) password = netrc.machines[url].password
return { url, username, password }
}
return {
url: program[ 'url' ],
username: program[ 'username' ],
password: program[ 'password' ]
}
}
// ---------------------------------------------------------------------------
export {
version,
config,
settings,
getSettings,
writeSettings,
netrc
getNetrc,
getRemoteObjectOrDie,
writeSettings
}

View File

@ -1,22 +1,25 @@
import * as program from 'commander'
import * as prompt from 'prompt'
const Table = require('cli-table')
import { getSettings, writeSettings, netrc } from './cli'
import { getSettings, writeSettings, getNetrc } from './cli'
import { isHostValid } from '../helpers/custom-validators/servers'
import { isUserUsernameValid } from '../helpers/custom-validators/users'
const Table = require('cli-table')
async function delInstance (url: string) {
const settings = await getSettings()
const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
settings.remotes.splice(settings.remotes.indexOf(url))
await writeSettings(settings)
delete netrc.machines[url]
await netrc.save()
}
async function setInstance (url: string, username: string, password: string) {
const settings = await getSettings()
const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
if (settings.remotes.indexOf(url) === -1) {
settings.remotes.push(url)
}
@ -82,12 +85,13 @@ program
.command('list')
.description('lists registered remote instances')
.action(async () => {
const settings = await getSettings()
const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
const table = new Table({
head: ['instance', 'login'],
colWidths: [30, 30]
})
netrc.loadSync()
settings.remotes.forEach(element => {
table.push([
element,

View File

@ -12,7 +12,7 @@ import * as prompt from 'prompt'
import { remove } from 'fs-extra'
import { sha256 } from '../helpers/core-utils'
import { buildOriginallyPublishedAt, safeGetYoutubeDL } from '../helpers/youtube-dl'
import { getSettings, netrc } from './cli'
import { getNetrc, getRemoteObjectOrDie, getSettings } from './cli'
let accessToken: string
let client: { id: string, secret: string }
@ -32,48 +32,30 @@ program
.option('-v, --verbose', 'Verbose mode')
.parse(process.argv)
getSettings()
.then(settings => {
if ((!program['url'] || !program['username'] || !program['targetUrl']) && settings.remotes.length === 0) {
if (!program['url']) console.error('--url field is required.')
if (!program['username']) console.error('--username field is required.')
if (!program['targetUrl']) console.error('--targetUrl field is required.')
Promise.all([ getSettings(), getNetrc() ])
.then(([ settings, netrc ]) => {
const { url, username, password } = getRemoteObjectOrDie(program, settings)
process.exit(-1)
}
if (!program[ 'targetUrl' ]) {
console.error('--targetUrl field is required.')
if ((!program[ 'url' ] || !program[ 'username' ]) && settings.remotes.length > 0) {
if (!program[ 'url' ]) {
program[ 'url' ] = settings.default !== -1
? settings.remotes[ settings.default ]
: settings.remotes[ 0 ]
}
process.exit(-1)
}
if (!program['username']) program['username'] = netrc.machines[program['url']].login
if (!program['password']) program['password'] = netrc.machines[program['url']].password
}
removeEndSlashes(url)
removeEndSlashes(program[ 'targetUrl' ])
if (
!program['targetUrl']
) {
if (!program['targetUrl']) console.error('--targetUrl field is required.')
process.exit(-1)
}
const user = {
username: username,
password: password
}
removeEndSlashes(program['url'])
removeEndSlashes(program['targetUrl'])
const user = {
username: program['username'],
password: program['password']
}
run(user, program['url'])
.catch(err => {
console.error(err)
process.exit(-1)
})
})
run(user, url)
.catch(err => {
console.error(err)
process.exit(-1)
})
})
async function promptPassword () {
return new Promise((res, rej) => {
@ -116,7 +98,7 @@ async function run (user, url: string) {
const youtubeDL = await safeGetYoutubeDL()
const options = [ '-j', '--flat-playlist', '--playlist-reverse' ]
youtubeDL.getInfo(program['targetUrl'], options, processOptions, async (err, info) => {
youtubeDL.getInfo(program[ 'targetUrl' ], options, processOptions, async (err, info) => {
if (err) {
console.log(err.message)
process.exit(1)
@ -133,20 +115,20 @@ async function run (user, url: string) {
console.log('Will download and upload %d videos.\n', infoArray.length)
for (const info of infoArray) {
await processVideo(info, program['language'], processOptions.cwd, url, user)
await processVideo(info, program[ 'language' ], processOptions.cwd, url, user)
}
console.log('Video/s for user %s imported: %s', program['username'], program['targetUrl'])
console.log('Video/s for user %s imported: %s', program[ 'username' ], program[ 'targetUrl' ])
process.exit(0)
})
}
function processVideo (info: any, languageCode: string, cwd: string, url: string, user) {
return new Promise(async res => {
if (program['verbose']) console.log('Fetching object.', info)
if (program[ 'verbose' ]) console.log('Fetching object.', info)
const videoInfo = await fetchObject(info)
if (program['verbose']) console.log('Fetched object.', videoInfo)
if (program[ 'verbose' ]) console.log('Fetched object.', videoInfo)
const result = await searchVideoWithSort(url, videoInfo.title, '-match')
@ -187,9 +169,9 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st
let tags = []
if (Array.isArray(videoInfo.tags)) {
tags = videoInfo.tags
.filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
.map(t => t.normalize())
.slice(0, 5)
.filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
.map(t => t.normalize())
.slice(0, 5)
}
let thumbnailfile
@ -253,7 +235,7 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st
async function getCategory (categories: string[], url: string) {
if (!categories) return undefined
const categoryString = categories[0]
const categoryString = categories[ 0 ]
if (categoryString === 'News & Politics') return 11
@ -261,7 +243,7 @@ async function getCategory (categories: string[], url: string) {
const categoriesServer = res.body
for (const key of Object.keys(categoriesServer)) {
const categoryServer = categoriesServer[key]
const categoryServer = categoriesServer[ key ]
if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10)
}
@ -285,12 +267,12 @@ function normalizeObject (obj: any) {
// Deprecated key
if (key === 'resolution') continue
const value = obj[key]
const value = obj[ key ]
if (typeof value === 'string') {
newObj[key] = value.normalize()
newObj[ key ] = value.normalize()
} else {
newObj[key] = value
newObj[ key ] = value
}
}

View File

@ -4,7 +4,7 @@ import { isAbsolute } from 'path'
import { getClient, login } from '../../shared/extra-utils'
import { uploadVideo } from '../../shared/extra-utils/'
import { VideoPrivacy } from '../../shared/models/videos'
import { netrc, getSettings } from './cli'
import { getRemoteObjectOrDie, getSettings } from './cli'
program
.name('upload')
@ -26,49 +26,15 @@ program
.option('-f, --file <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['downloadEnabled']) program['downloadEnabled'] = true
getSettings()
.then(settings => {
if (
(!program['url'] ||
!program['username'] ||
!program['password']) &&
(settings.remotes.length === 0)
) {
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)
}
const { url, username, password } = getRemoteObjectOrDie(program, settings)
if (
(!program['url'] ||
!program['username'] ||
!program['password']) &&
(settings.remotes.length > 0)
) {
if (!program['url']) {
program['url'] = (settings.default !== -1) ?
settings.remotes[settings.default] :
settings.remotes[0]
}
if (!program['username']) program['username'] = netrc.machines[program['url']].login
if (!program['password']) program['password'] = netrc.machines[program['url']].password
}
if (!program['videoName'] || !program['file'] || !program['channelId']) {
if (!program['videoName']) console.error('--video-name is required.')
if (!program['file']) console.error('--file is required.')
if (!program['channelId']) console.error('--channel-id is required.')
if (
!program['videoName'] ||
!program['file']
) {
if (!program['videoName']) console.error('--video-name field is required.')
if (!program['file']) console.error('--file field is required.')
process.exit(-1)
}
@ -77,28 +43,25 @@ getSettings()
process.exit(-1)
}
run().catch(err => {
run(url, username, password).catch(err => {
console.error(err)
process.exit(-1)
})
})
async function run () {
const res = await getClient(program[ 'url' ])
async function run (url: string, username: string, password: string) {
const resClient = await getClient(program[ 'url' ])
const client = {
id: res.body.client_id,
secret: res.body.client_secret
id: resClient.body.client_id,
secret: resClient.body.client_secret
}
const user = {
username: program[ 'username' ],
password: program[ 'password' ]
}
const user = { username, password }
let accessToken: string
try {
const res2 = await login(program[ 'url' ], client, user)
accessToken = res2.body.access_token
const res = await login(url, client, user)
accessToken = res.body.access_token
} catch (err) {
throw new Error('Cannot authenticate. Please check your username/password.')
}
@ -109,27 +72,32 @@ async function run () {
const videoAttributes = {
name: program['videoName'],
category: program['category'],
category: program['category'] || undefined,
channelId: program['channelId'],
licence: program['licence'],
language: program['language'],
nsfw: program['nsfw'],
description: program['videoDescription'],
tags: program['tags'],
commentsEnabled: program['commentsEnabled'],
downloadEnabled: program['downloadEnabled'],
licence: program['licence'] || undefined,
language: program['language'] || undefined,
nsfw: program['nsfw'] !== undefined ? program['nsfw'] : false,
description: program['videoDescription'] || '',
tags: program['tags'] || [],
commentsEnabled: program['commentsEnabled'] !== undefined ? program['commentsEnabled'] : true,
downloadEnabled: program['downloadEnabled'] !== undefined ? program['downloadEnabled'] : true,
fixture: program['file'],
thumbnailfile: program['thumbnail'],
previewfile: program['preview'],
waitTranscoding: true,
privacy: program['privacy'],
privacy: program['privacy'] || VideoPrivacy.PUBLIC,
support: undefined
}
await uploadVideo(program[ 'url' ], accessToken, videoAttributes)
console.log(`Video ${program['videoName']} uploaded.`)
process.exit(0)
try {
await uploadVideo(url, accessToken, videoAttributes)
console.log(`Video ${program['videoName']} uploaded.`)
process.exit(0)
} catch (err) {
console.log('coucou')
console.error(require('util').inspect(err))
process.exit(-1)
}
}
// ----------------------------------------------------------------------------