fix: Error when calculating tmp folder size.

Instructed the bot to directly ignore errors.

Fixed #224.
This commit is contained in:
Terry Geng 2021-02-28 08:59:48 +08:00
parent 999252c98b
commit 1637501601
No known key found for this signature in database
GPG Key ID: F982F8EA1DF720E7
3 changed files with 51 additions and 45 deletions

View File

@ -1,44 +0,0 @@
import logging
import os
log = logging.getLogger("bot")
def get_size_folder(path):
global log
folder_size = 0
for (path, dirs, files) in os.walk(path):
for file in files:
filename = os.path.join(path, file)
folder_size += os.path.getsize(filename)
return int(folder_size / (1024 * 1024))
def clear_tmp_folder(path, size):
global log
if size == -1:
return
elif size == 0:
for (path, dirs, files) in os.walk(path):
for file in files:
filename = os.path.join(path, file)
os.remove(filename)
else:
if get_size_folder(path=path) > size:
all_files = ""
for (path, dirs, files) in os.walk(path):
all_files = [os.path.join(path, file) for file in files]
all_files.sort(key=lambda x: os.path.getmtime(x))
size_tp = 0
for idx, file in enumerate(all_files):
size_tp += os.path.getsize(file)
if int(size_tp / (1024 * 1024)) > size:
log.info("Cleaning tmp folder")
to_remove = all_files[:idx]
print(to_remove)
for f in to_remove:
log.debug("Removing " + f)
os.remove(os.path.join(path, f))
return

View File

@ -9,6 +9,7 @@ import glob
from io import BytesIO
import base64
import util
from constants import tr_cli as tr
import media
import variables as var
@ -151,7 +152,7 @@ class URLItem(BaseItem):
raise ValidationFailedError(tr('unable_download', item=self.format_title()))
def _download(self):
media.system.clear_tmp_folder(var.tmp_folder, var.config.getint('bot', 'tmp_folder_max_size'))
util.clear_tmp_folder(var.tmp_folder, var.config.getint('bot', 'tmp_folder_max_size'))
self.downloading = True
base_path = var.tmp_folder + self.id

49
util.py
View File

@ -508,3 +508,52 @@ class VolumeHelper:
# Some dirty trick to stretch the function, to make to be 0 when input is -35 dB
return (10 ** (dB / 20) - 10 ** (-35 / 20)) / (1 - 10 ** (-35 / 20))
def get_size_folder(path):
global log
folder_size = 0
for (path, dirs, files) in os.walk(path):
for file in files:
filename = os.path.join(path, file)
try:
folder_size += os.path.getsize(filename)
except (FileNotFoundError, OSError):
continue
return int(folder_size / (1024 * 1024))
def clear_tmp_folder(path, size):
global log
if size == -1:
return
elif size == 0:
for (path, dirs, files) in os.walk(path):
for file in files:
filename = os.path.join(path, file)
try:
os.remove(filename)
except (FileNotFoundError, OSError):
continue
else:
if get_size_folder(path=path) > size:
all_files = ""
for (path, dirs, files) in os.walk(path):
all_files = [os.path.join(path, file) for file in files]
all_files.sort(key=lambda x: os.path.getmtime(x))
size_tp = 0
for idx, file in enumerate(all_files):
size_tp += os.path.getsize(file)
if int(size_tp / (1024 * 1024)) > size:
log.info("Cleaning tmp folder")
to_remove = all_files[:idx]
print(to_remove)
for f in to_remove:
log.debug("Removing " + f)
try:
os.remove(os.path.join(path, f))
except (FileNotFoundError, OSError):
continue
return