3 import os, re, datetime
4 from time import gmtime, strftime
5 from django.conf import settings
8 from filebrowser.fb_settings import *
9 from filebrowser.functions import _get_file_type, _url_join, _is_selectable, _get_version_path
21 class FileObject(object):
23 The FileObject represents a File on the Server.
25 PATH has to be relative to MEDIA_ROOT.
28 def __init__(self, path):
30 self.head = os.path.split(path)[0]
31 self.filename = os.path.split(path)[1]
32 self.filename_lower = self.filename.lower() # important for sorting
33 self.filetype = _get_file_type(self.filename)
39 if os.path.isfile(os.path.join(MEDIA_ROOT, self.path)) or os.path.isdir(os.path.join(MEDIA_ROOT, self.path)):
40 return os.path.getsize(os.path.join(MEDIA_ROOT, self.path))
42 filesize = property(_filesize)
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.getmtime(os.path.join(MEDIA_ROOT, self.path))
51 date = property(_date)
57 return datetime.datetime.fromtimestamp(self.date)
58 datetime = property(_datetime)
64 return u"%s" % os.path.splitext(self.filename)[1]
65 extension = property(_extension)
67 def _filetype_checked(self):
68 if self.filetype == "Folder" and os.path.isdir(self.path_full):
70 elif self.filetype != "Folder" and os.path.isfile(self.path_full):
74 filetype_checked = property(_filetype_checked)
78 Full server PATH including MEDIA_ROOT.
80 return u"%s" % os.path.join(MEDIA_ROOT, self.path)
81 path_full = property(_path_full)
83 def _path_relative(self):
85 path_relative = property(_path_relative)
87 def _path_relative_directory(self):
89 Path relative to initial directory.
91 directory_re = re.compile(r'^(%s)' % (DIRECTORY))
92 value = directory_re.sub('', self.path)
94 path_relative_directory = property(_path_relative_directory)
96 def _url_relative(self):
98 url_relative = property(_url_relative)
102 Full URL including MEDIA_URL.
104 return u"%s" % _url_join(MEDIA_URL, self.path)
105 url_full = property(_url_full)
109 URL used for the filebrowsefield.
115 url_save = property(_url_save)
117 def _url_thumbnail(self):
121 if self.filetype == "Image":
122 return u"%s" % _url_join(MEDIA_URL, _get_version_path(self.path, 'fb_thumb'))
125 url_thumbnail = property(_url_thumbnail)
128 if self.filetype_checked == "Folder":
129 directory_re = re.compile(r'^(%s)' % (DIRECTORY))
130 value = directory_re.sub('', self.path)
133 return u"%s" % _url_join(MEDIA_URL, self.path)
135 def _dimensions(self):
139 if self.filetype == 'Image':
141 im = Image.open(os.path.join(MEDIA_ROOT, self.path))
147 dimensions = property(_dimensions)
153 return self.dimensions[0]
154 width = property(_width)
160 return self.dimensions[1]
161 height = property(_height)
163 def _orientation(self):
168 if self.dimensions[0] >= self.dimensions[1]:
174 orientation = property(_orientation)
178 True if Folder is empty, False if not.
180 if os.path.isdir(self.path_full):
181 if not os.listdir(self.path_full):
187 is_empty = property(_is_empty)
190 return u"%s" % self.url_save
193 return u"%s" % self.url_save
195 def __unicode__(self):
196 return u"%s" % self.url_save