diff options
-rw-r--r-- | server/initializers/constants.ts | 2 | ||||
-rw-r--r-- | server/initializers/migrations/0585-video-file-names.ts | 55 | ||||
-rw-r--r-- | server/lib/job-queue/handlers/video-file-import.ts | 37 | ||||
-rw-r--r-- | server/lib/video-transcoding.ts | 1 | ||||
-rw-r--r-- | server/tests/cli/create-import-video-file-job.ts | 2 |
5 files changed, 74 insertions, 23 deletions
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 6b0984186..c4c7ffdac 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts | |||
@@ -24,7 +24,7 @@ import { CONFIG, registerConfigChangedHandler } from './config' | |||
24 | 24 | ||
25 | // --------------------------------------------------------------------------- | 25 | // --------------------------------------------------------------------------- |
26 | 26 | ||
27 | const LAST_MIGRATION_VERSION = 580 | 27 | const LAST_MIGRATION_VERSION = 585 |
28 | 28 | ||
29 | // --------------------------------------------------------------------------- | 29 | // --------------------------------------------------------------------------- |
30 | 30 | ||
diff --git a/server/initializers/migrations/0585-video-file-names.ts b/server/initializers/migrations/0585-video-file-names.ts new file mode 100644 index 000000000..dd5fec3a1 --- /dev/null +++ b/server/initializers/migrations/0585-video-file-names.ts | |||
@@ -0,0 +1,55 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
3 | async function up (utils: { | ||
4 | transaction: Sequelize.Transaction | ||
5 | queryInterface: Sequelize.QueryInterface | ||
6 | sequelize: Sequelize.Sequelize | ||
7 | db: any | ||
8 | }): Promise<void> { | ||
9 | for (const column of [ 'filename', 'fileUrl', 'torrentFilename', 'torrentUrl' ]) { | ||
10 | const data = { | ||
11 | type: Sequelize.STRING, | ||
12 | allowNull: true, | ||
13 | defaultValue: null | ||
14 | } | ||
15 | |||
16 | await utils.queryInterface.addColumn('videoFile', column, data) | ||
17 | } | ||
18 | |||
19 | // Generate filenames for webtorrent files | ||
20 | { | ||
21 | const webtorrentQuery = `SELECT "videoFile".id, "video".uuid, "videoFile".resolution, "videoFile".extname ` + | ||
22 | `FROM video INNER JOIN "videoFile" ON "videoFile"."videoId" = video.id` | ||
23 | |||
24 | const query = `UPDATE "videoFile" ` + | ||
25 | `SET filename = t.uuid || '-' || t.resolution || t.extname, ` + | ||
26 | `"torrentFilename" = t.uuid || '-' || t.resolution || '.torrent' ` + | ||
27 | `FROM (${webtorrentQuery}) AS t WHERE t.id = "videoFile"."id"` | ||
28 | |||
29 | await utils.sequelize.query(query) | ||
30 | } | ||
31 | |||
32 | // Generate filenames for HLS files | ||
33 | { | ||
34 | const hlsQuery = `SELECT "videoFile".id, "video".uuid, "videoFile".resolution, "videoFile".extname ` + | ||
35 | `FROM video ` + | ||
36 | `INNER JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."videoId" = video.id ` + | ||
37 | `INNER JOIN "videoFile" ON "videoFile"."videoStreamingPlaylistId" = "videoStreamingPlaylist".id` | ||
38 | |||
39 | const query = `UPDATE "videoFile" ` + | ||
40 | `SET filename = t.uuid || '-' || t.resolution || '-fragmented' || t.extname, ` + | ||
41 | `"torrentFilename" = t.uuid || '-' || t.resolution || '-hls.torrent' ` + | ||
42 | `FROM (${hlsQuery}) AS t WHERE t.id = "videoFile"."id"` | ||
43 | |||
44 | await utils.sequelize.query(query) | ||
45 | } | ||
46 | } | ||
47 | |||
48 | function down (options) { | ||
49 | throw new Error('Not implemented.') | ||
50 | } | ||
51 | |||
52 | export { | ||
53 | up, | ||
54 | down | ||
55 | } | ||
diff --git a/server/lib/job-queue/handlers/video-file-import.ts b/server/lib/job-queue/handlers/video-file-import.ts index 86c9b5c29..839916306 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' | |||
4 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' | 4 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' |
5 | import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths' | 5 | import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths' |
6 | import { UserModel } from '@server/models/account/user' | 6 | import { UserModel } from '@server/models/account/user' |
7 | import { MVideoFile, MVideoFullLight } from '@server/types/models' | 7 | import { MVideoFullLight } from '@server/types/models' |
8 | import { VideoFileImportPayload } from '@shared/models' | 8 | import { VideoFileImportPayload } from '@shared/models' |
9 | import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils' | 9 | import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils' |
10 | import { logger } from '../../../helpers/logger' | 10 | import { logger } from '../../../helpers/logger' |
@@ -56,16 +56,8 @@ async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) { | |||
56 | const fps = await getVideoFileFPS(inputFilePath) | 56 | const fps = await getVideoFileFPS(inputFilePath) |
57 | 57 | ||
58 | const fileExt = extname(inputFilePath) | 58 | const fileExt = extname(inputFilePath) |
59 | let updatedVideoFile = new VideoFileModel({ | ||
60 | resolution: videoFileResolution, | ||
61 | extname: fileExt, | ||
62 | filename: generateVideoFilename(video, false, videoFileResolution, fileExt), | ||
63 | size, | ||
64 | fps, | ||
65 | videoId: video.id | ||
66 | }) as MVideoFile | ||
67 | 59 | ||
68 | const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution) | 60 | const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === videoFileResolution) |
69 | 61 | ||
70 | if (currentVideoFile) { | 62 | if (currentVideoFile) { |
71 | // Remove old file and old torrent | 63 | // Remove old file and old torrent |
@@ -74,20 +66,23 @@ async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) { | |||
74 | // Remove the old video file from the array | 66 | // Remove the old video file from the array |
75 | video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile) | 67 | video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile) |
76 | 68 | ||
77 | // Update the database | 69 | await currentVideoFile.destroy() |
78 | currentVideoFile.extname = updatedVideoFile.extname | ||
79 | currentVideoFile.size = updatedVideoFile.size | ||
80 | currentVideoFile.fps = updatedVideoFile.fps | ||
81 | |||
82 | updatedVideoFile = currentVideoFile | ||
83 | } | 70 | } |
84 | 71 | ||
85 | const outputPath = getVideoFilePath(video, updatedVideoFile) | 72 | const newVideoFile = new VideoFileModel({ |
86 | await copy(inputFilePath, outputPath) | 73 | resolution: videoFileResolution, |
74 | extname: fileExt, | ||
75 | filename: generateVideoFilename(video, false, videoFileResolution, fileExt), | ||
76 | size, | ||
77 | fps, | ||
78 | videoId: video.id | ||
79 | }) | ||
87 | 80 | ||
88 | await createTorrentAndSetInfoHash(video, video, updatedVideoFile) | 81 | const outputPath = getVideoFilePath(video, newVideoFile) |
82 | await copy(inputFilePath, outputPath) | ||
89 | 83 | ||
90 | await updatedVideoFile.save() | 84 | video.VideoFiles.push(newVideoFile) |
85 | await createTorrentAndSetInfoHash(video, video, newVideoFile) | ||
91 | 86 | ||
92 | video.VideoFiles.push(updatedVideoFile) | 87 | await newVideoFile.save() |
93 | } | 88 | } |
diff --git a/server/lib/video-transcoding.ts b/server/lib/video-transcoding.ts index b366e2e44..e3cd18e25 100644 --- a/server/lib/video-transcoding.ts +++ b/server/lib/video-transcoding.ts | |||
@@ -166,6 +166,7 @@ async function mergeAudioVideofile (video: MVideoFullLight, resolution: VideoRes | |||
166 | 166 | ||
167 | // Important to do this before getVideoFilename() to take in account the new file extension | 167 | // Important to do this before getVideoFilename() to take in account the new file extension |
168 | inputVideoFile.extname = newExtname | 168 | inputVideoFile.extname = newExtname |
169 | inputVideoFile.filename = generateVideoFilename(video, false, inputVideoFile.resolution, newExtname) | ||
169 | 170 | ||
170 | const videoOutputPath = getVideoFilePath(video, inputVideoFile) | 171 | const videoOutputPath = getVideoFilePath(video, inputVideoFile) |
171 | // ffmpeg generated a new video file, so update the video duration | 172 | // ffmpeg generated a new video file, so update the video duration |
diff --git a/server/tests/cli/create-import-video-file-job.ts b/server/tests/cli/create-import-video-file-job.ts index 7eaf2c19e..49758ff56 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.expect | |||
22 | 22 | ||
23 | function assertVideoProperties (video: VideoFile, resolution: number, extname: string, size?: number) { | 23 | function assertVideoProperties (video: VideoFile, resolution: number, extname: string, size?: number) { |
24 | expect(video).to.have.nested.property('resolution.id', resolution) | 24 | expect(video).to.have.nested.property('resolution.id', resolution) |
25 | expect(video).to.have.property('magnetUri').that.includes(`.${extname}`) | ||
26 | expect(video).to.have.property('torrentUrl').that.includes(`-${resolution}.torrent`) | 25 | expect(video).to.have.property('torrentUrl').that.includes(`-${resolution}.torrent`) |
27 | expect(video).to.have.property('fileUrl').that.includes(`.${extname}`) | 26 | expect(video).to.have.property('fileUrl').that.includes(`.${extname}`) |
27 | expect(video).to.have.property('magnetUri').that.includes(`.${extname}`) | ||
28 | expect(video).to.have.property('size').that.is.above(0) | 28 | expect(video).to.have.property('size').that.is.above(0) |
29 | 29 | ||
30 | if (size) expect(video.size).to.equal(size) | 30 | if (size) expect(video.size).to.equal(size) |