-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstorage.py
167 lines (137 loc) · 5.72 KB
/
storage.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -*- coding: utf-8 -*-
#
# GMG localfiles plugin -- local file import
# Copyright (C) 2012 Odin Hørthe Omdal
#
# 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 logging
import os
import re
import urlparse
from mediagoblin.storage import NoWebServing
from mediagoblin.storage.filestorage import BasicFileStorage
CACHE_DIR = 'mg_cache'
_log = logging.getLogger(__name__)
def _is_cachefile(filepath):
if filepath and filepath[0] == CACHE_DIR:
return True
fn = filepath[-1].lower()
return any(k in fn for k in ['.thumbnail.', '.medium.',
'.nef.jpg', '.cr2.jpg'])
def _ensure_in_cache_dir(filepath):
if filepath and filepath[0] == CACHE_DIR:
return filepath
return [CACHE_DIR] + list(filepath)
class PersistentFileStorage(BasicFileStorage):
"""
Local filesystem implementation of storage API that doesn't delete files
"""
def _cachefile_to_original_filepath(self, filepath):
"""
Custom function taking a cache file and returns original filepath.
"""
if CACHE_DIR == filepath[0]:
filepath = filepath[1:]
filename = self._re_raw_from_preview.sub(r'\1.\2', filepath[-1])
return list(filepath[:-1]) + [filename]
_re_raw_from_preview = re.compile(r'^(.*)\.(nef|cr2)(\.jpg)$', re.I)
def _resolve_filepath(self, filepath):
"""
Transform the given filepath into a local filesystem path.
Differences from filestorage:
- If filename looks like a cache file, it will ensure
that the path returned is in cache directory.
- Check if the file exists, if it doesn't it will
try using an uppercase extension.
"""
if _is_cachefile(filepath):
filepath = _ensure_in_cache_dir(filepath)
path = os.path.join(self.base_dir, *filepath)
return path
# Sadly, since MediaGoblin always expect the file extension
# to be lower case (it renames MyPic.JPG to MyPic.jpg),
# we cannot be sure about what path we should return.
#
# So we have to check if the file exists on disk,
# if it does not we should use the upper case
# version of the name (if it's neither, you are
# on your own).
path = os.path.join(self.base_dir, *filepath)
if os.path.exists(path):
return path
# The expected file didn't exist. GMG probably gave
# us ".jpg", so let's return ".JPG"
fn, ext = os.path.splitext(filepath[-1])
filepath = list(filepath[:-1]) + [fn + ext.upper()]
path = os.path.join(self.base_dir, *filepath)
return path
def file_url(self, filepath):
"""
Takes filepath returns URL
Differences from filestorage are two:
- If the filepath looks like a cachefile, it'll use cache dir
- It won't "clean" the filename of non-ascii letters
"""
if not self.base_url:
raise NoWebServing(
"base_url not set, cannot provide file urls")
if _is_cachefile(filepath):
filepath = _ensure_in_cache_dir(filepath)
return urlparse.urljoin(self.base_url, '/'.join(filepath))
def get_file(self, filepath, mode='r'):
if _is_cachefile(filepath):
return super(PersistentFileStorage, self).get_file(filepath, mode)
if not os.path.exists(self._resolve_filepath(filepath)):
return PersistentStorageObjectWrapper(
None, self._resolve_filepath(filepath))
mode = mode.replace("w", "r")
# Grab and return the file in the mode specified
return PersistentStorageObjectWrapper(
open(self._resolve_filepath(filepath), mode))
def delete_file(self, filepath):
_log.info(u'Not removing {0} as requested.'.format(
self._resolve_filepath(filepath)))
def delete_dir(self, dirpath, recursive=False):
return False
def copy_local_to_storage(self, filename, filepath):
"""
Copy this file from locally to the storage system.
"""
# Everything that mediagoblin possibly wants to create
# should go in the cache dir.
if _is_cachefile(filepath):
filepath = _ensure_in_cache_dir(filepath)
super(type(self), self).copy_local_to_storage(filename, filepath)
else:
_log.debug('Refusing to copy non-cache file path {}.'
''.format(filepath))
class PersistentStorageObjectWrapper():
def __init__(self, storage_object, name=None, *args, **kwargs):
self.storage_object = storage_object
self.name = name
if storage_object:
self.name = storage_object.name
def read(self, *args, **kwargs):
_log.debug(u'Reading {0}'.format(
self.name).encode("utf-8"))
return self.storage_object.read(*args, **kwargs)
def write(self, data, *args, **kwargs):
_log.debug(u'Not writing {0}'.format(
self.name).encode("utf-8"))
def close(self):
pass
def __enter__(self):
return self
def __exit__(self, *exc_info):
self.close()