5 import os, re, datetime
6 from time import gmtime, strftime
7 from django.conf import settings
8 from django.utils.encoding import smart_unicode
12 from filebrowser.fb_settings import *
13 from filebrowser.functions import _get_file_type, _url_join, _is_selectable, _get_version_path
25 def filesystem_encoding(ucode):
26 ucode = ucode.encode('utf-8')
27 ucode = urllib.quote(ucode)
30 class FileObject(object):
32 The FileObject represents a File on the Server.
34 PATH has to be relative to MEDIA_ROOT.
37 def __init__(self, path):
39 self.head = os.path.split(path)[0]
40 self.filename = os.path.split(path)[1]
41 self.filename_lower = self.filename.lower() # important for sorting
42 self.filetype = _get_file_type(self.filename)
48 if os.path.isfile(os.path.join(MEDIA_ROOT, self.path)) or os.path.isdir(os.path.join(MEDIA_ROOT, self.path)):
49 return os.path.getsize(os.path.join(MEDIA_ROOT, self.path))
51 filesize = property(_filesize)
57 if os.path.isfile(os.path.join(MEDIA_ROOT, self.path)) or os.path.isdir(os.path.join(MEDIA_ROOT, self.path)):
58 return os.path.getmtime(os.path.join(MEDIA_ROOT, self.path))
59 # IMHO this should raise an exception
61 date = property(_date)
67 return datetime.datetime.fromtimestamp(self.date)
68 datetime = property(_datetime)
74 return u"%s" % os.path.splitext(self.filename)[1]
75 extension = property(_extension)
77 def _filetype_checked(self):
78 if self.filetype == "Folder" and os.path.isdir(self.path_full):
80 elif self.filetype != "Folder" and os.path.isfile(self.path_full):
84 filetype_checked = property(_filetype_checked)
88 Full server PATH including MEDIA_ROOT.
90 return u"%s" % os.path.join(MEDIA_ROOT, self.path)
91 path_full = property(_path_full)
93 def _path_relative(self):
95 path_relative = property(_path_relative)
97 def _path_relative_directory(self):
99 Path relative to initial directory.
101 directory_re = re.compile(r'^(%s)' % (DIRECTORY))
102 value = directory_re.sub('', self.path)
104 path_relative_directory = property(_path_relative_directory)
106 def _url_relative(self):
108 url_relative = property(_url_relative)
112 Full URL including MEDIA_URL.
114 return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, self.path))
116 url_full = property(_url_full)
120 URL used for the filebrowsefield.
125 return filesystem_encoding(self.path)
126 url_save = property(_url_save)
128 def _url_thumbnail(self):
132 if self.filetype == "Image":
133 return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, _get_version_path(self.path, u'fb_thumb')))
136 url_thumbnail = property(_url_thumbnail)
139 if self.filetype_checked == "Folder":
140 directory_re = re.compile(r'^(%s)' % (DIRECTORY))
141 value = directory_re.sub('', self.path)
142 return filesystem_encoding(u"%s" % value)
144 return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, self.path))
146 def _dimensions(self):
150 if self.filetype == 'Image':
152 im = Image.open(os.path.join(MEDIA_ROOT, self.path))
158 dimensions = property(_dimensions)
164 return self.dimensions[0]
165 width = property(_width)
171 return self.dimensions[1]
172 height = property(_height)
174 def _orientation(self):
179 if self.dimensions[0] >= self.dimensions[1]:
185 orientation = property(_orientation)
189 True if Folder is empty, False if not.
191 if os.path.isdir(self.path_full):
192 if not os.listdir(self.path_full):
198 is_empty = property(_is_empty)
201 return u"%s" % self.url_save
204 return u"%s" % self.url_save
206 def __unicode__(self):
207 return u"%s" % self.url_save