From: Chocobozzz Date: Sun, 2 Oct 2016 13:39:09 +0000 (+0200) Subject: Merge branch 'master' into webseed-merged X-Git-Tag: v0.0.1-alpha~697^2 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=a6375e69668ea42e19531c6bc68dcd37f3f7cbd7;p=oweals%2Fpeertube.git Merge branch 'master' into webseed-merged --- a6375e69668ea42e19531c6bc68dcd37f3f7cbd7 diff --cc .gitignore index ec5b4b2f0,18a2e808d..7ca89dca8 --- a/.gitignore +++ b/.gitignore @@@ -14,4 -14,7 +14,11 @@@ upload thumbnails config/production.yaml ffmpeg ++<<<<<<< HEAD +torrents ++======= + .tags + *.sublime-project + *.sublime-workspace + torrents/ ++>>>>>>> master diff --cc client/src/app/shared/auth/auth.service.ts index 584298fff,c57486d9a..a30c79c86 --- a/client/src/app/shared/auth/auth.service.ts +++ b/client/src/app/shared/auth/auth.service.ts @@@ -106,9 -123,9 +123,10 @@@ export class AuthService logout() { // TODO: make an HTTP request to revoke the tokens this.user = null; - User.flush(); + - this.setStatus(AuthStatus.LoggedIn); + AuthUser.flush(); + + this.setStatus(AuthStatus.LoggedOut); } refreshAccessToken() { diff --cc config/default.yaml index 9a8a57879,d32d3e17b..b44be31b0 --- a/config/default.yaml +++ b/config/default.yaml @@@ -17,10 -17,6 +17,7 @@@ storage uploads: 'uploads/' logs: 'logs/' thumbnails: 'thumbnails/' + torrents: 'torrents/' - network: - friends: [] - electron: debug: false diff --cc config/test-1.yaml index 0998eaea1,0a8dd3937..a59566cc4 --- a/config/test-1.yaml +++ b/config/test-1.yaml @@@ -14,8 -14,3 +14,4 @@@ storage uploads: 'test1/uploads/' logs: 'test1/logs/' thumbnails: 'test1/thumbnails/' + torrents: 'test1/torrents/' - - network: - friends: - - 'http://localhost:9002' diff --cc config/test-2.yaml index ec2cff811,40f410559..1b937898f --- a/config/test-2.yaml +++ b/config/test-2.yaml @@@ -14,8 -14,3 +14,4 @@@ storage uploads: 'test2/uploads/' logs: 'test2/logs/' thumbnails: 'test2/thumbnails/' + torrents: 'test2/torrents/' - - network: - friends: - - 'http://localhost:9003' diff --cc config/test-3.yaml index 24f5533e0,87b335228..e522c13e7 --- a/config/test-3.yaml +++ b/config/test-3.yaml @@@ -14,8 -14,3 +14,4 @@@ storage uploads: 'test3/uploads/' logs: 'test3/logs/' thumbnails: 'test3/thumbnails/' + torrents: 'test3/torrents/' - - network: - friends: - - 'http://localhost:9001' diff --cc config/test-4.yaml index 1f884dbf2,22abc0a58..e30cd7978 --- a/config/test-4.yaml +++ b/config/test-4.yaml @@@ -14,8 -14,3 +14,4 @@@ storage uploads: 'test4/uploads/' logs: 'test4/logs/' thumbnails: 'test4/thumbnails/' + torrents: 'test4/torrents/' - - network: - friends: - - 'http://localhost:9002' diff --cc config/test-5.yaml index 08ed9f068,af5619994..3a54599f5 --- a/config/test-5.yaml +++ b/config/test-5.yaml @@@ -14,9 -14,3 +14,4 @@@ storage uploads: 'test5/uploads/' logs: 'test5/logs/' thumbnails: 'test5/thumbnails/' + torrents: 'test5/torrents/' - - network: - friends: - - 'http://localhost:9001' - - 'http://localhost:9004' diff --cc config/test-6.yaml index a57784cca,5b8bf306b..31608add2 --- a/config/test-6.yaml +++ b/config/test-6.yaml @@@ -14,10 -14,3 +14,4 @@@ storage uploads: 'test6/uploads/' logs: 'test6/logs/' thumbnails: 'test6/thumbnails/' + torrents: 'test6/torrents/' - - network: - friends: - - 'http://localhost:9001' - - 'http://localhost:9002' - - 'http://localhost:9003' diff --cc package.json index 63d014376,27958dd43..59c7a4332 --- a/package.json +++ b/package.json @@@ -59,8 -60,8 +60,7 @@@ "request": "^2.57.0", "request-replay": "^1.0.2", "scripty": "^1.5.0", - "segfault-handler": "^1.0.0", "ursa": "^0.9.1", - "webtorrent": "^0.96.0", "winston": "^2.1.1", "ws": "^1.1.1" }, diff --cc server.js index 0033ed1db,ca275ccec..5feb21476 --- a/server.js +++ b/server.js @@@ -32,9 -32,13 +32,10 @@@ if (miss.length !== 0) // ----------- PeerTube modules ----------- const customValidators = require('./server/helpers/custom-validators') const installer = require('./server/initializers/installer') + const migrator = require('./server/initializers/migrator') const mongoose = require('mongoose') const routes = require('./server/controllers') -const utils = require('./server/helpers/utils') -const webtorrent = require('./server/lib/webtorrent') const Request = mongoose.model('Request') -const Video = mongoose.model('Video') // Get configurations const port = config.get('listen.port') @@@ -125,14 -125,38 +129,19 @@@ app.use(function (err, req, res, next) installer.installApplication(function (err) { if (err) throw err - // ----------- Make the server listening ----------- - server.listen(port, function () { - // Activate the pool requests - Request.activate() + // Run the migration scripts if needed + migrator.migrate(function (err) { + if (err) throw err + - // Create/activate the webtorrent module - webtorrent.create(function () { - function cleanForExit () { - utils.cleanForExit(webtorrent.app) - } - - function exitGracefullyOnSignal () { - process.exit(-1) - } - - process.on('exit', cleanForExit) - process.on('SIGINT', exitGracefullyOnSignal) - process.on('SIGTERM', exitGracefullyOnSignal) - - // ----------- Make the server listening ----------- - server.listen(port, function () { - // Activate the pool requests - Request.activate() - - Video.seedAllExisting(function (err) { - if (err) throw err - - logger.info('Seeded all the videos') - logger.info('Server listening on port %d', port) - app.emit('ready') - }) - }) ++ // ----------- Make the server listening ----------- ++ server.listen(port, function () { ++ // Activate the pool requests ++ Request.activate() + - logger.info('Seeded all the videos') - logger.info('Server listening on port %d', port) - app.emit('ready') ++ logger.info('Seeded all the videos') ++ logger.info('Server listening on port %d', port) ++ app.emit('ready') + }) }) }) diff --cc server/initializers/constants.js index e0ea188af,b1d033377..be2e3e943 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@@ -35,67 -117,58 +117,62 @@@ const REQUESTS_LIMIT = 1 // Number of requests to retry for replay requests module const RETRY_REQUESTS = 5 - // Sortable columns per schema - const SEARCHABLE_COLUMNS = { - VIDEOS: [ 'name', 'magnetUri', 'podUrl', 'author', 'tags' ] - } - - // Seeds in parallel we send to electron when "seed all" - // Once a video is in seeding state we seed another video etc - const SEEDS_IN_PARALLEL = 3 + // --------------------------------------------------------------------------- - // Sortable columns per schema - const SORTABLE_COLUMNS = { - VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdDate', '-createdDate' ] - } + // Password encryption + const BCRYPT_SALT_SIZE = 10 +// Express static paths (router) +const STATIC_PATHS = { + THUMBNAILS: '/static/thumbnails', + TORRENTS: '/static/torrents/', + WEBSEED: '/static/webseed/' +} + // Videos thumbnail size const THUMBNAILS_SIZE = '200x110' - const VIDEOS_CONSTRAINTS_FIELDS = { - NAME: { min: 3, max: 50 }, // Length - DESCRIPTION: { min: 3, max: 250 }, // Length - MAGNET_URI: { min: 10 }, // Length - DURATION: { min: 1, max: 7200 }, // Number - AUTHOR: { min: 3, max: 20 }, // Length - TAGS: { min: 1, max: 3 }, // Number of total tags - TAG: { min: 2, max: 10 }, // Length - THUMBNAIL: { min: 2, max: 30 }, - THUMBNAIL64: { min: 0, max: 20000 } // Bytes -// Path for access to thumbnails with express router -const THUMBNAILS_STATIC_PATH = '/static/thumbnails' - + const USER_ROLES = { + ADMIN: 'admin', + USER: 'user' } + // Seeds in parallel we send to electron when "seed all" + // Once a video is in seeding state we seed another video etc + const SEEDS_IN_PARALLEL = 3 + + // --------------------------------------------------------------------------- + // Special constants for a test instance if (isTestInstance() === true) { + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14 FRIEND_SCORE.BASE = 20 - INTERVAL = 10000 - VIDEOS_CONSTRAINTS_FIELDS.DURATION.max = 14 + REQUESTS_INTERVAL = 10000 } // --------------------------------------------------------------------------- module.exports = { - API_VERSION: API_VERSION, - FRIEND_SCORE: FRIEND_SCORE, - INTERVAL: INTERVAL, - OAUTH_LIFETIME: OAUTH_LIFETIME, - PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT, - PODS_SCORE: PODS_SCORE, - REQUESTS_IN_PARALLEL: REQUESTS_IN_PARALLEL, - REQUESTS_LIMIT: REQUESTS_LIMIT, - RETRY_REQUESTS: RETRY_REQUESTS, - SEARCHABLE_COLUMNS: SEARCHABLE_COLUMNS, - SEEDS_IN_PARALLEL: SEEDS_IN_PARALLEL, - SORTABLE_COLUMNS: SORTABLE_COLUMNS, - STATIC_PATHS: STATIC_PATHS, - THUMBNAILS_SIZE: THUMBNAILS_SIZE, - VIDEOS_CONSTRAINTS_FIELDS: VIDEOS_CONSTRAINTS_FIELDS + API_VERSION, + BCRYPT_SALT_SIZE, + CONFIG, + CONSTRAINTS_FIELDS, + FRIEND_SCORE, + LAST_MONGO_SCHEMA_VERSION, + MONGO_MIGRATION_SCRIPTS, + OAUTH_LIFETIME, + PAGINATION_COUNT_DEFAULT, + PODS_SCORE, + REQUESTS_IN_PARALLEL, + REQUESTS_INTERVAL, + REQUESTS_LIMIT, + RETRY_REQUESTS, + SEARCHABLE_COLUMNS, + SEEDS_IN_PARALLEL, + SORTABLE_COLUMNS, ++ STATIC_PATHS, + THUMBNAILS_SIZE, - THUMBNAILS_STATIC_PATH, + USER_ROLES } // --------------------------------------------------------------------------- diff --cc server/models/video.js index 14e0df6f2,b9999c8f6..7d073cffa --- a/server/models/video.js +++ b/server/models/video.js @@@ -11,17 -8,11 +11,18 @@@ const magnet = require('magnet-uri' const mongoose = require('mongoose') const constants = require('../initializers/constants') - const customValidators = require('../helpers/custom-validators') + const customVideosValidators = require('../helpers/custom-validators').videos const logger = require('../helpers/logger') + const modelUtils = require('./utils') const utils = require('../helpers/utils') -const webtorrent = require('../lib/webtorrent') + +const http = config.get('webserver.https') === true ? 'https' : 'http' +const host = config.get('webserver.host') +const port = config.get('webserver.port') +const uploadsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads')) +const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails')) +const torrentsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.torrents')) +const webseedBaseUrl = http + '://' + host + ':' + port + constants.STATIC_PATHS.WEBSEED // --------------------------------------------------------------------------- @@@ -62,14 -53,16 +63,15 @@@ VideoSchema.methods = } VideoSchema.statics = { - getDurationFromFile: getDurationFromFile, - list: list, - listByUrlAndMagnet: listByUrlAndMagnet, - listByUrls: listByUrls, - listOwned: listOwned, - listRemotes: listRemotes, - load: load, - search: search + getDurationFromFile, + listForApi, + listByUrlAndMagnet, + listByUrls, + listOwned, + listOwnedByAuthor, + listRemotes, + load, - search, - seedAllExisting ++ search } VideoSchema.pre('remove', function (next) { @@@ -101,25 -94,12 +103,25 @@@ VideoSchema.pre('save', function (next const tasks = [] if (video.isOwned()) { - const videoPath = pathUtils.join(uploadsDir, video.filename) - this.podUrl = http + '://' + host + ':' + port + const videoPath = pathUtils.join(constants.CONFIG.STORAGE.UPLOAD_DIR, video.filename) + this.podUrl = constants.CONFIG.WEBSERVER.URL tasks.push( + // TODO: refractoring function (callback) { - seed(videoPath, callback) + createTorrent(videoPath, { announceList: [ [ 'ws://' + host + ':' + port + '/tracker/socket' ] ], urlList: [ webseedBaseUrl + video.filename ] }, function (err, torrent) { + if (err) return callback(err) + + fs.writeFile(torrentsDir + video.filename + '.torrent', torrent, function (err) { + if (err) return callback(err) + + const parsedTorrent = parseTorrent(torrent) + parsedTorrent.xs = video.podUrl + constants.STATIC_PATHS.TORRENTS + video.filename + '.torrent' + video.magnetUri = magnet.encode(parsedTorrent) + + callback(null) + }) + }) }, function (callback) { createThumbnail(videoPath, callback) @@@ -242,32 -227,24 +248,13 @@@ function search (value, field, start, c query[field] = new RegExp(value) } - findWithCount.call(this, query, start, count, sort, callback) + modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) } -function seedAllExisting (callback) { - listOwned.call(this, function (err, videos) { - if (err) return callback(err) - - eachLimit(videos, constants.SEEDS_IN_PARALLEL, function (video, callbackEach) { - const videoPath = pathUtils.join(constants.CONFIG.STORAGE.UPLOAD_DIR, video.filename) - seed(videoPath, callbackEach) - }, callback) - }) -} - // --------------------------------------------------------------------------- - function findWithCount (query, start, count, sort, callback) { - const self = this - - parallel([ - function (asyncCallback) { - self.find(query).skip(start).limit(count).sort(sort).exec(asyncCallback) - }, - function (asyncCallback) { - self.count(query, asyncCallback) - } - ], function (err, results) { - if (err) return callback(err) - - const videos = results[0] - const totalVideos = results[1] - return callback(null, videos, totalVideos) - }) - } - function removeThumbnail (video, callback) { - fs.unlink(thumbnailsDir + video.thumbnail, callback) + fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.thumbnail, callback) } function removeFile (video, callback) {