2021-01-15 08:57:37 -07:00
|
|
|
# Copyright 2018-2021 The Matrix.org Foundation C.I.C.
|
2018-01-08 10:19:55 -07:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2021-01-15 08:57:37 -07:00
|
|
|
import abc
|
2018-07-09 00:09:20 -06:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import shutil
|
2022-03-08 08:58:14 -07:00
|
|
|
from typing import TYPE_CHECKING, Callable, Optional
|
2018-01-08 10:19:55 -07:00
|
|
|
|
2018-01-16 08:44:08 -07:00
|
|
|
from synapse.config._base import Config
|
2019-07-03 08:07:04 -06:00
|
|
|
from synapse.logging.context import defer_to_thread, run_in_background
|
2020-12-11 12:05:15 -07:00
|
|
|
from synapse.util.async_helpers import maybe_awaitable
|
2018-01-08 10:19:55 -07:00
|
|
|
|
2020-07-27 12:40:11 -06:00
|
|
|
from ._base import FileInfo, Responder
|
2018-07-09 00:09:20 -06:00
|
|
|
from .media_storage import FileResponder
|
2018-01-08 10:19:55 -07:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-01-15 08:57:37 -07:00
|
|
|
if TYPE_CHECKING:
|
2021-03-23 05:12:48 -06:00
|
|
|
from synapse.server import HomeServer
|
2018-01-08 10:19:55 -07:00
|
|
|
|
2021-01-15 08:57:37 -07:00
|
|
|
|
|
|
|
class StorageProvider(metaclass=abc.ABCMeta):
|
2018-01-08 10:19:55 -07:00
|
|
|
"""A storage provider is a service that can store uploaded media and
|
|
|
|
retrieve them.
|
|
|
|
"""
|
2019-06-20 03:32:02 -06:00
|
|
|
|
2021-01-15 08:57:37 -07:00
|
|
|
@abc.abstractmethod
|
|
|
|
async def store_file(self, path: str, file_info: FileInfo) -> None:
|
2018-01-08 10:19:55 -07:00
|
|
|
"""Store the file described by file_info. The actual contents can be
|
|
|
|
retrieved by reading the file in file_info.upload_path.
|
|
|
|
|
|
|
|
Args:
|
2020-07-27 12:40:11 -06:00
|
|
|
path: Relative path of file in local cache
|
|
|
|
file_info: The metadata of the file.
|
2018-01-08 10:19:55 -07:00
|
|
|
"""
|
|
|
|
|
2021-01-15 08:57:37 -07:00
|
|
|
@abc.abstractmethod
|
2020-07-27 12:40:11 -06:00
|
|
|
async def fetch(self, path: str, file_info: FileInfo) -> Optional[Responder]:
|
2018-01-08 10:19:55 -07:00
|
|
|
"""Attempt to fetch the file described by file_info and stream it
|
|
|
|
into writer.
|
|
|
|
|
|
|
|
Args:
|
2020-07-27 12:40:11 -06:00
|
|
|
path: Relative path of file in local cache
|
|
|
|
file_info: The metadata of the file.
|
2018-01-08 10:19:55 -07:00
|
|
|
|
|
|
|
Returns:
|
2020-07-27 12:40:11 -06:00
|
|
|
Returns a Responder if the provider has the file, otherwise returns None.
|
2018-01-08 10:19:55 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class StorageProviderWrapper(StorageProvider):
|
|
|
|
"""Wraps a storage provider and provides various config options
|
|
|
|
|
|
|
|
Args:
|
2020-07-27 12:40:11 -06:00
|
|
|
backend: The storage provider to wrap.
|
|
|
|
store_local: Whether to store new local files or not.
|
|
|
|
store_synchronous: Whether to wait for file to be successfully
|
2019-07-12 08:29:32 -06:00
|
|
|
uploaded, or todo the upload in the background.
|
2020-07-27 12:40:11 -06:00
|
|
|
store_remote: Whether remote media should be uploaded
|
2018-01-08 10:19:55 -07:00
|
|
|
"""
|
2019-06-20 03:32:02 -06:00
|
|
|
|
2020-07-27 12:40:11 -06:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
backend: StorageProvider,
|
|
|
|
store_local: bool,
|
|
|
|
store_synchronous: bool,
|
|
|
|
store_remote: bool,
|
|
|
|
):
|
2018-01-08 10:19:55 -07:00
|
|
|
self.backend = backend
|
2018-01-16 08:44:08 -07:00
|
|
|
self.store_local = store_local
|
2018-01-08 10:19:55 -07:00
|
|
|
self.store_synchronous = store_synchronous
|
|
|
|
self.store_remote = store_remote
|
|
|
|
|
2021-01-15 08:57:37 -07:00
|
|
|
def __str__(self) -> str:
|
2020-01-23 05:11:44 -07:00
|
|
|
return "StorageProviderWrapper[%s]" % (self.backend,)
|
|
|
|
|
2021-01-15 08:57:37 -07:00
|
|
|
async def store_file(self, path: str, file_info: FileInfo) -> None:
|
2018-01-18 11:37:59 -07:00
|
|
|
if not file_info.server_name and not self.store_local:
|
2020-07-27 12:40:11 -06:00
|
|
|
return None
|
2018-01-08 10:19:55 -07:00
|
|
|
|
|
|
|
if file_info.server_name and not self.store_remote:
|
2020-07-27 12:40:11 -06:00
|
|
|
return None
|
2018-01-08 10:19:55 -07:00
|
|
|
|
2021-09-27 05:55:27 -06:00
|
|
|
if file_info.url_cache:
|
|
|
|
# The URL preview cache is short lived and not worth offloading or
|
|
|
|
# backing up.
|
|
|
|
return None
|
|
|
|
|
2018-01-08 10:19:55 -07:00
|
|
|
if self.store_synchronous:
|
2020-08-04 07:44:25 -06:00
|
|
|
# store_file is supposed to return an Awaitable, but guard
|
|
|
|
# against improper implementations.
|
2021-01-15 08:57:37 -07:00
|
|
|
await maybe_awaitable(self.backend.store_file(path, file_info)) # type: ignore
|
2018-01-08 10:19:55 -07:00
|
|
|
else:
|
|
|
|
# TODO: Handle errors.
|
2021-09-15 06:45:32 -06:00
|
|
|
async def store() -> None:
|
2018-04-27 04:07:40 -06:00
|
|
|
try:
|
2020-12-11 12:05:15 -07:00
|
|
|
return await maybe_awaitable(
|
|
|
|
self.backend.store_file(path, file_info)
|
|
|
|
)
|
2018-04-27 04:07:40 -06:00
|
|
|
except Exception:
|
|
|
|
logger.exception("Error storing file")
|
2019-06-20 03:32:02 -06:00
|
|
|
|
2018-04-27 04:07:40 -06:00
|
|
|
run_in_background(store)
|
2018-01-08 10:19:55 -07:00
|
|
|
|
2021-01-15 08:57:37 -07:00
|
|
|
async def fetch(self, path: str, file_info: FileInfo) -> Optional[Responder]:
|
2021-09-27 05:55:27 -06:00
|
|
|
if file_info.url_cache:
|
|
|
|
# Files in the URL preview cache definitely aren't stored here,
|
|
|
|
# so avoid any potentially slow I/O or network access.
|
|
|
|
return None
|
|
|
|
|
2020-08-04 07:44:25 -06:00
|
|
|
# store_file is supposed to return an Awaitable, but guard
|
|
|
|
# against improper implementations.
|
2020-12-11 12:05:15 -07:00
|
|
|
return await maybe_awaitable(self.backend.fetch(path, file_info))
|
2018-01-08 10:19:55 -07:00
|
|
|
|
|
|
|
|
|
|
|
class FileStorageProviderBackend(StorageProvider):
|
|
|
|
"""A storage provider that stores files in a directory on a filesystem.
|
|
|
|
|
|
|
|
Args:
|
2021-01-15 08:57:37 -07:00
|
|
|
hs
|
2018-01-18 10:11:20 -07:00
|
|
|
config: The config returned by `parse_config`.
|
2018-01-08 10:19:55 -07:00
|
|
|
"""
|
|
|
|
|
2021-01-15 08:57:37 -07:00
|
|
|
def __init__(self, hs: "HomeServer", config: str):
|
2019-05-13 14:01:14 -06:00
|
|
|
self.hs = hs
|
2021-09-24 05:25:21 -06:00
|
|
|
self.cache_directory = hs.config.media.media_store_path
|
2018-01-16 08:44:08 -07:00
|
|
|
self.base_directory = config
|
2018-01-08 10:19:55 -07:00
|
|
|
|
2021-09-15 06:45:32 -06:00
|
|
|
def __str__(self) -> str:
|
2020-01-23 05:11:44 -07:00
|
|
|
return "FileStorageProviderBackend[%s]" % (self.base_directory,)
|
|
|
|
|
2021-01-15 08:57:37 -07:00
|
|
|
async def store_file(self, path: str, file_info: FileInfo) -> None:
|
2018-01-08 10:19:55 -07:00
|
|
|
"""See StorageProvider.store_file"""
|
|
|
|
|
|
|
|
primary_fname = os.path.join(self.cache_directory, path)
|
|
|
|
backup_fname = os.path.join(self.base_directory, path)
|
|
|
|
|
|
|
|
dirname = os.path.dirname(backup_fname)
|
2021-09-27 04:29:23 -06:00
|
|
|
os.makedirs(dirname, exist_ok=True)
|
2018-01-08 10:19:55 -07:00
|
|
|
|
2022-03-08 08:58:14 -07:00
|
|
|
# mypy needs help inferring the type of the second parameter, which is generic
|
|
|
|
shutil_copyfile: Callable[[str, str], str] = shutil.copyfile
|
2021-01-15 08:57:37 -07:00
|
|
|
await defer_to_thread(
|
2022-03-08 08:58:14 -07:00
|
|
|
self.hs.get_reactor(),
|
|
|
|
shutil_copyfile,
|
|
|
|
primary_fname,
|
|
|
|
backup_fname,
|
2018-01-08 10:19:55 -07:00
|
|
|
)
|
|
|
|
|
2021-01-15 08:57:37 -07:00
|
|
|
async def fetch(self, path: str, file_info: FileInfo) -> Optional[Responder]:
|
2018-01-08 10:19:55 -07:00
|
|
|
"""See StorageProvider.fetch"""
|
|
|
|
|
|
|
|
backup_fname = os.path.join(self.base_directory, path)
|
|
|
|
if os.path.isfile(backup_fname):
|
|
|
|
return FileResponder(open(backup_fname, "rb"))
|
2018-01-16 08:44:08 -07:00
|
|
|
|
2021-01-15 08:57:37 -07:00
|
|
|
return None
|
|
|
|
|
2018-01-18 10:11:45 -07:00
|
|
|
@staticmethod
|
2021-01-15 08:57:37 -07:00
|
|
|
def parse_config(config: dict) -> str:
|
2018-01-16 08:44:08 -07:00
|
|
|
"""Called on startup to parse config supplied. This should parse
|
|
|
|
the config and raise if there is a problem.
|
|
|
|
|
|
|
|
The returned value is passed into the constructor.
|
|
|
|
|
2018-01-18 10:11:20 -07:00
|
|
|
In this case we only care about a single param, the directory, so let's
|
2018-01-16 08:44:08 -07:00
|
|
|
just pull that out.
|
|
|
|
"""
|
|
|
|
return Config.ensure_directory(config["directory"])
|