handle filenotfounderror for manual cookie import

This commit is contained in:
simon 2022-07-21 23:01:32 +07:00
parent eb229440d4
commit 4b1ebf44c9
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
2 changed files with 29 additions and 13 deletions

View File

@ -83,8 +83,13 @@ class CookieHandler:
"""import cookie from file""" """import cookie from file"""
cache_path = self.config["application"]["cache_dir"] cache_path = self.config["application"]["cache_dir"]
import_path = os.path.join(cache_path, "import", "cookies.google.txt") import_path = os.path.join(cache_path, "import", "cookies.google.txt")
try:
with open(import_path, encoding="utf-8") as cookie_file: with open(import_path, encoding="utf-8") as cookie_file:
cookie = cookie_file.read() cookie = cookie_file.read()
except FileNotFoundError as err:
print(f"cookie: {import_path} file not found")
raise err
self.set_cookie(cookie) self.set_cookie(cookie)

View File

@ -929,25 +929,36 @@ class SettingsView(View):
if config_value == "cookie_import": if config_value == "cookie_import":
self.process_cookie(config, updated_value) self.process_cookie(config, updated_value)
@staticmethod def process_cookie(self, config, updated_value):
def process_cookie(config, updated_value):
"""import and validate cookie""" """import and validate cookie"""
handler = CookieHandler(config) handler = CookieHandler(config)
if updated_value: if updated_value:
try:
handler.import_cookie() handler.import_cookie()
except FileNotFoundError:
print("cookie: import failed, file not found")
handler.revoke()
self._fail_message("Cookie file not found.")
return
valid = handler.validate() valid = handler.validate()
if not valid: if not valid:
handler.revoke() handler.revoke()
self._fail_message("Failed to validate cookie file.")
else:
handler.revoke()
@staticmethod
def _fail_message(message_line):
"""notify our failure"""
key = "message:setting" key = "message:setting"
message = { message = {
"status": key, "status": key,
"level": "error", "level": "error",
"title": "Cookie import failed", "title": "Cookie import failed",
"message": "", "message": message_line,
} }
RedisArchivist().set_message(key, message=message, expire=True) RedisArchivist().set_message(key, message=message, expire=True)
else:
handler.revoke()
def progress(request): def progress(request):