3 from django.conf import settings
4 from django.utils.encoding import smart_unicode
5 from filebrowser.fb_settings import *
7 from filebrowser.functions import _get_file_type, _url_join, _is_selectable, \
10 from time import gmtime, strftime
31 def filesystem_encoding(ucode):
32 ucode = ucode.encode('utf-8')
33 ucode = urllib.quote(ucode)
37 class FileObject(object):
39 The FileObject represents a File on the Server.
41 PATH has to be relative to MEDIA_ROOT.
44 def __init__(self, path):
46 self.head = os.path.split(path)[0]
47 self.filename = os.path.split(path)[1]
48 self.filename_lower = self.filename.lower() # important for sorting
49 self.filetype = _get_file_type(self.filename)
55 if os.path.isfile(os.path.join(MEDIA_ROOT, self.path)) or os.path.isdir(os.path.join(MEDIA_ROOT, self.path)):
56 return os.path.getsize(os.path.join(MEDIA_ROOT, self.path))
58 filesize = property(_filesize)
64 if os.path.isfile(os.path.join(MEDIA_ROOT, self.path)) or os.path.isdir(os.path.join(MEDIA_ROOT, self.path)):
65 return os.path.getmtime(os.path.join(MEDIA_ROOT, self.path))
66 # IMHO this should raise an exception
68 date = property(_date)
74 return datetime.datetime.fromtimestamp(self.date)
75 datetime = property(_datetime)
81 return u"%s" % os.path.splitext(self.filename)[1]
82 extension = property(_extension)
84 def _filetype_checked(self):
85 if self.filetype == "Folder" and os.path.isdir(self.path_full):
87 elif self.filetype != "Folder" and os.path.isfile(self.path_full):
91 filetype_checked = property(_filetype_checked)
95 Full server PATH including MEDIA_ROOT.
97 return u"%s" % os.path.join(MEDIA_ROOT, self.path)
98 path_full = property(_path_full)
100 def _path_relative(self):
102 path_relative = property(_path_relative)
104 def _path_relative_directory(self):
106 Path relative to initial directory.
108 directory_re = re.compile(r'^(%s)' % (DIRECTORY))
109 value = directory_re.sub('', self.path)
111 path_relative_directory = property(_path_relative_directory)
113 def _url_relative(self):
115 url_relative = property(_url_relative)
119 Full URL including MEDIA_URL.
121 return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, self.path))
123 url_full = property(_url_full)
127 URL used for the filebrowsefield.
132 return filesystem_encoding(self.path)
133 url_save = property(_url_save)
135 def _url_thumbnail(self):
139 if self.filetype == "Image":
140 return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, _get_version_path(self.path, u'fb_thumb')))
143 url_thumbnail = property(_url_thumbnail)
146 if self.filetype_checked == "Folder":
147 directory_re = re.compile(r'^(%s)' % (DIRECTORY))
148 value = directory_re.sub('', self.path)
149 return filesystem_encoding(u"%s" % value)
151 return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, self.path))
153 def _dimensions(self):
157 if self.filetype == 'Image':
159 im = Image.open(os.path.join(MEDIA_ROOT, self.path))
165 dimensions = property(_dimensions)
171 return self.dimensions[0]
172 width = property(_width)
178 return self.dimensions[1]
179 height = property(_height)
181 def _orientation(self):
186 if self.dimensions[0] >= self.dimensions[1]:
192 orientation = property(_orientation)
196 True if Folder is empty, False if not.
198 if os.path.isdir(self.path_full):
199 if not os.listdir(self.path_full):
205 is_empty = property(_is_empty)
208 return u"%s" % self.url_save
211 return u"%s" % self.url_save
213 def __unicode__(self):
214 return u"%s" % self.url_save