diff options
author | Florent Fayolle <florent.fayolle69@gmail.com> | 2018-06-02 21:39:41 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-06-07 08:57:48 +0200 |
commit | 0138af9237b77dd7d3a49260d164193b4048de84 (patch) | |
tree | 5a377c7577cd99d5b324967187e10cd9fcc81383 /server/models/video/video.ts | |
parent | 157b62b1f4450c32bb7383ccacbea555ec607013 (diff) | |
download | PeerTube-0138af9237b77dd7d3a49260d164193b4048de84.tar.gz PeerTube-0138af9237b77dd7d3a49260d164193b4048de84.tar.zst PeerTube-0138af9237b77dd7d3a49260d164193b4048de84.zip |
Add create-import-video-file-job command
Diffstat (limited to 'server/models/video/video.ts')
-rw-r--r-- | server/models/video/video.ts | 35 |
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' | |||
2 | import { map, maxBy } from 'lodash' | 2 | import { map, maxBy } from 'lodash' |
3 | import * as magnetUtil from 'magnet-uri' | 3 | import * as magnetUtil from 'magnet-uri' |
4 | import * as parseTorrent from 'parse-torrent' | 4 | import * as parseTorrent from 'parse-torrent' |
5 | import { join } from 'path' | 5 | import { join, extname } from 'path' |
6 | import * as Sequelize from 'sequelize' | 6 | import * as Sequelize from 'sequelize' |
7 | import { | 7 | import { |
8 | AllowNull, | 8 | AllowNull, |
@@ -32,6 +32,7 @@ import { VideoFilter } from '../../../shared/models/videos/video-query.type' | |||
32 | import { | 32 | import { |
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 | ||