5 import os, re, datetime
6 from time import gmtime, strftime
7 from django.conf import settings
11 from filebrowser.fb_settings import *
12 from filebrowser.functions import _get_file_type, _url_join, _is_selectable, _get_version_path
24 def filesystem_encoding(ucode):
25 ucode = ucode.encode('utf-8')
26 ucode = urllib.quote(ucode)
29 class FileObject(object):
31 The FileObject represents a File on the Server.
33 PATH has to be relative to MEDIA_ROOT.
36 def __init__(self, path):
37 self.path = unicode(path)
38 self.head = os.path.split(path)[0]
39 self.filename = os.path.split(path)[1]
40 self.filename_lower = self.filename.lower() # important for sorting
41 self.filetype = _get_file_type(self.filename)
47 if os.path.isfile(os.path.join(MEDIA_ROOT, self.path)) or os.path.isdir(os.path.join(MEDIA_ROOT, self.path)):
48 return os.path.getsize(os.path.join(MEDIA_ROOT, self.path))
50 filesize = property(_filesize)
56 if os.path.isfile(os.path.join(MEDIA_ROOT, self.path)) or os.path.isdir(os.path.join(MEDIA_ROOT, self.path)):
57 return os.path.getmtime(os.path.join(MEDIA_ROOT, self.path))
58 # IMHO this should raise an exception
60 date = property(_date)
66 return datetime.datetime.fromtimestamp(self.date)
67 datetime = property(_datetime)
73 return u"%s" % os.path.splitext(self.filename)[1]
74 extension = property(_extension)
76 def _filetype_checked(self):
77 if self.filetype == "Folder" and os.path.isdir(self.path_full):
79 elif self.filetype != "Folder" and os.path.isfile(self.path_full):
83 filetype_checked = property(_filetype_checked)
87 Full server PATH including MEDIA_ROOT.
89 return u"%s" % os.path.join(MEDIA_ROOT, self.path)
90 path_full = property(_path_full)
92 def _path_relative(self):
94 path_relative = property(_path_relative)
96 def _path_relative_directory(self):
98 Path relative to initial directory.
100 directory_re = re.compile(r'^(%s)' % (DIRECTORY))
101 value = directory_re.sub('', self.path)
103 path_relative_directory = property(_path_relative_directory)
105 def _url_relative(self):
107 url_relative = property(_url_relative)
111 Full URL including MEDIA_URL.
113 return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, self.path))
115 url_full = property(_url_full)
119 URL used for the filebrowsefield.
124 return filesystem_encoding(self.path)
125 url_save = property(_url_save)
127 def _url_thumbnail(self):
131 if self.filetype == "Image":
132 return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, _get_version_path(self.path, u'fb_thumb')))
135 url_thumbnail = property(_url_thumbnail)
138 if self.filetype_checked == "Folder":
139 directory_re = re.compile(r'^(%s)' % (DIRECTORY))
140 value = directory_re.sub('', self.path)
141 return filesystem_encoding(u"%s" % value)
143 return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, self.path))
145 def _dimensions(self):
149 if self.filetype == 'Image':
151 im = Image.open(os.path.join(MEDIA_ROOT, self.path))
157 dimensions = property(_dimensions)
163 return self.dimensions[0]
164 width = property(_width)
170 return self.dimensions[1]
171 height = property(_height)
173 def _orientation(self):
178 if self.dimensions[0] >= self.dimensions[1]:
184 orientation = property(_orientation)
188 True if Folder is empty, False if not.
190 if os.path.isdir(self.path_full):
191 if not os.listdir(self.path_full):
197 is_empty = property(_is_empty)
200 return u"%s" % self.url_save
203 return u"%s" % self.url_save
205 def __unicode__(self):
206 return u"%s" % self.url_save