fix: file path error when start as daemon
This commit is contained in:
parent
53ae085c57
commit
948aabd9ab
19
command.py
19
command.py
@ -136,12 +136,11 @@ def cmd_pause(bot, user, text, command, parameter):
|
||||
|
||||
|
||||
def cmd_play_file(bot, user, text, command, parameter):
|
||||
music_folder = var.config.get('bot', 'music_folder')
|
||||
# if parameter is {index}
|
||||
if parameter.isdigit():
|
||||
files = util.get_recursive_filelist_sorted(music_folder)
|
||||
files = util.get_recursive_filelist_sorted(var.music_folder)
|
||||
if int(parameter) < len(files):
|
||||
filename = files[int(parameter)].replace(music_folder, '')
|
||||
filename = files[int(parameter)].replace(var.music_folder, '')
|
||||
music = {'type': 'file',
|
||||
'path': filename,
|
||||
'user': user}
|
||||
@ -152,8 +151,8 @@ def cmd_play_file(bot, user, text, command, parameter):
|
||||
# if parameter is {path}
|
||||
else:
|
||||
# sanitize "../" and so on
|
||||
path = os.path.abspath(os.path.join(music_folder, parameter))
|
||||
if not path.startswith(os.path.abspath(music_folder)):
|
||||
path = os.path.abspath(os.path.join(var.music_folder, parameter))
|
||||
if not path.startswith(os.path.abspath(var.music_folder)):
|
||||
bot.send_msg(constants.strings('no_file'), text)
|
||||
return
|
||||
|
||||
@ -174,8 +173,8 @@ def cmd_play_file(bot, user, text, command, parameter):
|
||||
else:
|
||||
parameter = ""
|
||||
|
||||
files = util.get_recursive_filelist_sorted(music_folder)
|
||||
music_library = util.Dir(music_folder)
|
||||
files = util.get_recursive_filelist_sorted(var.music_folder)
|
||||
music_library = util.Dir(var.music_folder)
|
||||
for file in files:
|
||||
music_library.add_file(file)
|
||||
|
||||
@ -199,7 +198,7 @@ def cmd_play_file(bot, user, text, command, parameter):
|
||||
|
||||
else:
|
||||
# try to do a partial match
|
||||
files = util.get_recursive_filelist_sorted(music_folder)
|
||||
files = util.get_recursive_filelist_sorted(var.music_folder)
|
||||
matches = [(index, file) for index, file in enumerate(files) if parameter.lower() in file.lower()]
|
||||
if len(matches) == 0:
|
||||
bot.send_msg(constants.strings('no_file'), text)
|
||||
@ -218,7 +217,7 @@ def cmd_play_file(bot, user, text, command, parameter):
|
||||
|
||||
|
||||
def cmd_play_file_match(bot, user, text, command, parameter):
|
||||
music_folder = var.config.get('bot', 'music_folder')
|
||||
music_folder = var.music_folder
|
||||
if parameter:
|
||||
files = util.get_recursive_filelist_sorted(music_folder)
|
||||
msgs = [ constants.strings('multiple_file_added')]
|
||||
@ -561,7 +560,7 @@ def cmd_remove(bot, user, text, command, parameter):
|
||||
|
||||
|
||||
def cmd_list_file(bot, user, text, command, parameter):
|
||||
folder_path = var.config.get('bot', 'music_folder')
|
||||
folder_path = var.music_folder
|
||||
|
||||
files = util.get_recursive_filelist_sorted(folder_path)
|
||||
msgs = [ "<br> <b>Files available:</b>" if not parameter else "<br> <b>Matched files:</b>" ]
|
||||
|
10
interface.py
10
interface.py
@ -153,7 +153,7 @@ def post():
|
||||
if request.form:
|
||||
logging.debug("Post request: "+ str(request.form))
|
||||
if 'add_file_bottom' in request.form and ".." not in request.form['add_file_bottom']:
|
||||
path = var.config.get('bot', 'music_folder') + request.form['add_file_bottom']
|
||||
path = var.music_folder + request.form['add_file_bottom']
|
||||
if os.path.isfile(path):
|
||||
item = {'type': 'file',
|
||||
'path' : request.form['add_file_bottom'],
|
||||
@ -163,7 +163,7 @@ def post():
|
||||
logging.info('web: add to playlist(bottom): ' + util.format_debug_song_string(item))
|
||||
|
||||
elif 'add_file_next' in request.form and ".." not in request.form['add_file_next']:
|
||||
path = var.config.get('bot', 'music_folder') + request.form['add_file_next']
|
||||
path = var.music_folder + request.form['add_file_next']
|
||||
if os.path.isfile(path):
|
||||
item = {'type': 'file',
|
||||
'path' : request.form['add_file_next'],
|
||||
@ -186,7 +186,7 @@ def post():
|
||||
|
||||
print('folder:', folder)
|
||||
|
||||
if os.path.isdir(var.config.get('bot', 'music_folder') + folder):
|
||||
if os.path.isdir(var.music_folder + folder):
|
||||
|
||||
files = util.get_recursive_filelist_sorted(var.music_folder)
|
||||
music_library = util.Dir(folder_path)
|
||||
@ -261,13 +261,13 @@ def post():
|
||||
var.botamusique.launch_music(int(request.form['play_music']))
|
||||
|
||||
elif 'delete_music_file' in request.form and ".." not in request.form['delete_music_file']:
|
||||
path = var.config.get('bot', 'music_folder') + request.form['delete_music_file']
|
||||
path = var.music_folder + request.form['delete_music_file']
|
||||
if os.path.isfile(path):
|
||||
logging.info("web: delete file " + path)
|
||||
os.remove(path)
|
||||
|
||||
elif 'delete_folder' in request.form and ".." not in request.form['delete_folder']:
|
||||
path = var.config.get('bot', 'music_folder') + request.form['delete_folder']
|
||||
path = var.music_folder + request.form['delete_folder']
|
||||
if os.path.isdir(path):
|
||||
logging.info("web: delete folder " + path)
|
||||
shutil.rmtree(path)
|
||||
|
28
mumbleBot.py
28
mumbleBot.py
@ -91,7 +91,8 @@ class MumbleBot:
|
||||
logging.error("Starting in ERROR loglevel")
|
||||
|
||||
var.user = args.user
|
||||
var.music_folder = var.config.get('bot', 'music_folder')
|
||||
var.music_folder = util.solve_filepath(var.config.get('bot', 'music_folder'))
|
||||
var.tmp_folder = util.solve_filepath(var.config.get('bot', 'tmp_folder'))
|
||||
var.is_proxified = var.config.getboolean(
|
||||
"webinterface", "is_web_proxified")
|
||||
self.exit = False
|
||||
@ -139,7 +140,7 @@ class MumbleBot:
|
||||
if args.certificate:
|
||||
certificate = args.certificate
|
||||
else:
|
||||
certificate = var.config.get("server", "certificate")
|
||||
certificate = util.solve_filepath(var.config.get("server", "certificate"))
|
||||
|
||||
if args.tokens:
|
||||
tokens = args.tokens
|
||||
@ -329,8 +330,7 @@ class MumbleBot:
|
||||
logging.info("bot: play music " + util.format_debug_song_string(music))
|
||||
if music["type"] == "url":
|
||||
# Delete older music is the tmp folder is too big
|
||||
media.system.clear_tmp_folder(var.config.get(
|
||||
'bot', 'tmp_folder'), var.config.getint('bot', 'tmp_folder_max_size'))
|
||||
media.system.clear_tmp_folder(var.tmp_folder, var.config.getint('bot', 'tmp_folder_max_size'))
|
||||
|
||||
# Check if the music is ready to be played
|
||||
if music["ready"] == "downloading":
|
||||
@ -348,8 +348,7 @@ class MumbleBot:
|
||||
elif music["type"] == "file":
|
||||
if not self.check_item_path_or_remove():
|
||||
return
|
||||
uri = var.config.get('bot', 'music_folder') + \
|
||||
var.playlist.current_item()["path"]
|
||||
uri = var.music_folder + var.playlist.current_item()["path"]
|
||||
|
||||
elif music["type"] == "radio":
|
||||
uri = music["url"]
|
||||
@ -386,7 +385,7 @@ class MumbleBot:
|
||||
|
||||
url_hash = hashlib.md5(url.encode()).hexdigest()
|
||||
|
||||
path = var.config.get('bot', 'tmp_folder') + url_hash + ".%(ext)s"
|
||||
path = var.tmp_folder + url_hash + ".%(ext)s"
|
||||
mp3 = path.replace(".%(ext)s", ".mp3")
|
||||
music['path'] = mp3
|
||||
|
||||
@ -429,7 +428,7 @@ class MumbleBot:
|
||||
|
||||
url_hash = hashlib.md5(url.encode()).hexdigest()
|
||||
|
||||
path = var.config.get('bot', 'tmp_folder') + url_hash + ".%(ext)s"
|
||||
path = var.tmp_folder + url_hash + ".%(ext)s"
|
||||
mp3 = path.replace(".%(ext)s", ".mp3")
|
||||
music['path'] = mp3
|
||||
|
||||
@ -519,7 +518,7 @@ class MumbleBot:
|
||||
return False
|
||||
|
||||
elif music["type"] == "file":
|
||||
uri = var.config.get('bot', 'music_folder') + music["path"]
|
||||
uri = var.music_folder + music["path"]
|
||||
if not os.path.exists(uri):
|
||||
logging.info("bot: music file missed. removing music from the playlist: %s" % util.format_debug_song_string(music))
|
||||
self.send_msg(constants.strings('file_missed', file=music["path"]))
|
||||
@ -664,8 +663,7 @@ class MumbleBot:
|
||||
uri = music['path']
|
||||
|
||||
elif music["type"] == "file":
|
||||
uri = var.config.get('bot', 'music_folder') + \
|
||||
var.playlist.current_item()["path"]
|
||||
uri = var.music_folder + var.playlist.current_item()["path"]
|
||||
|
||||
command = ("ffmpeg", '-v', ffmpeg_debug, '-nostdin', '-ss', "%f" % self.playhead, '-i',
|
||||
uri, '-ac', '1', '-f', 's16le', '-ar', '48000', '-')
|
||||
@ -733,8 +731,10 @@ if __name__ == '__main__':
|
||||
args = parser.parse_args()
|
||||
|
||||
config = configparser.ConfigParser(interpolation=None, allow_no_value=True)
|
||||
parsed_configs = config.read(['configuration.default.ini', args.config], encoding='utf-8')
|
||||
var.dbfile = args.db if args.db is not None else config.get("bot", "database_path", fallback="database.db")
|
||||
parsed_configs = config.read([util.solve_filepath('configuration.default.ini'), util.solve_filepath(args.config)],
|
||||
encoding='utf-8')
|
||||
var.dbfile = args.db if args.db is not None else config.get("bot", "database_path",
|
||||
fallback=util.solve_filepath("database.db"))
|
||||
|
||||
if len(parsed_configs) == 0:
|
||||
logging.error('Could not read configuration from file \"{}\"'.format(args.config))
|
||||
@ -748,7 +748,7 @@ if __name__ == '__main__':
|
||||
formatter = logging.Formatter('[%(asctime)s %(levelname)s %(threadName)s] %(message)s', "%b %d %H:%M:%S")
|
||||
root.setLevel(logging.INFO)
|
||||
|
||||
logfile = var.config.get('bot', 'logfile')
|
||||
logfile = util.solve_filepath(var.config.get('bot', 'logfile'))
|
||||
|
||||
handler = None
|
||||
if logfile:
|
||||
|
14
util.py
14
util.py
@ -22,6 +22,16 @@ import base64
|
||||
import media
|
||||
import media.radio
|
||||
|
||||
def solve_filepath(path):
|
||||
if not path:
|
||||
return ''
|
||||
|
||||
if path[0] == '/':
|
||||
return path
|
||||
else:
|
||||
mydir = os.path.dirname(os.path.realpath(__file__))
|
||||
return mydir + '/' + path
|
||||
|
||||
def get_recursive_filelist_sorted(path):
|
||||
filelist = []
|
||||
for root, dirs, files in os.walk(path):
|
||||
@ -50,7 +60,7 @@ def get_music_path(music):
|
||||
if music["type"] == "url":
|
||||
uri = music['path']
|
||||
elif music["type"] == "file":
|
||||
uri = var.config.get('bot', 'music_folder') + music["path"]
|
||||
uri = var.music_folder + music["path"]
|
||||
elif music["type"] == "radio":
|
||||
uri = music['url']
|
||||
|
||||
@ -213,7 +223,7 @@ def format_current_playing():
|
||||
# - hash is a sha1 of the string representation of the directories' contents (which are
|
||||
# zipped)
|
||||
def zipdir(zippath, zipname_prefix=None):
|
||||
zipname = var.config.get('bot', 'tmp_folder')
|
||||
zipname = var.tmp_folder
|
||||
if zipname_prefix and '../' not in zipname_prefix:
|
||||
zipname += zipname_prefix.strip().replace('/', '_') + '_'
|
||||
|
||||
|
@ -1,9 +1,12 @@
|
||||
current_music = None
|
||||
botamusique = None
|
||||
playlist = None
|
||||
|
||||
user = ""
|
||||
music_folder = ""
|
||||
is_proxified = False
|
||||
|
||||
dbfile = None
|
||||
db = None
|
||||
config = None
|
||||
botamusique = None
|
||||
|
||||
music_folder = ""
|
||||
tmp_folder = ""
|
||||
|
Loading…
x
Reference in New Issue
Block a user