aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/video/video.ts')
-rw-r--r--server/models/video/video.ts35
1 files changed, 34 insertions, 1 deletions
diff --git a/server/models/video/video.ts b/server/models/video/video.ts
index 5821ea397..2875e6685 100644
--- a/server/models/video/video.ts
+++ b/server/models/video/video.ts
@@ -2,7 +2,7 @@ import * as Bluebird from 'bluebird'
2import { map, maxBy } from 'lodash' 2import { map, maxBy } from 'lodash'
3import * as magnetUtil from 'magnet-uri' 3import * as magnetUtil from 'magnet-uri'
4import * as parseTorrent from 'parse-torrent' 4import * as parseTorrent from 'parse-torrent'
5import { join } from 'path' 5import { join, extname } from 'path'
6import * as Sequelize from 'sequelize' 6import * as Sequelize from 'sequelize'
7import { 7import {
8 AllowNull, 8 AllowNull,
@@ -32,6 +32,7 @@ import { VideoFilter } from '../../../shared/models/videos/video-query.type'
32import { 32import {
33 createTorrentPromise, 33 createTorrentPromise,
34 peertubeTruncate, 34 peertubeTruncate,
35 copyFilePromise,
35 renamePromise, 36 renamePromise,
36 statPromise, 37 statPromise,
37 unlinkPromise, 38 unlinkPromise,
@@ -1315,6 +1316,38 @@ export class VideoModel extends Model<VideoModel> {
1315 this.VideoFiles.push(newVideoFile) 1316 this.VideoFiles.push(newVideoFile)
1316 } 1317 }
1317 1318
1319 async importVideoFile (inputFilePath: string) {
1320 let updatedVideoFile = new VideoFileModel({
1321 resolution: (await getVideoFileResolution(inputFilePath)).videoFileResolution,
1322 extname: extname(inputFilePath),
1323 size: (await statPromise(inputFilePath)).size,
1324 videoId: this.id
1325 })
1326
1327 const outputPath = this.getVideoFilePath(updatedVideoFile)
1328 await copyFilePromise(inputFilePath, outputPath)
1329
1330 const currentVideoFile = this.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
1331 const isNewVideoFile = !currentVideoFile
1332
1333 if (!isNewVideoFile) {
1334 if (currentVideoFile.extname !== updatedVideoFile.extname) {
1335 await this.removeFile(currentVideoFile)
1336 currentVideoFile.set('extname', updatedVideoFile.extname)
1337 }
1338 currentVideoFile.set('size', updatedVideoFile.size)
1339 updatedVideoFile = currentVideoFile
1340 }
1341
1342 await this.createTorrentAndSetInfoHash(updatedVideoFile)
1343
1344 await updatedVideoFile.save()
1345
1346 if (isNewVideoFile) {
1347 this.VideoFiles.push(updatedVideoFile)
1348 }
1349 }
1350
1318 getOriginalFileResolution () { 1351 getOriginalFileResolution () {
1319 const originalFilePath = this.getVideoFilePath(this.getOriginalFile()) 1352 const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
1320 1353