- 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)