2021-03-20 10:32:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2020 mmonkey
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2021-09-22 04:39:00 +00:00
|
|
|
from . import logger
|
2021-03-20 10:32:50 +00:00
|
|
|
from .constants import CACHE_DIR
|
2021-09-25 08:04:38 +00:00
|
|
|
from os import makedirs, remove
|
2021-03-20 10:32:50 +00:00
|
|
|
from os.path import isdir, isfile, join
|
|
|
|
from shutil import rmtree
|
|
|
|
|
|
|
|
|
|
|
|
class FileSystem:
|
|
|
|
_instance = None
|
|
|
|
_cache_dir = CACHE_DIR
|
|
|
|
|
|
|
|
def __new__(cls):
|
|
|
|
if cls._instance is None:
|
|
|
|
cls._instance = super(FileSystem, cls).__new__(cls)
|
2021-09-22 04:39:00 +00:00
|
|
|
cls.log = logger.create()
|
2021-03-20 10:32:50 +00:00
|
|
|
return cls._instance
|
|
|
|
|
|
|
|
def get_cache_dir(self, cache_type=None):
|
|
|
|
if not isdir(self._cache_dir):
|
2021-09-22 04:39:00 +00:00
|
|
|
try:
|
|
|
|
makedirs(self._cache_dir)
|
|
|
|
except OSError:
|
|
|
|
self.log.info(f'Failed to create path {self._cache_dir} (Permission denied).')
|
2022-04-30 06:26:00 +00:00
|
|
|
raise
|
2021-03-20 10:32:50 +00:00
|
|
|
|
2021-09-22 04:39:00 +00:00
|
|
|
path = join(self._cache_dir, cache_type)
|
|
|
|
if cache_type and not isdir(path):
|
|
|
|
try:
|
|
|
|
makedirs(path)
|
|
|
|
except OSError:
|
|
|
|
self.log.info(f'Failed to create path {path} (Permission denied).')
|
2022-04-30 06:26:00 +00:00
|
|
|
raise
|
2021-03-20 10:32:50 +00:00
|
|
|
|
2021-09-22 04:39:00 +00:00
|
|
|
return path if cache_type else self._cache_dir
|
2021-03-20 10:32:50 +00:00
|
|
|
|
2021-09-25 08:04:38 +00:00
|
|
|
def get_cache_file_dir(self, filename, cache_type=None):
|
|
|
|
path = join(self.get_cache_dir(cache_type), filename[:2])
|
|
|
|
if not isdir(path):
|
|
|
|
try:
|
|
|
|
makedirs(path)
|
|
|
|
except OSError:
|
|
|
|
self.log.info(f'Failed to create path {path} (Permission denied).')
|
2022-04-30 06:26:00 +00:00
|
|
|
raise
|
2021-09-25 08:04:38 +00:00
|
|
|
|
|
|
|
return path
|
|
|
|
|
2021-03-20 10:32:50 +00:00
|
|
|
def get_cache_file_path(self, filename, cache_type=None):
|
2021-09-25 08:04:38 +00:00
|
|
|
return join(self.get_cache_file_dir(filename, cache_type), filename) if filename else None
|
2021-03-20 10:32:50 +00:00
|
|
|
|
2021-09-24 08:11:14 +00:00
|
|
|
def get_cache_file_exists(self, filename, cache_type=None):
|
|
|
|
path = self.get_cache_file_path(filename, cache_type)
|
|
|
|
return isfile(path)
|
2021-09-20 03:45:19 +00:00
|
|
|
|
2021-03-20 10:32:50 +00:00
|
|
|
def delete_cache_dir(self, cache_type=None):
|
|
|
|
if not cache_type and isdir(self._cache_dir):
|
2021-09-22 04:39:00 +00:00
|
|
|
try:
|
|
|
|
rmtree(self._cache_dir)
|
|
|
|
except OSError:
|
|
|
|
self.log.info(f'Failed to delete path {self._cache_dir} (Permission denied).')
|
2022-04-30 06:26:00 +00:00
|
|
|
raise
|
2021-09-22 04:39:00 +00:00
|
|
|
|
|
|
|
path = join(self._cache_dir, cache_type)
|
|
|
|
if cache_type and isdir(path):
|
|
|
|
try:
|
|
|
|
rmtree(path)
|
|
|
|
except OSError:
|
|
|
|
self.log.info(f'Failed to delete path {path} (Permission denied).')
|
2022-04-30 06:26:00 +00:00
|
|
|
raise
|
2021-03-20 10:32:50 +00:00
|
|
|
|
|
|
|
def delete_cache_file(self, filename, cache_type=None):
|
2021-09-25 08:04:38 +00:00
|
|
|
path = self.get_cache_file_path(filename, cache_type)
|
2021-09-22 04:39:00 +00:00
|
|
|
if isfile(path):
|
|
|
|
try:
|
|
|
|
remove(path)
|
|
|
|
except OSError:
|
|
|
|
self.log.info(f'Failed to delete path {path} (Permission denied).')
|
2022-04-30 06:26:00 +00:00
|
|
|
raise
|