da53d91f852cdb9fef8f7225e8c44fde99694542
[redakcja.git] / apps / filebrowser / base.py
1 # coding: utf-8
2
3 import locale
4
5 import os, re, datetime
6 from time import gmtime, strftime
7 from django.conf import settings
8 import urllib
9
10 # filebrowser imports
11 from filebrowser.fb_settings import *
12 from filebrowser.functions import _get_file_type, _url_join, _is_selectable, _get_version_path
13
14 # PIL import
15 if STRICT_PIL:
16     from PIL import Image
17 else:
18     try:
19         from PIL import Image
20     except ImportError:
21         import Image
22
23
24 def filesystem_encoding(ucode):
25     ucode = ucode.encode('utf-8')
26     ucode = urllib.quote(ucode)
27     return ucode
28
29 class FileObject(object):
30     """
31     The FileObject represents a File on the Server.
32     
33     PATH has to be relative to MEDIA_ROOT.
34     """
35
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)
42
43     def _filesize(self):
44         """
45         Filesize.
46         """
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))
49         return ""
50     filesize = property(_filesize)
51
52     def _date(self):
53         """
54         Date.
55         """
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
59         return None
60     date = property(_date)
61
62     def _datetime(self):
63         """
64         Datetime Object.
65         """
66         return datetime.datetime.fromtimestamp(self.date)
67     datetime = property(_datetime)
68
69     def _extension(self):
70         """
71         Extension.
72         """
73         return u"%s" % os.path.splitext(self.filename)[1]
74     extension = property(_extension)
75
76     def _filetype_checked(self):
77         if self.filetype == "Folder" and os.path.isdir(self.path_full):
78             return self.filetype
79         elif self.filetype != "Folder" and os.path.isfile(self.path_full):
80             return self.filetype
81         else:
82             return ""
83     filetype_checked = property(_filetype_checked)
84
85     def _path_full(self):
86         """
87         Full server PATH including MEDIA_ROOT.
88         """
89         return u"%s" % os.path.join(MEDIA_ROOT, self.path)
90     path_full = property(_path_full)
91
92     def _path_relative(self):
93         return self.path
94     path_relative = property(_path_relative)
95
96     def _path_relative_directory(self):
97         """
98         Path relative to initial directory.
99         """
100         directory_re = re.compile(r'^(%s)' % (DIRECTORY))
101         value = directory_re.sub('', self.path)
102         return u"%s" % value
103     path_relative_directory = property(_path_relative_directory)
104
105     def _url_relative(self):
106         return self.path
107     url_relative = property(_url_relative)
108
109     def _url_full(self):
110         """
111         Full URL including MEDIA_URL.
112         """
113         return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, self.path))
114
115     url_full = property(_url_full)
116
117     def _url_save(self):
118         """
119         URL used for the filebrowsefield.
120         """
121         if SAVE_FULL_URL:
122             return self.url_full
123         else:
124             return filesystem_encoding(self.path)
125     url_save = property(_url_save)
126
127     def _url_thumbnail(self):
128         """
129         Thumbnail URL.
130         """
131         if self.filetype == "Image":
132             return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, _get_version_path(self.path, u'fb_thumb')))
133         else:
134             return ""
135     url_thumbnail = property(_url_thumbnail)
136
137     def url_admin(self):
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)
142         else:
143             return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, self.path))
144
145     def _dimensions(self):
146         """
147         Image Dimensions.
148         """
149         if self.filetype == 'Image':
150             try:
151                 im = Image.open(os.path.join(MEDIA_ROOT, self.path))
152                 return im.size
153             except:
154                 pass
155         else:
156             return False
157     dimensions = property(_dimensions)
158
159     def _width(self):
160         """
161         Image Width.
162         """
163         return self.dimensions[0]
164     width = property(_width)
165
166     def _height(self):
167         """
168         Image Height.
169         """
170         return self.dimensions[1]
171     height = property(_height)
172
173     def _orientation(self):
174         """
175         Image Orientation.
176         """
177         if self.dimensions:
178             if self.dimensions[0] >= self.dimensions[1]:
179                 return "Landscape"
180             else:
181                 return "Portrait"
182         else:
183             return None
184     orientation = property(_orientation)
185
186     def _is_empty(self):
187         """
188         True if Folder is empty, False if not.
189         """
190         if os.path.isdir(self.path_full):
191             if not os.listdir(self.path_full):
192                 return True
193             else:
194                 return False
195         else:
196             return None
197     is_empty = property(_is_empty)
198
199     def __repr__(self):
200         return u"%s" % self.url_save
201
202     def __str__(self):
203         return u"%s" % self.url_save
204
205     def __unicode__(self):
206         return u"%s" % self.url_save
207
208
209