]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos.js
First version with PostgreSQL
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos.js
index e2d3930740ff30bb5ee81f9f82ed0335177668f0..a61f2b2c9fe54fc57a451fa1eaa74b3bd7a1281b 100644 (file)
@@ -1,11 +1,13 @@
 'use strict'
 
 const express = require('express')
-const mongoose = require('mongoose')
+const fs = require('fs')
 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')
@@ -20,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({
@@ -86,20 +87,59 @@ function addVideo (req, res, next) {
 
   waterfall([
 
-    function insertIntoDB (callback) {
+    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 = {
         name: videoInfos.name,
-        filename: videoFile.filename,
+        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)
-      video.save(function (err, video) {
-        // Assert there are only one argument sent to the next function (video)
-        return callback(err, video)
+      const video = db.Video.build(videoData)
+
+      return callback(null, author, video)
+    },
+
+     // 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, author, 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)
       })
     },
 
@@ -126,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) {
@@ -138,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))
@@ -150,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)
@@ -164,7 +204,7 @@ function removeVideo (req, res, next) {
     function sendInformationToFriends (video, callback) {
       const params = {
         name: video.name,
-        magnetUri: video.magnetUri
+        remoteId: video.id
       }
 
       friends.removeVideoToFriends(params)
@@ -182,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)