3 from django.utils.translation import ugettext as _
4 from django.utils.safestring import mark_safe
5 from time import gmtime, strftime, localtime, mktime, time
6 from django.core.files import File
7 from django.core.files.storage import default_storage
9 from urlparse import urlparse
12 from filebrowser.fb_settings import *
24 def _url_to_path(value):
27 Value has to be an URL relative to MEDIA URL or a full URL (including MEDIA_URL).
29 Returns a PATH relative to MEDIA_ROOT.
31 print "URL2PATH", repr(value)
32 mediaurl_re = re.compile(r'^(%s)' % (MEDIA_URL))
33 value = mediaurl_re.sub(u'', value)
37 def _path_to_url(value):
40 Value has to be a PATH relative to MEDIA_ROOT.
42 Return an URL relative to MEDIA_ROOT.
44 mediaroot_re = re.compile(r'^(%s)' % (MEDIA_ROOT))
45 value = mediaroot_re.sub('', value)
46 return _url_join(MEDIA_URL, value)
49 def _dir_from_url(value):
51 Get the relative server directory from a URL.
52 URL has to be an absolute URL including MEDIA_URL or
53 an URL relative to MEDIA_URL.
56 mediaurl_re = re.compile(r'^(%s)' % (MEDIA_URL))
57 value = mediaurl_re.sub('', value)
58 directory_re = re.compile(r'^(%s)' % (DIRECTORY))
59 value = directory_re.sub('', value)
60 return os.path.split(value)[0]
63 def _get_version_path(value, version_prefix):
65 Construct the PATH to an Image version.
66 Value has to be server-path, relative to MEDIA_ROOT.
68 version_filename = filename + version_prefix + ext
69 Returns a path relative to MEDIA_ROOT.
71 if os.path.isfile(os.path.join(MEDIA_ROOT, value)):
72 path, filename = os.path.split(value)
73 filename, ext = os.path.splitext(filename)
74 version_filename = filename + u"_" + version_prefix + ext
75 return os.path.join(VERSIONS_BASEDIR, path, version_filename)
81 if args[0].startswith(u"http://"):
86 arg = unicode(arg).replace(u"\\", u"/")
87 arg_split = arg.split(u"/")
88 for elem in arg_split:
89 if elem != u"" and elem != u"http:":
90 url = url + elem + u"/"
91 # remove trailing slash for filenames
92 if os.path.splitext(args[-1])[1]:
93 url = url.rstrip(u"/")
102 if os.path.isabs(path) or not os.path.isdir(os.path.join(MEDIA_ROOT, DIRECTORY, path)):
107 def _get_file(path, filename):
112 if not os.path.isfile(os.path.join(MEDIA_ROOT, DIRECTORY, path, filename)) and not os.path.isdir(os.path.join(MEDIA_ROOT, DIRECTORY, path, filename)):
117 def _get_breadcrumbs(query, path, title):
125 for item in path.split(os.sep):
126 dir_query = os.path.join(dir_query, item)
127 breadcrumbs.append([item, dir_query])
129 breadcrumbs.append([title, ''])
133 def _get_filterdate(filterDate, dateTime):
139 dateYear = strftime("%Y", gmtime(dateTime))
140 dateMonth = strftime("%m", gmtime(dateTime))
141 dateDay = strftime("%d", gmtime(dateTime))
142 if filterDate == 'today' and int(dateYear) == int(localtime()[0]) and int(dateMonth) == int(localtime()[1]) and int(dateDay) == int(localtime()[2]): returnvalue = 'true'
143 elif filterDate == 'thismonth' and dateTime >= time() - 2592000: returnvalue = 'true'
144 elif filterDate == 'thisyear' and int(dateYear) == int(localtime()[0]): returnvalue = 'true'
145 elif filterDate == 'past7days' and dateTime >= time() - 604800: returnvalue = 'true'
146 elif filterDate == '': returnvalue = 'true'
150 def _get_settings_var():
152 Get settings variables used for FileBrowser listing.
157 settings_var['DEBUG'] = DEBUG
158 settings_var['MEDIA_ROOT'] = MEDIA_ROOT
159 settings_var['MEDIA_URL'] = MEDIA_URL
160 settings_var['DIRECTORY'] = DIRECTORY
162 settings_var['URL_FILEBROWSER_MEDIA'] = URL_FILEBROWSER_MEDIA
163 settings_var['PATH_FILEBROWSER_MEDIA'] = PATH_FILEBROWSER_MEDIA
165 settings_var['URL_TINYMCE'] = URL_TINYMCE
166 settings_var['PATH_TINYMCE'] = PATH_TINYMCE
167 # Extensions/Formats (for FileBrowseField)
168 settings_var['EXTENSIONS'] = EXTENSIONS
169 settings_var['SELECT_FORMATS'] = SELECT_FORMATS
171 settings_var['VERSIONS_BASEDIR'] = VERSIONS_BASEDIR
172 settings_var['VERSIONS'] = VERSIONS
173 settings_var['ADMIN_VERSIONS'] = ADMIN_VERSIONS
174 settings_var['ADMIN_THUMBNAIL'] = ADMIN_THUMBNAIL
175 # FileBrowser Options
176 settings_var['MAX_UPLOAD_SIZE'] = MAX_UPLOAD_SIZE
178 settings_var['CONVERT_FILENAME'] = CONVERT_FILENAME
182 def _handle_file_upload(path, file):
187 file_path = os.path.join(path, file.name)
188 uploadedfile = default_storage.save(file_path, file)
192 def _get_file_type(filename):
194 Get file type as defined in EXTENSIONS.
196 file_extension = os.path.splitext(filename)[1].lower()
197 for ftype, ext_list in EXTENSIONS.iteritems():
198 if file_extension in map(str.lower, ext_list):
203 def _is_selectable(filename, selecttype):
205 Get select type as defined in FORMATS.
208 file_extension = os.path.splitext(filename)[1].lower()
210 for k, v in SELECT_FORMATS.iteritems():
212 if file_extension == extension.lower():
213 select_types.append(k)
217 def _version_generator(value, version_prefix, force = None):
219 Generate Version for an Image.
220 value has to be a serverpath relative to MEDIA_ROOT.
223 # PIL's Error "Suspension not allowed here" work around:
224 # s. http://mail.python.org/pipermail/image-sig/1999-August/000816.html
226 from PIL import ImageFile
229 from PIL import ImageFile
232 ImageFile.MAXBLOCK = IMAGE_MAXBLOCK # default is 64k
235 im = Image.open(os.path.join(MEDIA_ROOT, value))
236 version_path = _get_version_path(value, version_prefix)
237 absolute_version_path = os.path.join(MEDIA_ROOT, version_path)
238 version_dir = os.path.split(absolute_version_path)[0]
239 if not os.path.isdir(version_dir):
240 os.makedirs(version_dir)
241 os.chmod(version_dir, 0775)
242 version = scale_and_crop(im, VERSIONS[version_prefix]['width'], VERSIONS[version_prefix]['height'], VERSIONS[version_prefix]['opts'])
244 version.save(absolute_version_path, quality = 90, optimize = 1)
246 version.save(absolute_version_path, quality = 90)
252 def scale_and_crop(im, width, height, opts):
253 x, y = [float(v) for v in im.size]
257 xr = float(x * height / y)
261 yr = float(y * width / x)
264 r = max(xr / x, yr / y)
266 r = min(xr / x, yr / y)
268 if r < 1.0 or (r > 1.0 and 'upscale' in opts):
269 im = im.resize((int(x * r), int(y * r)), resample = Image.ANTIALIAS)
272 x, y = [float(v) for v in im.size]
273 ex, ey = (x - min(x, xr)) / 2, (y - min(y, yr)) / 2
275 im = im.crop((int(ex), int(ey), int(x - ex), int(y - ey)))
277 scale_and_crop.valid_options = ('crop', 'upscale')
280 def _convert_filename(value):
282 return value.replace(" ", "_").lower()