|
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
|
|
|
if 'source_folder' in kwargs: |
|
28
|
|
|
self.source_folder = kwargs.pop('source_folder').strip('/') |
|
29
|
|
|
else: |
|
30
|
|
|
raise ValueError('Source folder does not set.') |
|
31
|
|
|
|
|
32
|
|
|
self.cache_folder = kwargs.pop('cache_folder', 'cache').strip('/') |
|
33
|
|
|
|
|
34
|
|
|
def get_item(self, path): |
|
35
|
|
|
""" |
|
36
|
|
|
Get resource item |
|
37
|
|
|
:param path: string |
|
38
|
|
|
:return: Image |
|
39
|
|
|
""" |
|
40
|
|
|
item_path = '%s/%s/%s' % ( |
|
41
|
|
|
current_app.root_path, |
|
42
|
|
|
self.source_folder, |
|
43
|
|
|
path.strip('/') |
|
44
|
|
|
) |
|
45
|
|
|
|
|
46
|
|
|
if os.path.isfile(item_path): |
|
47
|
|
|
try: |
|
48
|
|
|
return Image.open(item_path) |
|
49
|
|
|
except IOError, e: |
|
50
|
|
|
LOGGER.warning('File not found on path "%s" with error: %s' % (item_path, unicode(e))) |
|
51
|
|
|
return False |
|
52
|
|
|
else: |
|
53
|
|
|
return False |
|
54
|
|
|
|
|
55
|
|
|
def create_cached_item(self, path, content): |
|
56
|
|
|
""" |
|
57
|
|
|
Create cached resource item |
|
58
|
|
|
:param path: string |
|
59
|
|
|
:param content: Image |
|
60
|
|
|
:return: |
|
61
|
|
|
""" |
|
62
|
|
|
if isinstance(content, Image.Image): |
|
63
|
|
|
item_path = '%s/%s/%s/%s' % ( |
|
64
|
|
|
current_app.root_path, |
|
65
|
|
|
self.source_folder, |
|
66
|
|
|
self.cache_folder, |
|
67
|
|
|
path.strip('/') |
|
68
|
|
|
) |
|
69
|
|
|
self.make_dirs(item_path) |
|
70
|
|
|
|
|
71
|
|
|
content.save(item_path) |
|
72
|
|
|
|
|
73
|
|
|
if os.path.isfile(item_path): |
|
74
|
|
|
return '/%s/%s/%s' % (self.source_folder, self.cache_folder, path.strip('/')) |
|
75
|
|
|
else: |
|
76
|
|
|
LOGGER.warning('File is not created on path: %s' % item_path) |
|
77
|
|
|
return False |
|
78
|
|
|
else: |
|
79
|
|
|
return False |
|
80
|
|
|
|
|
81
|
|
|
def get_cached_item(self, path): |
|
82
|
|
|
""" |
|
83
|
|
|
Get cached resource item |
|
84
|
|
|
:param path: string |
|
85
|
|
|
:return: |
|
86
|
|
|
""" |
|
87
|
|
|
item_path = '%s/%s/%s/%s' % ( |
|
88
|
|
|
current_app.root_path, |
|
89
|
|
|
self.source_folder, |
|
90
|
|
|
self.cache_folder, |
|
91
|
|
|
path.strip('/') |
|
92
|
|
|
) |
|
93
|
|
|
|
|
94
|
|
|
if os.path.isfile(item_path): |
|
95
|
|
|
try: |
|
96
|
|
|
return Image.open(item_path) |
|
97
|
|
|
except IOError, e: |
|
98
|
|
|
LOGGER.warning('Cached file not found on path "%s" with error: %s' % (item_path, unicode(e))) |
|
99
|
|
|
return False |
|
100
|
|
|
else: |
|
101
|
|
|
return False |
|
102
|
|
|
|
|
103
|
|
|
def check_cached_item(self, path): |
|
104
|
|
|
""" |
|
105
|
|
|
Check for cached resource item exists |
|
106
|
|
|
:param path: string |
|
107
|
|
|
:return: |
|
108
|
|
|
""" |
|
109
|
|
|
item_path = '%s/%s/%s/%s' % ( |
|
110
|
|
|
current_app.root_path, |
|
111
|
|
|
self.source_folder, |
|
112
|
|
|
self.cache_folder, |
|
113
|
|
|
path.strip('/') |
|
114
|
|
|
) |
|
115
|
|
|
|
|
116
|
|
|
return os.path.isfile(item_path) |
|
117
|
|
|
|
|
118
|
|
|
def remove_cached_item(self, path): |
|
119
|
|
|
""" |
|
120
|
|
|
Remove cached resource item |
|
121
|
|
|
:param path: string |
|
122
|
|
|
:return: |
|
123
|
|
|
""" |
|
124
|
|
|
item_path = '%s/%s/%s/%s' % ( |
|
125
|
|
|
current_app.root_path, |
|
126
|
|
|
self.source_folder, |
|
127
|
|
|
self.cache_folder, |
|
128
|
|
|
path.strip('/') |
|
129
|
|
|
) |
|
130
|
|
|
|
|
131
|
|
|
if os.path.isfile(item_path): |
|
132
|
|
|
os.remove(item_path) |
|
133
|
|
|
|
|
134
|
|
|
return True |
|
135
|
|
|
|
|
136
|
|
|
@staticmethod |
|
137
|
|
|
def make_dirs(path): |
|
138
|
|
|
""" |
|
139
|
|
|
Create directories if not exist |
|
140
|
|
|
:param path: string |
|
141
|
|
|
:return: |
|
142
|
|
|
""" |
|
143
|
|
|
try: |
|
144
|
|
|
os.makedirs(os.path.dirname(path)) |
|
145
|
|
|
except OSError as e: |
|
146
|
|
|
if e.errno != errno.EEXIST: |
|
147
|
|
|
LOGGER.error('Failed to create directory %s with error: %s' % (path, unicode(e))) |
|
148
|
|
|
raise |
|
149
|
|
|
|