import { inject , injectable } from 'inversify' ; import * as path from 'path' ; import * as apid from '../../../../api' ; import Recorded from '../../../db/entities/Recorded' ; import IRecordedDB from '../../db/IRecordedDB' ; import IVideoFileDB from '../../db/IVideoFileDB' ; import IEncodeManageModel from '../../service/encode/IEncodeManageModel' ; import IRecordedItemUtil from '../IRecordedItemUtil' ; import IEncodeApiModel from './IEncodeApiModel' ; @ injectable ( ) export default class EncodeApiModel implements IEncodeApiModel { private encodeManage : IEncodeManageModel ; private videoFileDB : IVideoFileDB ; private recordedDB : IRecordedDB ; private recordedItemUtil : IRecordedItemUtil ; constructor ( @ inject ( 'IEncodeManageModel' ) encodeManage , @ inject ( 'IVideoFileDB' ) videoFileDB , @ inject ( 'IRecordedDB' ) recordedDB , @ inject ( 'IRecordedItemUtil' ) recordedItemUtil ) { this . encodeManage = encodeManage ; this . videoFileDB = videoFileDB ; this . recordedDB = recordedDB ; this . recordedItemUtil = recordedItemUtil ; } param isHalfWidth return public async getAll ( isHalfWidth ) { const info = this . encodeManage . getEncodeInfo ( ) ; if ( info . runningQueue . length === 0 && info . waitQueue . length === 0 ) { return { runningItems : [ ] , waitItems : [ ] , } ; } let recordedIds = [ ] ; for ( const i of info . runningQueue ) { recordedIds . push ( i . recordedId ) ; } for ( const i of info . waitQueue ) { recordedIds . push ( i . recordedId ) ; } recordedIds = Array . from ( new Set ( recordedIds ) ) ; const recordedItems = await this . recordedDB . findIds ( recordedIds ) ; const recordedIndex = { } ; for ( const i of recordedItems ) { recordedIndex [ i . id ] = i ; } const result = { runningItems : [ ] , waitItems : [ ] , } ; for ( const i of info . runningQueue ) { const recordedItem = recordedIndex [ i . recordedId ] ; if ( typeof recordedItem === 'undefined' ) { continue ; } const info = { id : i . id , mode : i . mode , recorded : this . recordedItemUtil . convertRecordedToRecordedItem ( recordedItem , isHalfWidth , { } ) , } ; if ( typeof i . percent !== 'undefined' && typeof i . log !== 'undefined' ) { info . percent = i . percent ; info . log = i . log ; } result . runningItems . push ( info ) ; } for ( const i of info . waitQueue ) { const recordedItem = recordedIndex [ i . recordedId ] ; if ( typeof recordedItem === 'undefined' ) { continue ; } result . waitItems . push ( { id : i . id , mode : i . mode , recorded : this . recordedItemUtil . convertRecordedToRecordedItem ( recordedItem , isHalfWidth , { } ) , } ) ; } return result ; } param addOption return public async add ( addOption ) { if ( typeof addOption . parentDir === 'undefined' && ! ! addOption . isSaveSameDirectory === false ) { throw new Error ( 'OptionError' ) ; } let option ; if ( typeof addOption . parentDir !== 'undefined' ) { option = { recordedId : addOption . recordedId , sourceVideoFileId : addOption . sourceVideoFileId , parentDir : addOption . parentDir , directory : addOption . directory , mode : addOption . mode , removeOriginal : addOption . removeOriginal , } ; } else { const video = await this . videoFileDB . findId ( addOption . sourceVideoFileId ) ; if ( video === null ) { throw new Error ( 'VideoFileIsNotFound' ) ; } let directory = path . dirname ( video . filePath ) ; if ( directory === '.' || directory === path . posix . sep || directory === path . win32 . sep ) { directory = null ; } option = { recordedId : addOption . recordedId , sourceVideoFileId : addOption . sourceVideoFileId , parentDir : video . parentDirectoryName , mode : addOption . mode , removeOriginal : addOption . removeOriginal , } ; if ( directory !== null ) { option . directory = directory ; } } return await this . encodeManage . push ( option ) ; } param encodeId return public async cancel ( encodeId ) { await this . encodeManage . cancel ( encodeId ) ; } }