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"""
cache_path = self.config["application"]["cache_dir"]
import_path = os.path.join(cache_path, "import", "cookies.google.txt")
with open(import_path, encoding="utf-8") as cookie_file:
cookie = cookie_file.read()
try:
with open(import_path, encoding="utf-8") as cookie_file:
cookie = cookie_file.read()
except FileNotFoundError as err:
print(f"cookie: {import_path} file not found")
raise err
self.set_cookie(cookie)

View File

@ -929,26 +929,37 @@ class SettingsView(View):
if config_value == "cookie_import":
self.process_cookie(config, updated_value)
@staticmethod
def process_cookie(config, updated_value):
def process_cookie(self, config, updated_value):
"""import and validate cookie"""
handler = CookieHandler(config)
if updated_value:
handler.import_cookie()
try:
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()
if not valid:
handler.revoke()
key = "message:setting"
message = {
"status": key,
"level": "error",
"title": "Cookie import failed",
"message": "",
}
RedisArchivist().set_message(key, message=message, expire=True)
self._fail_message("Failed to validate cookie file.")
else:
handler.revoke()
@staticmethod
def _fail_message(message_line):
"""notify our failure"""
key = "message:setting"
message = {
"status": key,
"level": "error",
"title": "Cookie import failed",
"message": message_line,
}
RedisArchivist().set_message(key, message=message, expire=True)
def progress(request):
# pylint: disable=unused-argument