From 0138af9237b77dd7d3a49260d164193b4048de84 Mon Sep 17 00:00:00 2001 From: Florent Fayolle Date: Sat, 2 Jun 2018 21:39:41 +0200 Subject: Add create-import-video-file-job command --- server/models/video/video.ts | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'server/models/video') 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' import { map, maxBy } from 'lodash' import * as magnetUtil from 'magnet-uri' import * as parseTorrent from 'parse-torrent' -import { join } from 'path' +import { join, extname } from 'path' import * as Sequelize from 'sequelize' import { AllowNull, @@ -32,6 +32,7 @@ import { VideoFilter } from '../../../shared/models/videos/video-query.type' import { createTorrentPromise, peertubeTruncate, + copyFilePromise, renamePromise, statPromise, unlinkPromise, @@ -1315,6 +1316,38 @@ export class VideoModel extends Model { this.VideoFiles.push(newVideoFile) } + async importVideoFile (inputFilePath: string) { + let updatedVideoFile = new VideoFileModel({ + resolution: (await getVideoFileResolution(inputFilePath)).videoFileResolution, + extname: extname(inputFilePath), + size: (await statPromise(inputFilePath)).size, + videoId: this.id + }) + + const outputPath = this.getVideoFilePath(updatedVideoFile) + await copyFilePromise(inputFilePath, outputPath) + + const currentVideoFile = this.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution) + const isNewVideoFile = !currentVideoFile + + if (!isNewVideoFile) { + if (currentVideoFile.extname !== updatedVideoFile.extname) { + await this.removeFile(currentVideoFile) + currentVideoFile.set('extname', updatedVideoFile.extname) + } + currentVideoFile.set('size', updatedVideoFile.size) + updatedVideoFile = currentVideoFile + } + + await this.createTorrentAndSetInfoHash(updatedVideoFile) + + await updatedVideoFile.save() + + if (isNewVideoFile) { + this.VideoFiles.push(updatedVideoFile) + } + } + getOriginalFileResolution () { const originalFilePath = this.getVideoFilePath(this.getOriginalFile()) -- cgit v1.2.3