aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/migrations/0030-video-magnet.js
blob: b9119d61c642c1fd600373c9d48023fa400a261b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
  Change video magnet structures
*/

const each = require('async/each')
const magnet = require('magnet-uri')
const mongoose = require('mongoose')

const Video = mongoose.model('Video')

exports.up = function (callback) {
  // Use of lean because the new Video scheme does not have magnetUri field
  Video.find({ }).lean().exec(function (err, videos) {
    if (err) throw err

    each(videos, function (video, callbackEach) {
      const parsed = magnet.decode(video.magnetUri)
      const infoHash = parsed.infoHash

      Video.load(video._id, function (err, videoObj) {
        if (err) return callbackEach(err)

        videoObj.magnet.infoHash = infoHash
        videoObj.save(callbackEach)
      })
    }, callback)
  })
}

exports.down = function (callback) {
  throw new Error('Not implemented.')
}