+function addRemoteVideo (videoToCreateData, fromPod, finalCallback) {
+ logger.debug('Adding remote video "%s".', videoToCreateData.name)
+
+ waterfall([
+
+ function startTransaction (callback) {
+ db.sequelize.transaction().asCallback(function (err, t) {
+ return callback(err, t)
+ })
+ },
+
+ function findOrCreateAuthor (t, callback) {
+ const name = videoToCreateData.author
+ const podId = fromPod.id
+ // This author is from another pod so we do not associate a user
+ const userId = null
+
+ db.Author.findOrCreateAuthor(name, podId, userId, t, function (err, authorInstance) {
+ return callback(err, t, authorInstance)
+ })
+ },
+
+ function findOrCreateTags (t, author, callback) {
+ const tags = videoToCreateData.tags
+
+ db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) {
+ return callback(err, t, author, tagInstances)
+ })
+ },
+
+ function createVideoObject (t, author, tagInstances, callback) {
+ const videoData = {
+ name: videoToCreateData.name,
+ remoteId: videoToCreateData.remoteId,
+ extname: videoToCreateData.extname,
+ infoHash: videoToCreateData.infoHash,
+ description: videoToCreateData.description,
+ authorId: author.id,
+ duration: videoToCreateData.duration,
+ createdAt: videoToCreateData.createdAt,
+ updatedAt: videoToCreateData.updatedAt
+ }
+
+ const video = db.Video.build(videoData)
+
+ return callback(null, t, tagInstances, video)
+ },
+
+ function generateThumbnail (t, tagInstances, video, callback) {
+ db.Video.generateThumbnailFromData(video, videoToCreateData.thumbnailData, function (err) {
+ if (err) {
+ logger.error('Cannot generate thumbnail from data.', { error: err })
+ return callback(err)
+ }
+
+ return callback(err, t, tagInstances, video)
+ })
+ },
+
+ function insertVideoIntoDB (t, tagInstances, video, callback) {
+ const options = {
+ transaction: t
+ }
+
+ video.save(options).asCallback(function (err, videoCreated) {
+ return callback(err, t, tagInstances, videoCreated)
+ })
+ },
+
+ function associateTagsToVideo (t, tagInstances, video, callback) {
+ const options = { transaction: t }
+
+ video.setTags(tagInstances, options).asCallback(function (err) {
+ return callback(err, t)
+ })
+ }
+
+ ], function (err, t) {
+ if (err) {
+ logger.error('Cannot insert the remote video.')