ActivityPub-federated video streaming platform using P2P directly in your web browser
Add video files migration
Chocobozzz2451916e4542
diff --git a/server/initializers/constants.ts b/server/initializers/constants.tsindex 6b0984186b..c4c7ffdac8 100644--- a/server/initializers/constants.ts+++ b/server/initializers/constants.ts@@ -24,7 +24,7 @@ import { CONFIG, registerConfigChangedHandler } from './config'// ----------------------------------------------------------------------------const LAST_MIGRATION_VERSION = 580+const LAST_MIGRATION_VERSION = 585// ---------------------------------------------------------------------------diff --git a/server/initializers/migrations/0585-video-file-names.ts b/server/initializers/migrations/0585-video-file-names.tsnew file mode 100644index 0000000000..dd5fec3a19--- /dev/null+++ b/server/initializers/migrations/0585-video-file-names.ts@@ -0,0 +1,55 @@+import * as Sequelize from 'sequelize'++async function up (utils: {+ transaction: Sequelize.Transaction+ queryInterface: Sequelize.QueryInterface+ sequelize: Sequelize.Sequelize+ db: any+}): Promise<void> {+ for (const column of [ 'filename', 'fileUrl', 'torrentFilename', 'torrentUrl' ]) {+ const data = {+ type: Sequelize.STRING,+ allowNull: true,+ defaultValue: null+ }++ await utils.queryInterface.addColumn('videoFile', column, data)+ }++ // Generate filenames for webtorrent files+ {+ const webtorrentQuery = `SELECT "videoFile".id, "video".uuid, "videoFile".resolution, "videoFile".extname ` ++ `FROM video INNER JOIN "videoFile" ON "videoFile"."videoId" = video.id`++ const query = `UPDATE "videoFile" ` ++ `SET filename = t.uuid || '-' || t.resolution || t.extname, ` ++ `"torrentFilename" = t.uuid || '-' || t.resolution || '.torrent' ` ++ `FROM (${webtorrentQuery}) AS t WHERE t.id = "videoFile"."id"`++ await utils.sequelize.query(query)+ }++ // Generate filenames for HLS files+ {+ const hlsQuery = `SELECT "videoFile".id, "video".uuid, "videoFile".resolution, "videoFile".extname ` ++ `FROM video ` ++ `INNER JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."videoId" = video.id ` ++ `INNER JOIN "videoFile" ON "videoFile"."videoStreamingPlaylistId" = "videoStreamingPlaylist".id`++ const query = `UPDATE "videoFile" ` ++ `SET filename = t.uuid || '-' || t.resolution || '-fragmented' || t.extname, ` ++ `"torrentFilename" = t.uuid || '-' || t.resolution || '-hls.torrent' ` ++ `FROM (${hlsQuery}) AS t WHERE t.id = "videoFile"."id"`++ await utils.sequelize.query(query)+ }+}++function down (options) {+ throw new Error('Not implemented.')+}++export {+ up,+ down+}diff --git a/server/lib/job-queue/handlers/video-file-import.ts b/server/lib/job-queue/handlers/video-file-import.tsindex 86c9b5c297..8399163069 100644--- a/server/lib/job-queue/handlers/video-file-import.ts+++ b/server/lib/job-queue/handlers/video-file-import.ts@@ -4,7 +4,7 @@ import { extname } from 'path'import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths'import { UserModel } from '@server/models/account/user'-import { MVideoFile, MVideoFullLight } from '@server/types/models'+import { MVideoFullLight } from '@server/types/models'import { VideoFileImportPayload } from '@shared/models'import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils'import { logger } from '../../../helpers/logger'@@ -56,16 +56,8 @@ async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {const fps = await getVideoFileFPS(inputFilePath)const fileExt = extname(inputFilePath)- let updatedVideoFile = new VideoFileModel({- resolution: videoFileResolution,- extname: fileExt,- filename: generateVideoFilename(video, false, videoFileResolution, fileExt),- size,- fps,- videoId: video.id- }) as MVideoFile- const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)+ const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === videoFileResolution)if (currentVideoFile) {// Remove old file and old torrent@@ -74,20 +66,23 @@ async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {// Remove the old video file from the arrayvideo.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)- // Update the database- currentVideoFile.extname = updatedVideoFile.extname- currentVideoFile.size = updatedVideoFile.size- currentVideoFile.fps = updatedVideoFile.fps-- updatedVideoFile = currentVideoFile+ await currentVideoFile.destroy()}- const outputPath = getVideoFilePath(video, updatedVideoFile)- await copy(inputFilePath, outputPath)+ const newVideoFile = new VideoFileModel({+ resolution: videoFileResolution,+ extname: fileExt,+ filename: generateVideoFilename(video, false, videoFileResolution, fileExt),+ size,+ fps,+ videoId: video.id+ })- await createTorrentAndSetInfoHash(video, video, updatedVideoFile)+ const outputPath = getVideoFilePath(video, newVideoFile)+ await copy(inputFilePath, outputPath)- await updatedVideoFile.save()+ video.VideoFiles.push(newVideoFile)+ await createTorrentAndSetInfoHash(video, video, newVideoFile)- video.VideoFiles.push(updatedVideoFile)+ await newVideoFile.save()}diff --git a/server/lib/video-transcoding.ts b/server/lib/video-transcoding.tsindex b366e2e44c..e3cd18e253 100644--- a/server/lib/video-transcoding.ts+++ b/server/lib/video-transcoding.ts@@ -166,6 +166,7 @@ async function mergeAudioVideofile (video: MVideoFullLight, resolution: VideoRes// Important to do this before getVideoFilename() to take in account the new file extensioninputVideoFile.extname = newExtname+ inputVideoFile.filename = generateVideoFilename(video, false, inputVideoFile.resolution, newExtname)const videoOutputPath = getVideoFilePath(video, inputVideoFile)// ffmpeg generated a new video file, so update the video durationdiff --git a/server/tests/cli/create-import-video-file-job.ts b/server/tests/cli/create-import-video-file-job.tsindex 7eaf2c19ee..49758ff56c 100644--- a/server/tests/cli/create-import-video-file-job.ts+++ b/server/tests/cli/create-import-video-file-job.ts@@ -22,9 +22,9 @@ const expect = chai.expectfunction assertVideoProperties (video: VideoFile, resolution: number, extname: string, size?: number) {expect(video).to.have.nested.property('resolution.id', resolution)- expect(video).to.have.property('magnetUri').that.includes(`.${extname}`)expect(video).to.have.property('torrentUrl').that.includes(`-${resolution}.torrent`)expect(video).to.have.property('fileUrl').that.includes(`.${extname}`)+ expect(video).to.have.property('magnetUri').that.includes(`.${extname}`)expect(video).to.have.property('size').that.is.above(0)if (size) expect(video.size).to.equal(size)