@ -155,51 +155,17 @@ exports._connectToDB = async (custom_connection_string = null) => {
}
}
exports . registerFileDB = async ( file _path , type , multiUserMode = null , sub = null , customPath = null , category = null , cropFileSettings = null , file _object = null ) => {
let db _path = null ;
const file _id = utils . removeFileExtension ( file _path ) ;
if ( ! file _object ) file _object = generateFileObject ( file _id , type , customPath || multiUserMode && multiUserMode . file _path , sub ) ;
if ( ! file _object ) {
logger . error ( ` Could not find associated JSON file for ${ type } file ${ file _id } ` ) ;
return false ;
}
utils . fixVideoMetadataPerms ( file _id , type , multiUserMode && multiUserMode . file _path ) ;
// add thumbnail path
file _object [ 'thumbnailPath' ] = utils . getDownloadedThumbnail ( file _id , type , customPath || multiUserMode && multiUserMode . file _path ) ;
// if category exists, only include essential info
if ( category ) file _object [ 'category' ] = { name : category [ 'name' ] , uid : category [ 'uid' ] } ;
// modify duration
if ( cropFileSettings ) {
file _object [ 'duration' ] = ( cropFileSettings . cropFileEnd || file _object . duration ) - cropFileSettings . cropFileStart ;
}
if ( multiUserMode ) file _object [ 'user_uid' ] = multiUserMode . user ;
const file _obj = await registerFileDBManual ( file _object ) ;
// remove metadata JSON if needed
if ( ! config _api . getConfigItem ( 'ytdl_include_metadata' ) ) {
utils . deleteJSONFile ( file _id , type , multiUserMode && multiUserMode . file _path )
}
return file _obj ;
}
exports . registerFileDB2 = async ( file _path , type , user _uid = null , category = null , sub _id = null , cropFileSettings = null , file _object = null ) => {
if ( ! file _object ) file _object = generateFileObject2 ( file _path , type ) ;
exports . registerFileDB = async ( file _path , type , user _uid = null , category = null , sub _id = null , cropFileSettings = null , file _object = null ) => {
if ( ! file _object ) file _object = generateFileObject ( file _path , type ) ;
if ( ! file _object ) {
logger . error ( ` Could not find associated JSON file for ${ type } file ${ file _path } ` ) ;
return false ;
}
utils . fixVideoMetadataPerms 2 ( file _path , type ) ;
utils . fixVideoMetadataPerms ( file _path , type ) ;
// add thumbnail path
file _object [ 'thumbnailPath' ] = utils . getDownloadedThumbnail 2 ( file _path , type ) ;
file _object [ 'thumbnailPath' ] = utils . getDownloadedThumbnail ( file _path ) ;
// if category exists, only include essential info
if ( category ) file _object [ 'category' ] = { name : category [ 'name' ] , uid : category [ 'uid' ] } ;
@ -216,7 +182,7 @@ exports.registerFileDB2 = async (file_path, type, user_uid = null, category = nu
// remove metadata JSON if needed
if ( ! config _api . getConfigItem ( 'ytdl_include_metadata' ) ) {
utils . deleteJSONFile 2 ( file _path , type )
utils . deleteJSONFile ( file _path , type )
}
return file _obj ;
@ -234,36 +200,7 @@ async function registerFileDBManual(file_object) {
return file _object ;
}
function generateFileObject ( id , type , customPath = null , sub = null ) {
if ( ! customPath && sub ) {
customPath = getAppendedBasePathSub ( sub , config _api . getConfigItem ( 'ytdl_subscriptions_base_path' ) ) ;
}
var jsonobj = ( type === 'audio' ) ? utils . getJSONMp3 ( id , customPath , true ) : utils . getJSONMp4 ( id , customPath , true ) ;
if ( ! jsonobj ) {
return null ;
}
const ext = ( type === 'audio' ) ? '.mp3' : '.mp4'
const file _path = utils . getTrueFileName ( jsonobj [ '_filename' ] , type ) ; // path.join(type === 'audio' ? audioFolderPath : videoFolderPath, id + ext);
// console.
var stats = fs . statSync ( path . join ( _ _dirname , file _path ) ) ;
var title = jsonobj . title ;
var url = jsonobj . webpage _url ;
var uploader = jsonobj . uploader ;
var upload _date = jsonobj . upload _date ;
upload _date = upload _date ? ` ${ upload _date . substring ( 0 , 4 ) } - ${ upload _date . substring ( 4 , 6 ) } - ${ upload _date . substring ( 6 , 8 ) } ` : 'N/A' ;
var size = stats . size ;
var thumbnail = jsonobj . thumbnail ;
var duration = jsonobj . duration ;
var isaudio = type === 'audio' ;
var description = jsonobj . description ;
var file _obj = new utils . File ( id , title , thumbnail , isaudio , duration , url , uploader , size , file _path , upload _date , description , jsonobj . view _count , jsonobj . height , jsonobj . abr ) ;
return file _obj ;
}
function generateFileObject2 ( file _path , type ) {
function generateFileObject ( file _path , type ) {
var jsonobj = utils . getJSON ( file _path , type ) ;
if ( ! jsonobj ) {
return null ;
@ -380,7 +317,7 @@ exports.importUnregisteredFiles = async () => {
const file _is _registered = ! ! ( files _with _same _url . find ( file _with _same _url => path . resolve ( file _with _same _url . path ) === path . resolve ( file . path ) ) ) ;
if ( ! file _is _registered ) {
// add additional info
await exports . registerFileDB 2 ( file [ 'path' ] , dir _to _check . type , dir _to _check . user _uid , null , dir _to _check . sub _id , null ) ;
await exports . registerFileDB ( file [ 'path' ] , dir _to _check . type , dir _to _check . user _uid , null , dir _to _check . sub _id , null ) ;
logger . verbose ( ` Added discovered file to the database: ${ file . id } ` ) ;
}
}
@ -388,24 +325,6 @@ exports.importUnregisteredFiles = async () => {
}
exports . preimportUnregisteredSubscriptionFile = async ( sub , appendedBasePath ) => {
const preimported _file _paths = [ ] ;
const files = await utils . getDownloadedFilesByType ( appendedBasePath , sub . type ) ;
for ( let i = 0 ; i < files . length ; i ++ ) {
const file = files [ i ] ;
// check if file exists in db, if not add it
const file _is _registered = await exports . getRecord ( 'files' , { id : file . id , sub _id : sub . id } ) ;
if ( ! file _is _registered ) {
// add additional info
await exports . registerFileDB2 ( file [ 'path' ] , sub . type , sub . user _uid , null , sub . id , null , file ) ;
preimported _file _paths . push ( file [ 'path' ] ) ;
logger . verbose ( ` Preemptively added subscription file to the database: ${ file . id } ` ) ;
}
}
return preimported _file _paths ;
}
exports . addMetadataPropertyToDB = async ( property _key ) => {
try {
const dirs _to _check = await exports . getFileDirectoriesAndDBs ( ) ;