]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/migrations/0025-video-filenames.js
2 Rename thumbnails and video filenames to _id.extension
5 const each
= require('async/each')
6 const fs
= require('fs')
7 const path
= require('path')
8 const mongoose
= require('mongoose')
10 const constants
= require('../constants')
11 const logger
= require('../../helpers/logger')
13 const Video
= mongoose
.model('Video')
15 exports
.up = function (callback
) {
16 // Use of lean because the new Video scheme does not have filename field
17 Video
.find({ filename: { $ne: null } }).lean().exec(function (err
, videos
) {
20 each(videos
, function (video
, callbackEach
) {
21 const torrentName
= video
.filename
+ '.torrent'
22 const thumbnailName
= video
.thumbnail
23 const thumbnailExtension
= path
.extname(thumbnailName
)
24 const videoName
= video
.filename
25 const videoExtension
= path
.extname(videoName
)
27 const newTorrentName
= video
._id
+ '.torrent'
28 const newThumbnailName
= video
._id
+ thumbnailExtension
29 const newVideoName
= video
._id
+ videoExtension
31 const torrentsDir
= constants
.CONFIG
.STORAGE
.TORRENTS_DIR
32 const thumbnailsDir
= constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
33 const videosDir
= constants
.CONFIG
.STORAGE
.VIDEOS_DIR
35 logger
.info('Renaming %s to %s.', torrentsDir
+ torrentName
, torrentsDir
+ newTorrentName
)
36 fs
.renameSync(torrentsDir
+ torrentName
, torrentsDir
+ newTorrentName
)
38 logger
.info('Renaming %s to %s.', thumbnailsDir
+ thumbnailName
, thumbnailsDir
+ newThumbnailName
)
39 fs
.renameSync(thumbnailsDir
+ thumbnailName
, thumbnailsDir
+ newThumbnailName
)
41 logger
.info('Renaming %s to %s.', videosDir
+ videoName
, videosDir
+ newVideoName
)
42 fs
.renameSync(videosDir
+ videoName
, videosDir
+ newVideoName
)
44 Video
.load(video
._id
, function (err
, videoObj
) {
45 if (err
) return callbackEach(err
)
47 videoObj
.extname
= videoExtension
48 videoObj
.remoteId
= null
49 videoObj
.save(callbackEach
)
55 exports
.down = function (callback
) {
56 throw new Error('Not implemented.')