Fix optional privacy in upload endpoint

This commit is contained in:
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, nsfw: videoInfo.nsfw || false,
description: videoInfo.description, description: videoInfo.description,
support: videoInfo.support, support: videoInfo.support,
privacy: videoInfo.privacy, privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
duration: videoPhysicalFile['duration'], // duration was added by a previous middleware duration: videoPhysicalFile['duration'], // duration was added by a previous middleware
channelId: res.locals.videoChannel.id, channelId: res.locals.videoChannel.id,
originallyPublishedAt: videoInfo.originallyPublishedAt originallyPublishedAt: videoInfo.originallyPublishedAt

View File

@ -1,10 +1,9 @@
import 'express-validator' import 'express-validator'
import * as validator from '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 { 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 { values } from 'lodash'
import { UserAdminFlag } from '../../../shared/models/users/user-flag.model'
const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS 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 const version = require('../../../package.json').version
let settings = {
remotes: [],
default: 0
}
interface Settings { interface Settings {
remotes: any[], remotes: any[],
default: number default: number
} }
async function getSettings () { function getSettings () {
return new Promise<Settings>((res, rej) => { return new Promise<Settings>((res, rej) => {
let settings = { const defaultSettings = {
remotes: [], remotes: [],
default: 0 default: 0
} as Settings }
config.read((err, data) => { config.read((err, data) => {
if (err) { if (err) return rej(err)
return rej(err)
} return res(Object.keys(data).length === 0 ? defaultSettings : data)
return res(Object.keys(data).length === 0 ? settings : data)
}) })
}) })
} }
async function writeSettings (settings) { async function getNetrc () {
await netrc.load()
return netrc
}
function writeSettings (settings) {
return new Promise((res, rej) => { return new Promise((res, rej) => {
config.write(settings, function (err) { config.write(settings, function (err) {
if (err) { if (err) return rej(err)
return rej(err)
}
return res() 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 { export {
version, version,
config, config,
settings,
getSettings, getSettings,
writeSettings, getNetrc,
netrc getRemoteObjectOrDie,
writeSettings
} }

View File

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

View File

@ -12,7 +12,7 @@ import * as prompt from 'prompt'
import { remove } from 'fs-extra' import { remove } from 'fs-extra'
import { sha256 } from '../helpers/core-utils' import { sha256 } from '../helpers/core-utils'
import { buildOriginallyPublishedAt, safeGetYoutubeDL } from '../helpers/youtube-dl' import { buildOriginallyPublishedAt, safeGetYoutubeDL } from '../helpers/youtube-dl'
import { getSettings, netrc } from './cli' import { getNetrc, getRemoteObjectOrDie, getSettings } from './cli'
let accessToken: string let accessToken: string
let client: { id: string, secret: string } let client: { id: string, secret: string }
@ -32,48 +32,30 @@ program
.option('-v, --verbose', 'Verbose mode') .option('-v, --verbose', 'Verbose mode')
.parse(process.argv) .parse(process.argv)
getSettings() Promise.all([ getSettings(), getNetrc() ])
.then(settings => { .then(([ settings, netrc ]) => {
if ((!program['url'] || !program['username'] || !program['targetUrl']) && settings.remotes.length === 0) { const { url, username, password } = getRemoteObjectOrDie(program, settings)
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.')
process.exit(-1) if (!program[ 'targetUrl' ]) {
} console.error('--targetUrl field is required.')
if ((!program[ 'url' ] || !program[ 'username' ]) && settings.remotes.length > 0) { process.exit(-1)
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 removeEndSlashes(url)
if (!program['password']) program['password'] = netrc.machines[program['url']].password removeEndSlashes(program[ 'targetUrl' ])
}
if ( const user = {
!program['targetUrl'] username: username,
) { password: password
if (!program['targetUrl']) console.error('--targetUrl field is required.') }
process.exit(-1)
}
removeEndSlashes(program['url']) run(user, url)
removeEndSlashes(program['targetUrl']) .catch(err => {
console.error(err)
const user = { process.exit(-1)
username: program['username'], })
password: program['password'] })
}
run(user, program['url'])
.catch(err => {
console.error(err)
process.exit(-1)
})
})
async function promptPassword () { async function promptPassword () {
return new Promise((res, rej) => { return new Promise((res, rej) => {
@ -116,7 +98,7 @@ async function run (user, url: string) {
const youtubeDL = await safeGetYoutubeDL() const youtubeDL = await safeGetYoutubeDL()
const options = [ '-j', '--flat-playlist', '--playlist-reverse' ] 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) { if (err) {
console.log(err.message) console.log(err.message)
process.exit(1) process.exit(1)
@ -133,20 +115,20 @@ async function run (user, url: string) {
console.log('Will download and upload %d videos.\n', infoArray.length) console.log('Will download and upload %d videos.\n', infoArray.length)
for (const info of infoArray) { 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) process.exit(0)
}) })
} }
function processVideo (info: any, languageCode: string, cwd: string, url: string, user) { function processVideo (info: any, languageCode: string, cwd: string, url: string, user) {
return new Promise(async res => { 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) 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') const result = await searchVideoWithSort(url, videoInfo.title, '-match')
@ -187,9 +169,9 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st
let tags = [] let tags = []
if (Array.isArray(videoInfo.tags)) { if (Array.isArray(videoInfo.tags)) {
tags = videoInfo.tags tags = videoInfo.tags
.filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min) .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
.map(t => t.normalize()) .map(t => t.normalize())
.slice(0, 5) .slice(0, 5)
} }
let thumbnailfile let thumbnailfile
@ -253,7 +235,7 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st
async function getCategory (categories: string[], url: string) { async function getCategory (categories: string[], url: string) {
if (!categories) return undefined if (!categories) return undefined
const categoryString = categories[0] const categoryString = categories[ 0 ]
if (categoryString === 'News & Politics') return 11 if (categoryString === 'News & Politics') return 11
@ -261,7 +243,7 @@ async function getCategory (categories: string[], url: string) {
const categoriesServer = res.body const categoriesServer = res.body
for (const key of Object.keys(categoriesServer)) { for (const key of Object.keys(categoriesServer)) {
const categoryServer = categoriesServer[key] const categoryServer = categoriesServer[ key ]
if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10) if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10)
} }
@ -285,12 +267,12 @@ function normalizeObject (obj: any) {
// Deprecated key // Deprecated key
if (key === 'resolution') continue if (key === 'resolution') continue
const value = obj[key] const value = obj[ key ]
if (typeof value === 'string') { if (typeof value === 'string') {
newObj[key] = value.normalize() newObj[ key ] = value.normalize()
} else { } 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 { getClient, login } from '../../shared/extra-utils'
import { uploadVideo } from '../../shared/extra-utils/' import { uploadVideo } from '../../shared/extra-utils/'
import { VideoPrivacy } from '../../shared/models/videos' import { VideoPrivacy } from '../../shared/models/videos'
import { netrc, getSettings } from './cli' import { getRemoteObjectOrDie, getSettings } from './cli'
program program
.name('upload') .name('upload')
@ -26,49 +26,15 @@ program
.option('-f, --file <file>', 'Video absolute file path') .option('-f, --file <file>', 'Video absolute file path')
.parse(process.argv) .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() getSettings()
.then(settings => { .then(settings => {
if ( const { url, username, password } = getRemoteObjectOrDie(program, settings)
(!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)
}
if ( if (!program['videoName'] || !program['file'] || !program['channelId']) {
(!program['url'] || if (!program['videoName']) console.error('--video-name is required.')
!program['username'] || if (!program['file']) console.error('--file is required.')
!program['password']) && if (!program['channelId']) console.error('--channel-id is required.')
(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']
) {
if (!program['videoName']) console.error('--video-name field is required.')
if (!program['file']) console.error('--file field is required.')
process.exit(-1) process.exit(-1)
} }
@ -77,28 +43,25 @@ getSettings()
process.exit(-1) process.exit(-1)
} }
run().catch(err => { run(url, username, password).catch(err => {
console.error(err) console.error(err)
process.exit(-1) process.exit(-1)
}) })
}) })
async function run () { async function run (url: string, username: string, password: string) {
const res = await getClient(program[ 'url' ]) const resClient = await getClient(program[ 'url' ])
const client = { const client = {
id: res.body.client_id, id: resClient.body.client_id,
secret: res.body.client_secret secret: resClient.body.client_secret
} }
const user = { const user = { username, password }
username: program[ 'username' ],
password: program[ 'password' ]
}
let accessToken: string let accessToken: string
try { try {
const res2 = await login(program[ 'url' ], client, user) const res = await login(url, client, user)
accessToken = res2.body.access_token accessToken = res.body.access_token
} catch (err) { } catch (err) {
throw new Error('Cannot authenticate. Please check your username/password.') throw new Error('Cannot authenticate. Please check your username/password.')
} }
@ -109,27 +72,32 @@ async function run () {
const videoAttributes = { const videoAttributes = {
name: program['videoName'], name: program['videoName'],
category: program['category'], category: program['category'] || undefined,
channelId: program['channelId'], channelId: program['channelId'],
licence: program['licence'], licence: program['licence'] || undefined,
language: program['language'], language: program['language'] || undefined,
nsfw: program['nsfw'], nsfw: program['nsfw'] !== undefined ? program['nsfw'] : false,
description: program['videoDescription'], description: program['videoDescription'] || '',
tags: program['tags'], tags: program['tags'] || [],
commentsEnabled: program['commentsEnabled'], commentsEnabled: program['commentsEnabled'] !== undefined ? program['commentsEnabled'] : true,
downloadEnabled: program['downloadEnabled'], downloadEnabled: program['downloadEnabled'] !== undefined ? program['downloadEnabled'] : true,
fixture: program['file'], fixture: program['file'],
thumbnailfile: program['thumbnail'], thumbnailfile: program['thumbnail'],
previewfile: program['preview'], previewfile: program['preview'],
waitTranscoding: true, waitTranscoding: true,
privacy: program['privacy'], privacy: program['privacy'] || VideoPrivacy.PUBLIC,
support: undefined support: undefined
} }
await uploadVideo(program[ 'url' ], accessToken, videoAttributes) try {
await uploadVideo(url, accessToken, videoAttributes)
console.log(`Video ${program['videoName']} uploaded.`) console.log(`Video ${program['videoName']} uploaded.`)
process.exit(0) process.exit(0)
} catch (err) {
console.log('coucou')
console.error(require('util').inspect(err))
process.exit(-1)
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------