@ -1,22 +1,26 @@
const logger = require ( './logger' ) ;
const fs = require ( 'fs' ) ;
const { BehaviorSubject } = require ( 'rxjs' ) ;
exports . CONFIG _ITEMS = require ( './consts.js' ) [ 'CONFIG_ITEMS' ] ;
exports . descriptors = { } ; // to get rid of file locks when needed, TODO: move to youtube-dl.js
let CONFIG _ITEMS = require ( './consts.js' ) [ 'CONFIG_ITEMS' ] ;
const debugMode = process . env . YTDL _MODE === 'debug' ;
let configPath = debugMode ? '../src/assets/default.json' : 'appdata/default.json' ;
exports . config _updated = new BehaviorSubject ( ) ;
function initialize ( ) {
exports . initialize = ( ) => {
ensureConfigFileExists ( ) ;
ensureConfigItemsExist ( ) ;
}
function ensureConfigItemsExist ( ) {
const config _keys = Object . keys ( CONFIG_ITEMS ) ;
const config _keys = Object . keys ( exports. CONFIG_ITEMS ) ;
for ( let i = 0 ; i < config _keys . length ; i ++ ) {
const config _key = config _keys [ i ] ;
getConfigItem( config _key ) ;
exports. getConfigItem( config _key ) ;
}
}
@ -57,17 +61,17 @@ function getElementNameInConfig(path) {
/ * *
* Check if config exists . If not , write default config to config path
* /
function configExistsCheck ( ) {
exports . configExistsCheck = ( ) => {
let exists = fs . existsSync ( configPath ) ;
if ( ! exists ) {
setConfigFile( DEFAULT _CONFIG ) ;
exports. setConfigFile( DEFAULT _CONFIG ) ;
}
}
/ *
* Gets config file and returns as a json
* /
function getConfigFile ( ) {
exports . getConfigFile = ( ) => {
try {
let raw _data = fs . readFileSync ( configPath ) ;
let parsed _data = JSON . parse ( raw _data ) ;
@ -78,35 +82,40 @@ function getConfigFile() {
}
}
function setConfigFile ( config ) {
exports . setConfigFile = ( config ) => {
try {
const old _config = exports . getConfigFile ( ) ;
fs . writeFileSync ( configPath , JSON . stringify ( config , null , 2 ) ) ;
const changes = exports . findChangedConfigItems ( old _config , config ) ;
if ( changes . length > 0 ) {
for ( const change of changes ) exports . config _updated . next ( change ) ;
}
return true ;
} catch ( e ) {
return false ;
}
}
function getConfigItem ( key ) {
let config _json = getConfigFile( ) ;
if ( ! CONFIG_ITEMS [ key ] ) {
exports . getConfigItem = ( key ) => {
let config _json = exports. getConfigFile( ) ;
if ( ! exports. CONFIG_ITEMS [ key ] ) {
logger . error ( ` Config item with key ' ${ key } ' is not recognized. ` ) ;
return null ;
}
let path = CONFIG_ITEMS [ key ] [ 'path' ] ;
let path = exports. CONFIG_ITEMS [ key ] [ 'path' ] ;
const val = Object . byString ( config _json , path ) ;
if ( val === undefined && Object . byString ( DEFAULT _CONFIG , path ) !== undefined ) {
logger . warn ( ` Cannot find config with key ' ${ key } '. Creating one with the default value... ` ) ;
setConfigItem( key , Object . byString ( DEFAULT _CONFIG , path ) ) ;
exports. setConfigItem( key , Object . byString ( DEFAULT _CONFIG , path ) ) ;
return Object . byString ( DEFAULT _CONFIG , path ) ;
}
return Object . byString ( config _json , path ) ;
}
function setConfigItem ( key , value ) {
exports . setConfigItem = ( key , value ) => {
let success = false ;
let config _json = getConfigFile( ) ;
let path = CONFIG_ITEMS [ key ] [ 'path' ] ;
let config _json = exports. getConfigFile( ) ;
let path = exports. CONFIG_ITEMS [ key ] [ 'path' ] ;
let element _name = getElementNameInConfig ( path ) ;
let parent _path = getParentPath ( path ) ;
let parent _object = Object . byString ( config _json , parent _path ) ;
@ -118,20 +127,18 @@ function setConfigItem(key, value) {
parent _parent _object [ parent _parent _single _key ] = { } ;
parent _object = Object . byString ( config _json , parent _path ) ;
}
if ( value === 'false' ) value = false ;
if ( value === 'true' ) value = true ;
parent _object [ element _name ] = value ;
if ( value === 'false' || value === 'true' ) {
parent _object [ element _name ] = ( value === 'true' ) ;
} else {
parent _object [ element _name ] = value ;
}
success = setConfigFile ( config _json ) ;
success = exports . setConfigFile ( config _json ) ;
return success ;
}
function setConfigItems ( items ) {
exports . setConfigItems = ( items ) => {
let success = false ;
let config _json = getConfigFile( ) ;
let config _json = exports. getConfigFile( ) ;
for ( let i = 0 ; i < items . length ; i ++ ) {
let key = items [ i ] . key ;
let value = items [ i ] . value ;
@ -141,7 +148,7 @@ function setConfigItems(items) {
value = ( value === 'true' ) ;
}
let item _path = CONFIG_ITEMS [ key ] [ 'path' ] ;
let item _path = exports. CONFIG_ITEMS [ key ] [ 'path' ] ;
let item _parent _path = getParentPath ( item _path ) ;
let item _element _name = getElementNameInConfig ( item _path ) ;
@ -149,28 +156,41 @@ function setConfigItems(items) {
item _parent _object [ item _element _name ] = value ;
}
success = setConfigFile( config _json ) ;
success = exports. setConfigFile( config _json ) ;
return success ;
}
function globalArgsRequiresSafeDownload ( ) {
const globalArgs = getConfigItem( 'ytdl_custom_args' ) . split ( ',,' ) ;
exports . globalArgsRequiresSafeDownload = ( ) => {
const globalArgs = exports. getConfigItem( 'ytdl_custom_args' ) . split ( ',,' ) ;
const argsThatRequireSafeDownload = [ '--write-sub' , '--write-srt' , '--proxy' ] ;
const failedArgs = globalArgs . filter ( arg => argsThatRequireSafeDownload . includes ( arg ) ) ;
return failedArgs && failedArgs . length > 0 ;
}
module . exports = {
getConfigItem : getConfigItem ,
setConfigItem : setConfigItem ,
setConfigItems : setConfigItems ,
getConfigFile : getConfigFile ,
setConfigFile : setConfigFile ,
configExistsCheck : configExistsCheck ,
CONFIG _ITEMS : CONFIG _ITEMS ,
initialize : initialize ,
descriptors : { } ,
globalArgsRequiresSafeDownload : globalArgsRequiresSafeDownload
exports . findChangedConfigItems = ( old _config , new _config , path = '' , changedConfigItems = [ ] , depth = 0 ) => {
if ( typeof old _config === 'object' && typeof new _config === 'object' && depth < 3 ) {
for ( const key in old _config ) {
if ( Object . prototype . hasOwnProperty . call ( new _config , key ) ) {
exports . findChangedConfigItems ( old _config [ key ] , new _config [ key ] , ` ${ path } ${ path ? '.' : '' } ${ key } ` , changedConfigItems , depth + 1 ) ;
}
}
} else {
if ( JSON . stringify ( old _config ) !== JSON . stringify ( new _config ) ) {
const key = getConfigItemKeyByPath ( path ) ;
changedConfigItems . push ( {
key : key ? key : path . split ( '.' ) [ path . split ( '.' ) . length - 1 ] , // return key in CONFIG_ITEMS or the object key
old _value : JSON . parse ( JSON . stringify ( old _config ) ) ,
new _value : JSON . parse ( JSON . stringify ( new _config ) )
} ) ;
}
}
return changedConfigItems ;
}
function getConfigItemKeyByPath ( path ) {
const found _item = Object . values ( exports . CONFIG _ITEMS ) . find ( item => item . path === path ) ;
if ( found _item ) return found _item [ 'key' ] ;
else return null ;
}
const DEFAULT _CONFIG = {
@ -219,6 +239,7 @@ const DEFAULT_CONFIG = {
"use_telegram_API" : false ,
"telegram_bot_token" : "" ,
"telegram_chat_id" : "" ,
"telegram_webhook_proxy" : "" ,
"webhook_URL" : "" ,
"discord_webhook_URL" : "" ,
"slack_webhook_URL" : "" ,