1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import io
import os
import shutil
import urllib.parse as urlparse
from mediagoblin.storage import (
StorageInterface,
clean_listy_filepath,
NoWebServing)
class FileObjectAwareFile(io.FileIO):
def write(self, data):
if hasattr(data, 'read'):
# We can call data.read(). It means that the data is a file-like
# object, which should be saved RAM-friendly way
shutil.copyfileobj(data, self)
else:
super().write(data)
class BasicFileStorage(StorageInterface):
"""
Basic local filesystem implementation of storage API
"""
local_storage = True
def __init__(self, base_dir, base_url=None, **kwargs):
"""
Keyword arguments:
- base_dir: Base directory things will be served out of. MUST
be an absolute path.
- base_url: URL files will be served from
"""
self.base_dir = base_dir
self.base_url = base_url
def _resolve_filepath(self, filepath):
"""
Transform the given filepath into a local filesystem filepath.
"""
return os.path.join(
self.base_dir, *clean_listy_filepath(filepath))
def file_exists(self, filepath):
return os.path.exists(self._resolve_filepath(filepath))
def get_file(self, filepath, mode='r'):
# Make directories if necessary
if len(filepath) > 1:
directory = self._resolve_filepath(filepath[:-1])
if not os.path.exists(directory):
os.makedirs(directory)
# Grab and return the file in the mode specified
return FileObjectAwareFile(self._resolve_filepath(filepath), mode)
def delete_file(self, filepath):
"""Delete file at filepath
Raises OSError in case filepath is a directory."""
#TODO: log error
os.remove(self._resolve_filepath(filepath))
def delete_dir(self, dirpath, recursive=False):
"""returns True on succes, False on failure"""
dirpath = self._resolve_filepath(dirpath)
# Shortcut the default and simple case of nonempty=F, recursive=F
if recursive:
try:
shutil.rmtree(dirpath)
except OSError as e:
#TODO: log something here
return False
else: # recursively delete everything
try:
os.rmdir(dirpath)
except OSError as e:
#TODO: log something here
return False
return True
def file_url(self, filepath):
if not self.base_url:
raise NoWebServing(
"base_url not set, cannot provide file urls")
return urlparse.urljoin(
self.base_url,
'/'.join(clean_listy_filepath(filepath)))
def get_local_path(self, filepath):
return self._resolve_filepath(filepath)
def copy_local_to_storage(self, filename, filepath):
"""
Copy this file from locally to the storage system.
"""
# Make directories if necessary
if len(filepath) > 1:
directory = self._resolve_filepath(filepath[:-1])
if not os.path.exists(directory):
os.makedirs(directory)
# This uses chunked copying of 16kb buffers (Py2.7):
shutil.copy(filename, self.get_local_path(filepath))
def get_file_size(self, filepath):
return os.stat(self._resolve_filepath(filepath)).st_size
|