import { injectable } from 'inversify' ; import * as path from 'path' ; import FileUtil from '../../../util/FileUtil' ; import IEncodeFileManageModel from './IEncodeFileManageModel' ; @ injectable ( ) export default class EncodeFileManageModel implements IEncodeFileManageModel { private usedFileNameIndex : { [ name ] : boolean ; } = { } ; param outputDirPath param inputFilePath param suffix returns public async getFilePath ( outputDirPath , inputFilePath , suffix ) { const basefileName = path . basename ( inputFilePath , path . extname ( inputFilePath ) ) ; let result = null ; let conflict = 0 ; while ( 1 ) { let fileName = basefileName ; if ( conflict > 0 ) { fileName += `(${ conflict . toString ( 10 ) })` ; } fileName += suffix ; result = path . join ( outputDirPath , fileName ) ; if ( typeof this . usedFileNameIndex [ result ] !== 'undefined' ) { conflict ++ ; continue ; } try { await FileUtil . stat ( result ) ; conflict ++ ; } catch ( e ) { break ; } } if ( result === null ) { throw new Error ( 'GetFilePathError' ) ; } this . usedFileNameIndex [ result ] = true ; return result ; } param filePath public release ( filePath ) { delete this . usedFileNameIndex [ filePath ] ; } }