mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 10:49:28 +02:00
First version with PostgreSQL
This commit is contained in:
parent
108626609e
commit
feb4bdfd9b
68 changed files with 1171 additions and 730 deletions
|
@ -1,13 +1,11 @@
|
|||
'use strict'
|
||||
|
||||
const express = require('express')
|
||||
const mongoose = require('mongoose')
|
||||
|
||||
const constants = require('../../initializers/constants')
|
||||
const db = require('../../initializers/database')
|
||||
const logger = require('../../helpers/logger')
|
||||
|
||||
const Client = mongoose.model('OAuthClient')
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/local', getLocalClient)
|
||||
|
@ -27,12 +25,12 @@ function getLocalClient (req, res, next) {
|
|||
return res.type('json').status(403).end()
|
||||
}
|
||||
|
||||
Client.loadFirstClient(function (err, client) {
|
||||
db.OAuthClient.loadFirstClient(function (err, client) {
|
||||
if (err) return next(err)
|
||||
if (!client) return next(new Error('No client available.'))
|
||||
|
||||
res.json({
|
||||
client_id: client._id,
|
||||
client_id: client.clientId,
|
||||
client_secret: client.clientSecret
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
'use strict'
|
||||
|
||||
const express = require('express')
|
||||
const mongoose = require('mongoose')
|
||||
const waterfall = require('async/waterfall')
|
||||
|
||||
const db = require('../../initializers/database')
|
||||
const logger = require('../../helpers/logger')
|
||||
const friends = require('../../lib/friends')
|
||||
const middlewares = require('../../middlewares')
|
||||
|
@ -15,7 +15,6 @@ const validators = middlewares.validators.pods
|
|||
const signatureValidator = middlewares.validators.remote.signature
|
||||
|
||||
const router = express.Router()
|
||||
const Pod = mongoose.model('Pod')
|
||||
|
||||
router.get('/', listPods)
|
||||
router.post('/',
|
||||
|
@ -53,15 +52,15 @@ function addPods (req, res, next) {
|
|||
|
||||
waterfall([
|
||||
function addPod (callback) {
|
||||
const pod = new Pod(informations)
|
||||
pod.save(function (err, podCreated) {
|
||||
const pod = db.Pod.build(informations)
|
||||
pod.save().asCallback(function (err, podCreated) {
|
||||
// Be sure about the number of parameters for the callback
|
||||
return callback(err, podCreated)
|
||||
})
|
||||
},
|
||||
|
||||
function sendMyVideos (podCreated, callback) {
|
||||
friends.sendOwnedVideosToPod(podCreated._id)
|
||||
friends.sendOwnedVideosToPod(podCreated.id)
|
||||
|
||||
callback(null)
|
||||
},
|
||||
|
@ -84,7 +83,7 @@ function addPods (req, res, next) {
|
|||
}
|
||||
|
||||
function listPods (req, res, next) {
|
||||
Pod.list(function (err, podsList) {
|
||||
db.Pod.list(function (err, podsList) {
|
||||
if (err) return next(err)
|
||||
|
||||
res.json(getFormatedPods(podsList))
|
||||
|
@ -111,11 +110,11 @@ function removePods (req, res, next) {
|
|||
|
||||
waterfall([
|
||||
function loadPod (callback) {
|
||||
Pod.loadByHost(host, callback)
|
||||
db.Pod.loadByHost(host, callback)
|
||||
},
|
||||
|
||||
function removePod (pod, callback) {
|
||||
pod.remove(callback)
|
||||
pod.destroy().asCallback(callback)
|
||||
}
|
||||
], function (err) {
|
||||
if (err) return next(err)
|
||||
|
|
|
@ -3,15 +3,15 @@
|
|||
const each = require('async/each')
|
||||
const eachSeries = require('async/eachSeries')
|
||||
const express = require('express')
|
||||
const mongoose = require('mongoose')
|
||||
const waterfall = require('async/waterfall')
|
||||
|
||||
const db = require('../../initializers/database')
|
||||
const middlewares = require('../../middlewares')
|
||||
const secureMiddleware = middlewares.secure
|
||||
const validators = middlewares.validators.remote
|
||||
const logger = require('../../helpers/logger')
|
||||
|
||||
const router = express.Router()
|
||||
const Video = mongoose.model('Video')
|
||||
|
||||
router.post('/videos',
|
||||
validators.signature,
|
||||
|
@ -53,34 +53,99 @@ function remoteVideos (req, res, next) {
|
|||
function addRemoteVideo (videoToCreateData, fromHost, callback) {
|
||||
logger.debug('Adding remote video "%s".', videoToCreateData.name)
|
||||
|
||||
const video = new Video(videoToCreateData)
|
||||
video.podHost = fromHost
|
||||
Video.generateThumbnailFromBase64(video, videoToCreateData.thumbnailBase64, function (err) {
|
||||
if (err) {
|
||||
logger.error('Cannot generate thumbnail from base 64 data.', { error: err })
|
||||
return callback(err)
|
||||
waterfall([
|
||||
|
||||
function findOrCreatePod (callback) {
|
||||
fromHost
|
||||
|
||||
const query = {
|
||||
where: {
|
||||
host: fromHost
|
||||
},
|
||||
defaults: {
|
||||
host: fromHost
|
||||
}
|
||||
}
|
||||
|
||||
db.Pod.findOrCreate(query).asCallback(function (err, result) {
|
||||
// [ instance, wasCreated ]
|
||||
return callback(err, result[0])
|
||||
})
|
||||
},
|
||||
|
||||
function findOrCreateAuthor (pod, callback) {
|
||||
const username = videoToCreateData.author
|
||||
|
||||
const query = {
|
||||
where: {
|
||||
name: username,
|
||||
podId: pod.id
|
||||
},
|
||||
defaults: {
|
||||
name: username,
|
||||
podId: pod.id
|
||||
}
|
||||
}
|
||||
|
||||
db.Author.findOrCreate(query).asCallback(function (err, result) {
|
||||
// [ instance, wasCreated ]
|
||||
return callback(err, result[0])
|
||||
})
|
||||
},
|
||||
|
||||
function createVideoObject (author, callback) {
|
||||
const videoData = {
|
||||
name: videoToCreateData.name,
|
||||
remoteId: videoToCreateData.remoteId,
|
||||
extname: videoToCreateData.extname,
|
||||
infoHash: videoToCreateData.infoHash,
|
||||
description: videoToCreateData.description,
|
||||
authorId: author.id,
|
||||
duration: videoToCreateData.duration,
|
||||
tags: videoToCreateData.tags
|
||||
}
|
||||
|
||||
const video = db.Video.build(videoData)
|
||||
|
||||
return callback(null, video)
|
||||
},
|
||||
|
||||
function generateThumbnail (video, callback) {
|
||||
db.Video.generateThumbnailFromBase64(video, videoToCreateData.thumbnailBase64, function (err) {
|
||||
if (err) {
|
||||
logger.error('Cannot generate thumbnail from base 64 data.', { error: err })
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
video.save().asCallback(callback)
|
||||
})
|
||||
},
|
||||
|
||||
function insertIntoDB (video, callback) {
|
||||
video.save().asCallback(callback)
|
||||
}
|
||||
|
||||
video.save(callback)
|
||||
})
|
||||
], callback)
|
||||
}
|
||||
|
||||
function removeRemoteVideo (videoToRemoveData, fromHost, callback) {
|
||||
// TODO: use bulkDestroy?
|
||||
|
||||
// We need the list because we have to remove some other stuffs (thumbnail etc)
|
||||
Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) {
|
||||
db.Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) {
|
||||
if (err) {
|
||||
logger.error('Cannot list videos from host and magnets.', { error: err })
|
||||
logger.error('Cannot list videos from host and remote id.', { error: err.message })
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
if (videosList.length === 0) {
|
||||
logger.error('No remote video was found for this pod.', { magnetUri: videoToRemoveData.magnetUri, podHost: fromHost })
|
||||
logger.error('No remote video was found for this pod.', { remoteId: videoToRemoveData.remoteId, podHost: fromHost })
|
||||
}
|
||||
|
||||
each(videosList, function (video, callbackEach) {
|
||||
logger.debug('Removing remote video %s.', video.magnetUri)
|
||||
logger.debug('Removing remote video %s.', video.remoteId)
|
||||
|
||||
video.remove(callbackEach)
|
||||
video.destroy().asCallback(callbackEach)
|
||||
}, callback)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,15 +1,13 @@
|
|||
'use strict'
|
||||
|
||||
const express = require('express')
|
||||
const mongoose = require('mongoose')
|
||||
|
||||
const constants = require('../../initializers/constants')
|
||||
const db = require('../../initializers/database')
|
||||
const middlewares = require('../../middlewares')
|
||||
const admin = middlewares.admin
|
||||
const oAuth = middlewares.oauth
|
||||
|
||||
const Request = mongoose.model('Request')
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/stats',
|
||||
|
@ -25,13 +23,13 @@ module.exports = router
|
|||
// ---------------------------------------------------------------------------
|
||||
|
||||
function getStatsRequests (req, res, next) {
|
||||
Request.list(function (err, requests) {
|
||||
db.Request.countTotalRequests(function (err, totalRequests) {
|
||||
if (err) return next(err)
|
||||
|
||||
return res.json({
|
||||
requests: requests,
|
||||
totalRequests: totalRequests,
|
||||
maxRequestsInParallel: constants.REQUESTS_IN_PARALLEL,
|
||||
remainingMilliSeconds: Request.remainingMilliSeconds(),
|
||||
remainingMilliSeconds: db.Request.remainingMilliSeconds(),
|
||||
milliSecondsInterval: constants.REQUESTS_INTERVAL
|
||||
})
|
||||
})
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
const each = require('async/each')
|
||||
const express = require('express')
|
||||
const mongoose = require('mongoose')
|
||||
const waterfall = require('async/waterfall')
|
||||
|
||||
const constants = require('../../initializers/constants')
|
||||
const db = require('../../initializers/database')
|
||||
const friends = require('../../lib/friends')
|
||||
const logger = require('../../helpers/logger')
|
||||
const middlewares = require('../../middlewares')
|
||||
|
@ -17,9 +17,6 @@ const validatorsPagination = middlewares.validators.pagination
|
|||
const validatorsSort = middlewares.validators.sort
|
||||
const validatorsUsers = middlewares.validators.users
|
||||
|
||||
const User = mongoose.model('User')
|
||||
const Video = mongoose.model('Video')
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/me', oAuth.authenticate, getUserInformation)
|
||||
|
@ -62,13 +59,13 @@ module.exports = router
|
|||
// ---------------------------------------------------------------------------
|
||||
|
||||
function createUser (req, res, next) {
|
||||
const user = new User({
|
||||
const user = db.User.build({
|
||||
username: req.body.username,
|
||||
password: req.body.password,
|
||||
role: constants.USER_ROLES.USER
|
||||
})
|
||||
|
||||
user.save(function (err, createdUser) {
|
||||
user.save().asCallback(function (err, createdUser) {
|
||||
if (err) return next(err)
|
||||
|
||||
return res.type('json').status(204).end()
|
||||
|
@ -76,7 +73,7 @@ function createUser (req, res, next) {
|
|||
}
|
||||
|
||||
function getUserInformation (req, res, next) {
|
||||
User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
|
||||
db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
|
||||
if (err) return next(err)
|
||||
|
||||
return res.json(user.toFormatedJSON())
|
||||
|
@ -84,7 +81,7 @@ function getUserInformation (req, res, next) {
|
|||
}
|
||||
|
||||
function listUsers (req, res, next) {
|
||||
User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) {
|
||||
db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) {
|
||||
if (err) return next(err)
|
||||
|
||||
res.json(getFormatedUsers(usersList, usersTotal))
|
||||
|
@ -94,18 +91,19 @@ function listUsers (req, res, next) {
|
|||
function removeUser (req, res, next) {
|
||||
waterfall([
|
||||
function getUser (callback) {
|
||||
User.loadById(req.params.id, callback)
|
||||
db.User.loadById(req.params.id, callback)
|
||||
},
|
||||
|
||||
// TODO: use foreignkey?
|
||||
function getVideos (user, callback) {
|
||||
Video.listOwnedByAuthor(user.username, function (err, videos) {
|
||||
db.Video.listOwnedByAuthor(user.username, function (err, videos) {
|
||||
return callback(err, user, videos)
|
||||
})
|
||||
},
|
||||
|
||||
function removeVideosFromDB (user, videos, callback) {
|
||||
each(videos, function (video, callbackEach) {
|
||||
video.remove(callbackEach)
|
||||
video.destroy().asCallback(callbackEach)
|
||||
}, function (err) {
|
||||
return callback(err, user, videos)
|
||||
})
|
||||
|
@ -115,7 +113,7 @@ function removeUser (req, res, next) {
|
|||
videos.forEach(function (video) {
|
||||
const params = {
|
||||
name: video.name,
|
||||
magnetUri: video.magnetUri
|
||||
remoteId: video.id
|
||||
}
|
||||
|
||||
friends.removeVideoToFriends(params)
|
||||
|
@ -125,7 +123,7 @@ function removeUser (req, res, next) {
|
|||
},
|
||||
|
||||
function removeUserFromDB (user, callback) {
|
||||
user.remove(callback)
|
||||
user.destroy().asCallback(callback)
|
||||
}
|
||||
], function andFinally (err) {
|
||||
if (err) {
|
||||
|
@ -138,11 +136,11 @@ function removeUser (req, res, next) {
|
|||
}
|
||||
|
||||
function updateUser (req, res, next) {
|
||||
User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
|
||||
db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
|
||||
if (err) return next(err)
|
||||
|
||||
user.password = req.body.password
|
||||
user.save(function (err) {
|
||||
user.save().asCallback(function (err) {
|
||||
if (err) return next(err)
|
||||
|
||||
return res.sendStatus(204)
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
|
||||
const express = require('express')
|
||||
const fs = require('fs')
|
||||
const mongoose = require('mongoose')
|
||||
const multer = require('multer')
|
||||
const path = require('path')
|
||||
const waterfall = require('async/waterfall')
|
||||
|
||||
const constants = require('../../initializers/constants')
|
||||
const db = require('../../initializers/database')
|
||||
const logger = require('../../helpers/logger')
|
||||
const friends = require('../../lib/friends')
|
||||
const middlewares = require('../../middlewares')
|
||||
|
@ -22,7 +22,6 @@ const sort = middlewares.sort
|
|||
const utils = require('../../helpers/utils')
|
||||
|
||||
const router = express.Router()
|
||||
const Video = mongoose.model('Video')
|
||||
|
||||
// multer configuration
|
||||
const storage = multer.diskStorage({
|
||||
|
@ -87,40 +86,60 @@ function addVideo (req, res, next) {
|
|||
const videoInfos = req.body
|
||||
|
||||
waterfall([
|
||||
function createVideoObject (callback) {
|
||||
const id = mongoose.Types.ObjectId()
|
||||
|
||||
function findOrCreateAuthor (callback) {
|
||||
const username = res.locals.oauth.token.user.username
|
||||
|
||||
const query = {
|
||||
where: {
|
||||
name: username,
|
||||
podId: null
|
||||
},
|
||||
defaults: {
|
||||
name: username,
|
||||
podId: null // null because it is OUR pod
|
||||
}
|
||||
}
|
||||
|
||||
db.Author.findOrCreate(query).asCallback(function (err, result) {
|
||||
// [ instance, wasCreated ]
|
||||
return callback(err, result[0])
|
||||
})
|
||||
},
|
||||
|
||||
function createVideoObject (author, callback) {
|
||||
const videoData = {
|
||||
_id: id,
|
||||
name: videoInfos.name,
|
||||
remoteId: null,
|
||||
extname: path.extname(videoFile.filename),
|
||||
description: videoInfos.description,
|
||||
author: res.locals.oauth.token.user.username,
|
||||
duration: videoFile.duration,
|
||||
tags: videoInfos.tags
|
||||
tags: videoInfos.tags,
|
||||
authorId: author.id
|
||||
}
|
||||
|
||||
const video = new Video(videoData)
|
||||
const video = db.Video.build(videoData)
|
||||
|
||||
return callback(null, video)
|
||||
return callback(null, author, video)
|
||||
},
|
||||
|
||||
// Set the videoname the same as the MongoDB id
|
||||
function renameVideoFile (video, callback) {
|
||||
// Set the videoname the same as the id
|
||||
function renameVideoFile (author, video, callback) {
|
||||
const videoDir = constants.CONFIG.STORAGE.VIDEOS_DIR
|
||||
const source = path.join(videoDir, videoFile.filename)
|
||||
const destination = path.join(videoDir, video.getVideoFilename())
|
||||
|
||||
fs.rename(source, destination, function (err) {
|
||||
return callback(err, video)
|
||||
return callback(err, author, video)
|
||||
})
|
||||
},
|
||||
|
||||
function insertIntoDB (video, callback) {
|
||||
video.save(function (err, video) {
|
||||
// Assert there are only one argument sent to the next function (video)
|
||||
return callback(err, video)
|
||||
function insertIntoDB (author, video, callback) {
|
||||
video.save().asCallback(function (err, videoCreated) {
|
||||
// Do not forget to add Author informations to the created video
|
||||
videoCreated.Author = author
|
||||
|
||||
return callback(err, videoCreated)
|
||||
})
|
||||
},
|
||||
|
||||
|
@ -147,7 +166,7 @@ function addVideo (req, res, next) {
|
|||
}
|
||||
|
||||
function getVideo (req, res, next) {
|
||||
Video.load(req.params.id, function (err, video) {
|
||||
db.Video.loadAndPopulateAuthorAndPod(req.params.id, function (err, video) {
|
||||
if (err) return next(err)
|
||||
|
||||
if (!video) {
|
||||
|
@ -159,7 +178,7 @@ function getVideo (req, res, next) {
|
|||
}
|
||||
|
||||
function listVideos (req, res, next) {
|
||||
Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) {
|
||||
db.Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) {
|
||||
if (err) return next(err)
|
||||
|
||||
res.json(getFormatedVideos(videosList, videosTotal))
|
||||
|
@ -171,11 +190,11 @@ function removeVideo (req, res, next) {
|
|||
|
||||
waterfall([
|
||||
function getVideo (callback) {
|
||||
Video.load(videoId, callback)
|
||||
db.Video.load(videoId, callback)
|
||||
},
|
||||
|
||||
function removeFromDB (video, callback) {
|
||||
video.remove(function (err) {
|
||||
video.destroy().asCallback(function (err) {
|
||||
if (err) return callback(err)
|
||||
|
||||
return callback(null, video)
|
||||
|
@ -185,7 +204,7 @@ function removeVideo (req, res, next) {
|
|||
function sendInformationToFriends (video, callback) {
|
||||
const params = {
|
||||
name: video.name,
|
||||
remoteId: video._id
|
||||
remoteId: video.id
|
||||
}
|
||||
|
||||
friends.removeVideoToFriends(params)
|
||||
|
@ -203,7 +222,7 @@ function removeVideo (req, res, next) {
|
|||
}
|
||||
|
||||
function searchVideos (req, res, next) {
|
||||
Video.search(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort,
|
||||
db.Video.searchAndPopulateAuthorAndPod(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort,
|
||||
function (err, videosList, videosTotal) {
|
||||
if (err) return next(err)
|
||||
|
||||
|
|
|
@ -3,13 +3,12 @@
|
|||
const parallel = require('async/parallel')
|
||||
const express = require('express')
|
||||
const fs = require('fs')
|
||||
const mongoose = require('mongoose')
|
||||
const path = require('path')
|
||||
const validator = require('express-validator').validator
|
||||
|
||||
const constants = require('../initializers/constants')
|
||||
const db = require('../initializers/database')
|
||||
|
||||
const Video = mongoose.model('Video')
|
||||
const router = express.Router()
|
||||
|
||||
const opengraphComment = '<!-- opengraph tags -->'
|
||||
|
@ -45,14 +44,14 @@ function addOpenGraphTags (htmlStringPage, video) {
|
|||
if (video.isOwned()) {
|
||||
basePreviewUrlHttp = constants.CONFIG.WEBSERVER.URL
|
||||
} else {
|
||||
basePreviewUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + video.podHost
|
||||
basePreviewUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + video.Author.Pod.host
|
||||
}
|
||||
|
||||
// We fetch the remote preview (bigger than the thumbnail)
|
||||
// This should not overhead the remote server since social websites put in a cache the OpenGraph tags
|
||||
// We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example)
|
||||
const previewUrl = basePreviewUrlHttp + constants.STATIC_PATHS.PREVIEWS + video.getPreviewName()
|
||||
const videoUrl = constants.CONFIG.WEBSERVER.URL + '/videos/watch/' + video._id
|
||||
const videoUrl = constants.CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id
|
||||
|
||||
const metaTags = {
|
||||
'og:type': 'video',
|
||||
|
@ -86,7 +85,7 @@ function generateWatchHtmlPage (req, res, next) {
|
|||
const videoId = req.params.id
|
||||
|
||||
// Let Angular application handle errors
|
||||
if (!validator.isMongoId(videoId)) return res.sendFile(indexPath)
|
||||
if (!validator.isUUID(videoId, 4)) return res.sendFile(indexPath)
|
||||
|
||||
parallel({
|
||||
file: function (callback) {
|
||||
|
@ -94,7 +93,7 @@ function generateWatchHtmlPage (req, res, next) {
|
|||
},
|
||||
|
||||
video: function (callback) {
|
||||
Video.load(videoId, callback)
|
||||
db.Video.loadAndPopulateAuthorAndPod(videoId, callback)
|
||||
}
|
||||
}, function (err, results) {
|
||||
if (err) return next(err)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue