@ -218,8 +218,11 @@ function deleteJSONFile(file_path, type) {
if ( fs . existsSync ( alternate _json _path ) ) fs . unlinkSync ( alternate _json _path ) ;
}
async function removeIDFromArchive ( archive _path , id ) {
let data = await fs . readFile ( archive _path , { encoding : 'utf-8' } ) ;
// archive helper functions
async function removeIDFromArchive ( archive _path , type , id ) {
const archive _file = path . join ( archive _path , ` archive_ ${ type } .txt ` ) ;
const data = await fs . readFile ( archive _file , { encoding : 'utf-8' } ) ;
if ( ! data ) {
logger . error ( 'Archive could not be found.' ) ;
return ;
@ -236,12 +239,34 @@ async function removeIDFromArchive(archive_path, id) {
}
}
if ( lastIndex === - 1 ) return null ;
const line = dataArray . splice ( lastIndex , 1 ) ; // remove the keyword id from the data Array
// UPDATE FILE WITH NEW DATA
const updatedData = dataArray . join ( '\n' ) ;
await fs . writeFile ( archive _path , updatedData ) ;
if ( line ) return line ;
await fs . writeFile ( archive _file , updatedData ) ;
if ( line ) return Array . isArray ( line ) && line . length === 1 ? line [ 0 ] : line ;
}
async function writeToBlacklist ( archive _folder , type , line ) {
let blacklistPath = path . join ( archive _folder , ( type === 'audio' ) ? 'blacklist_audio.txt' : 'blacklist_video.txt' ) ;
// adds newline to the beginning of the line
line . replace ( '\n' , '' ) ;
line . replace ( '\r' , '' ) ;
line = '\n' + line ;
await fs . appendFile ( blacklistPath , line ) ;
}
async function deleteFileFromArchive ( uid , type , archive _path , id , blacklistMode ) {
const archive _file = path . join ( archive _path , ` archive_ ${ type } .txt ` ) ;
if ( await fs . pathExists ( archive _path ) ) {
const line = id ? await removeIDFromArchive ( archive _path , type , id ) : null ;
if ( blacklistMode && line ) await writeToBlacklist ( archive _path , type , line ) ;
} else {
logger . info ( ` Could not find archive file for file ${ uid } . Creating... ` ) ;
await fs . close ( await fs . open ( archive _file , 'w' ) ) ;
}
}
function durationStringToNumber ( dur _str ) {
@ -471,6 +496,25 @@ const searchObjectByString = function(o, s) {
return o ;
}
function getArchiveFolder ( type , user _uid = null , sub = null ) {
const usersFolderPath = config _api . getConfigItem ( 'ytdl_users_base_path' ) ;
const subsFolderPath = config _api . getConfigItem ( 'ytdl_subscriptions_base_path' ) ;
if ( user _uid ) {
if ( sub ) {
return path . join ( usersFolderPath , user _uid , 'subscriptions' , 'archives' , sub . name ) ;
} else {
return path . join ( usersFolderPath , user _uid , type , 'archives' ) ;
}
} else {
if ( sub ) {
return path . join ( subsFolderPath , 'archives' , sub . name ) ;
} else {
return path . join ( 'appdata' , 'archives' ) ;
}
}
}
// objects
function File ( id , title , thumbnailURL , isAudio , duration , url , uploader , size , path , upload _date , description , view _count , height , abr ) {
@ -500,6 +544,8 @@ module.exports = {
fixVideoMetadataPerms : fixVideoMetadataPerms ,
deleteJSONFile : deleteJSONFile ,
removeIDFromArchive : removeIDFromArchive ,
writeToBlacklist : writeToBlacklist ,
deleteFileFromArchive : deleteFileFromArchive ,
getDownloadedFilesByType : getDownloadedFilesByType ,
createContainerZipFile : createContainerZipFile ,
durationStringToNumber : durationStringToNumber ,
@ -516,5 +562,6 @@ module.exports = {
restartServer : restartServer ,
injectArgs : injectArgs ,
searchObjectByString : searchObjectByString ,
getArchiveFolder : getArchiveFolder ,
File : File
}