1
|
|
|
""" |
2
|
|
|
This module implement a filesystem storage adapter. |
3
|
|
|
""" |
4
|
|
|
import errno |
5
|
|
|
import logging |
6
|
|
|
import os |
7
|
|
|
from flask import current_app |
8
|
|
|
from .interface import ImagineAdapterInterface |
9
|
|
|
from PIL import Image |
10
|
|
|
|
11
|
|
|
LOGGER = logging.getLogger(__name__) |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class ImagineFilesystemAdapter(ImagineAdapterInterface): |
15
|
|
|
""" |
16
|
|
|
Filesystem storage adapter |
17
|
|
|
""" |
18
|
|
|
source_folder = None |
19
|
|
|
cache_folder = None |
20
|
|
|
|
21
|
|
|
def __init__(self, **kwargs): |
22
|
|
|
""" |
23
|
|
|
Init adapter |
24
|
|
|
:param kwargs: parameters |
25
|
|
|
:return: |
26
|
|
|
""" |
27
|
|
|
self.source_folder = kwargs.get('source_folder', '').strip('/') |
28
|
|
|
self.cache_folder = kwargs.get('cache_folder', 'cache').strip('/') |
29
|
|
|
|
30
|
|
|
def get_item(self, path): |
31
|
|
|
""" |
32
|
|
|
Get resource item |
33
|
|
|
:param path: string |
34
|
|
|
:return: Image |
35
|
|
|
""" |
36
|
|
|
if self.source_folder: |
37
|
|
|
item_path = '%s/%s/%s' % ( |
38
|
|
|
current_app.static_folder, |
39
|
|
|
self.source_folder, |
40
|
|
|
path.strip('/') |
41
|
|
|
) |
42
|
|
|
else: |
43
|
|
|
|
44
|
|
|
item_path = '%s/%s' % ( |
45
|
|
|
current_app.static_folder, |
46
|
|
|
path.strip('/') |
47
|
|
|
) |
48
|
|
|
|
49
|
|
|
print item_path |
50
|
|
|
|
51
|
|
|
if os.path.isfile(item_path): |
52
|
|
|
try: |
53
|
|
|
return Image.open(item_path) |
54
|
|
|
except IOError, e: |
55
|
|
|
LOGGER.warning('File not found on path "%s" with error: %s' % (item_path, unicode(e))) |
56
|
|
|
return False |
57
|
|
|
else: |
58
|
|
|
return False |
59
|
|
|
|
60
|
|
|
def create_cached_item(self, path, content): |
61
|
|
|
""" |
62
|
|
|
Create cached resource item |
63
|
|
|
:param path: string |
64
|
|
|
:param content: Image |
65
|
|
|
:return: |
66
|
|
|
""" |
67
|
|
|
if isinstance(content, Image.Image): |
68
|
|
|
item_path = '%s/%s/%s' % ( |
69
|
|
|
current_app.static_folder, |
70
|
|
|
self.cache_folder, |
71
|
|
|
path.strip('/') |
72
|
|
|
) |
73
|
|
|
self.make_dirs(item_path) |
74
|
|
|
|
75
|
|
|
content.save(item_path) |
76
|
|
|
|
77
|
|
|
if os.path.isfile(item_path): |
78
|
|
|
return '%s/%s/%s' % (current_app.static_url_path, self.cache_folder, path.strip('/')) |
79
|
|
|
else: # pragma: no cover |
80
|
|
|
LOGGER.warning('File is not created on path: %s' % item_path) |
81
|
|
|
return False |
82
|
|
|
else: |
83
|
|
|
return False |
84
|
|
|
|
85
|
|
|
def get_cached_item(self, path): |
86
|
|
|
""" |
87
|
|
|
Get cached resource item |
88
|
|
|
:param path: string |
89
|
|
|
:return: |
90
|
|
|
""" |
91
|
|
|
item_path = '%s/%s/%s' % ( |
92
|
|
|
current_app.static_folder, |
93
|
|
|
self.cache_folder, |
94
|
|
|
path.strip('/') |
95
|
|
|
) |
96
|
|
|
|
97
|
|
|
if os.path.isfile(item_path): |
98
|
|
|
try: |
99
|
|
|
return Image.open(item_path) |
100
|
|
|
except IOError, e: # pragma: no cover |
101
|
|
|
LOGGER.warning('Cached file not found on path "%s" with error: %s' % (item_path, unicode(e))) |
102
|
|
|
return False |
103
|
|
|
else: |
104
|
|
|
return False |
105
|
|
|
|
106
|
|
|
def check_cached_item(self, path): |
107
|
|
|
""" |
108
|
|
|
Check for cached resource item exists |
109
|
|
|
:param path: string |
110
|
|
|
:return: |
111
|
|
|
""" |
112
|
|
|
item_path = '%s/%s/%s' % ( |
113
|
|
|
current_app.static_folder, |
114
|
|
|
self.cache_folder, |
115
|
|
|
path.strip('/') |
116
|
|
|
) |
117
|
|
|
|
118
|
|
|
if os.path.isfile(item_path): |
119
|
|
|
return '%s/%s/%s' % (current_app.static_url_path, self.cache_folder, path.strip('/')) |
120
|
|
|
else: |
121
|
|
|
return False |
122
|
|
|
|
123
|
|
|
def remove_cached_item(self, path): |
124
|
|
|
""" |
125
|
|
|
Remove cached resource item |
126
|
|
|
:param path: string |
127
|
|
|
:return: |
128
|
|
|
""" |
129
|
|
|
item_path = '%s/%s/%s' % ( |
130
|
|
|
current_app.static_folder, |
131
|
|
|
self.cache_folder, |
132
|
|
|
path.strip('/') |
133
|
|
|
) |
134
|
|
|
|
135
|
|
|
if os.path.isfile(item_path): |
136
|
|
|
os.remove(item_path) |
137
|
|
|
|
138
|
|
|
return True |
139
|
|
|
|
140
|
|
|
@staticmethod |
141
|
|
|
def make_dirs(path): |
142
|
|
|
""" |
143
|
|
|
Create directories if not exist |
144
|
|
|
:param path: string |
145
|
|
|
:return: |
146
|
|
|
""" |
147
|
|
|
try: |
148
|
|
|
os.makedirs(os.path.dirname(path)) |
149
|
|
|
except OSError as e: |
150
|
|
|
if e.errno != errno.EEXIST: |
151
|
|
|
LOGGER.error('Failed to create directory %s with error: %s' % (path, unicode(e))) |
152
|
|
|
raise |
153
|
|
|
|