5 from time import gmtime, strftime
8 from django.shortcuts import render_to_response, HttpResponse
9 from django.template import RequestContext as Context
10 from django.http import HttpResponseRedirect
11 from django.contrib.admin.views.decorators import staff_member_required
12 from django.views.decorators.cache import never_cache
13 from django.utils.translation import ugettext as _
14 from django.conf import settings
15 from django import forms
16 from django.core.urlresolvers import reverse
17 from django.core.exceptions import ImproperlyConfigured
18 from django.dispatch import Signal
20 from django.utils.encoding import smart_unicode, smart_str
23 from filebrowser.fb_settings import *
24 from filebrowser.functions import _url_to_path, _path_to_url, _get_path, _get_file, _get_version_path, _get_breadcrumbs, _get_filterdate, _get_settings_var, _handle_file_upload, _get_file_type, _url_join, _convert_filename
25 from filebrowser.templatetags.fb_tags import query_helper
26 from filebrowser.base import FileObject
27 from filebrowser.decorators import flash_login_required
29 # Precompile regular expressions
32 filter_re.append(re.compile(exp))
33 for k, v in VERSIONS.iteritems():
34 exp = (r'_%s.(%s)') % (k, '|'.join(EXTENSION_LIST))
35 filter_re.append(re.compile(exp))
40 Browse Files/Directories.
45 path = _get_path(query.get('dir', ''))
46 directory = _get_path('')
49 msg = _('Directory/File does not exist.')
50 request.user.message_set.create(message = msg)
52 # The DIRECTORY does not exist, raise an error to prevent eternal redirecting.
53 raise ImproperlyConfigured, _("Error finding upload directory. Maybe it does not exist?")
54 redirect_url = reverse("fb_browse") + query_helper(query, "", "dir")
55 return HttpResponseRedirect(redirect_url)
56 abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
59 results_var = {'results_total': 0, 'results_current': 0, 'delete_total': 0, 'images_total': 0, 'select_total': 0 }
61 for k, v in EXTENSIONS.iteritems():
64 dir_list = os.listdir(abs_path)
66 # print "LISTING FILES: ", dir_list
69 # EXCLUDE FILES MATCHING VERSIONS_PREFIX OR ANY OF THE EXCLUDE PATTERNS
70 filtered = file.startswith('.')
71 for re_prefix in filter_re:
72 if re_prefix.search(file):
76 results_var['results_total'] += 1
79 fileobject = FileObject(os.path.join(smart_str(DIRECTORY), smart_str(path), smart_str(file)))
83 if fileobject.filetype == request.GET.get('filter_type', fileobject.filetype) and _get_filterdate(request.GET.get('filter_date', ''), fileobject.date):
85 if request.GET.get('q') and not re.compile(request.GET.get('q').lower(), re.M).search(file.lower()):
90 files.append(fileobject)
91 results_var['results_current'] += 1
93 if fileobject.filetype == 'Image':
94 results_var['images_total'] += 1
95 if fileobject.filetype != 'Folder':
96 results_var['delete_total'] += 1
97 elif fileobject.filetype == 'Folder' and fileobject.is_empty:
98 results_var['delete_total'] += 1
99 if query.get('type') and query.get('type') in SELECT_FORMATS and fileobject.filetype in SELECT_FORMATS[query.get('type')]:
100 results_var['select_total'] += 1
101 elif not query.get('type'):
102 results_var['select_total'] += 1
105 if fileobject.filetype:
106 counter[fileobject.filetype] += 1
109 files.sort(key = lambda e: getattr(e, request.GET.get('o', DEFAULT_ORDER)))
110 if request.GET.get('ot') == "desc":
113 return render_to_response('filebrowser/index.html', {
116 'results_var': results_var,
119 'title': _(u'FileBrowser'),
120 'settings_var': _get_settings_var(),
121 'breadcrumbs': _get_breadcrumbs(query, path, ''),
122 }, context_instance = Context(request))
123 browse = staff_member_required(never_cache(browse))
127 filebrowser_pre_createdir = Signal(providing_args = ["path", "dirname"])
128 filebrowser_post_createdir = Signal(providing_args = ["path", "dirname"])
135 from filebrowser.forms import MakeDirForm
139 path = _get_path(query.get('dir', ''))
141 msg = _('Directory/File does not exist.')
142 request.user.message_set.create(message = msg)
143 return HttpResponseRedirect(reverse("fb_browse"))
144 abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
146 if request.method == 'POST':
147 form = MakeDirForm(abs_path, request.POST)
149 server_path = os.path.join(abs_path, form.cleaned_data['dir_name'])
152 filebrowser_pre_createdir.send(sender = request, path = path, dirname = form.cleaned_data['dir_name'])
154 os.mkdir(server_path)
155 os.chmod(server_path, 0775)
157 filebrowser_post_createdir.send(sender = request, path = path, dirname = form.cleaned_data['dir_name'])
159 msg = _('The Folder %s was successfully created.') % (form.cleaned_data['dir_name'])
160 request.user.message_set.create(message = msg)
161 # on redirect, sort by date desc to see the new directory on top of the list
162 # remove filter in order to actually _see_ the new folder
163 redirect_url = reverse("fb_browse") + query_helper(query, "ot=desc,o=date", "ot,o,filter_type,filter_date,q")
164 return HttpResponseRedirect(redirect_url)
165 except OSError, (errno, strerror):
167 form.errors['dir_name'] = forms.util.ErrorList([_('Permission denied.')])
169 form.errors['dir_name'] = forms.util.ErrorList([_('Error creating directory.')])
171 form = MakeDirForm(abs_path)
173 return render_to_response('filebrowser/makedir.html', {
176 'title': _(u'New Folder'),
177 'settings_var': _get_settings_var(),
178 'breadcrumbs': _get_breadcrumbs(query, path, _(u'New Folder')),
179 }, context_instance = Context(request))
180 mkdir = staff_member_required(never_cache(mkdir))
188 from django.http import parse_cookie
192 path = _get_path(query.get('dir', ''))
194 msg = _('Directory/File does not exist.')
195 request.user.message_set.create(message = msg)
196 return HttpResponseRedirect(reverse("fb_browse"))
197 abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
199 # SESSION (used for flash-uploading)
200 cookie_dict = parse_cookie(request.META.get('HTTP_COOKIE', ''))
201 engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])
202 session_key = cookie_dict.get(settings.SESSION_COOKIE_NAME, None)
204 return render_to_response('filebrowser/upload.html', {
206 'title': _(u'Select files to upload'),
207 'settings_var': _get_settings_var(),
208 'breadcrumbs': _get_breadcrumbs(query, path, _(u'Upload')),
209 'session_key': session_key,
210 }, context_instance = Context(request))
211 upload = staff_member_required(never_cache(upload))
214 def _check_file(request):
216 Check if file already exists on the server.
219 from django.utils import simplejson
221 folder = request.POST.get('folder')
222 fb_uploadurl_re = re.compile(r'^(%s)' % reverse("fb_upload"))
223 folder = fb_uploadurl_re.sub('', folder)
226 if request.method == 'POST':
227 for k, v in request.POST.items():
229 v = _convert_filename(v)
230 if os.path.isfile(os.path.join(MEDIA_ROOT, DIRECTORY, folder, v)):
233 return HttpResponse(simplejson.dumps(fileArray))
237 filebrowser_pre_upload = Signal(providing_args = ["path", "file"])
238 filebrowser_post_upload = Signal(providing_args = ["path", "file"])
240 def _upload_file(request):
242 Upload file to the server.
245 from django.core.files.move import file_move_safe
247 if request.method == 'POST':
248 folder = request.POST.get('folder')
249 fb_uploadurl_re = re.compile(r'^(%s)' % reverse("fb_upload"))
250 folder = fb_uploadurl_re.sub('', folder)
251 abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, folder)
253 filedata = request.FILES['Filedata']
254 filedata.name = _convert_filename(filedata.name)
256 filebrowser_pre_upload.send(sender = request, path = request.POST.get('folder'), file = filedata)
258 uploadedfile = _handle_file_upload(abs_path, filedata)
260 # if file already exists
261 if os.path.isfile(os.path.join(MEDIA_ROOT, DIRECTORY, folder, filedata.name)):
262 old_file = os.path.join(abs_path, filedata.name)
263 new_file = os.path.join(abs_path, uploadedfile)
264 file_move_safe(new_file, old_file)
266 filebrowser_post_upload.send(sender = request, path = request.POST.get('folder'), file = FileObject(os.path.join(DIRECTORY, folder, filedata.name)))
267 return HttpResponse('True')
268 _upload_file = flash_login_required(_upload_file)
272 filebrowser_pre_delete = Signal(providing_args = ["path", "filename"])
273 filebrowser_post_delete = Signal(providing_args = ["path", "filename"])
277 Delete existing File/Directory.
279 When trying to delete a Directory, the Directory has to be empty.
284 path = _get_path(query.get('dir', ''))
285 filename = _get_file(query.get('dir', ''), query.get('filename', ''))
286 if path is None or filename is None:
287 msg = _('Directory/File does not exist.')
288 request.user.message_set.create(message = msg)
289 return HttpResponseRedirect(reverse("fb_browse"))
290 abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
294 if request.GET.get('filetype') != "Folder":
295 relative_server_path = os.path.join(DIRECTORY, path, filename)
298 filebrowser_pre_delete.send(sender = request, path = path, filename = filename)
299 # DELETE IMAGE VERSIONS/THUMBNAILS
300 for version in VERSIONS:
302 os.unlink(os.path.join(MEDIA_ROOT, _get_version_path(relative_server_path, version)))
306 os.unlink(os.path.join(abs_path, filename))
308 filebrowser_post_delete.send(sender = request, path = path, filename = filename)
310 msg = _('The file %s was successfully deleted.') % (filename.lower())
311 request.user.message_set.create(message = msg)
312 redirect_url = reverse("fb_browse") + query_helper(query, "", "filename,filetype")
313 return HttpResponseRedirect(redirect_url)
315 # todo: define error message
320 filebrowser_pre_delete.send(sender = request, path = path, filename = filename)
322 os.rmdir(os.path.join(abs_path, filename))
324 filebrowser_post_delete.send(sender = request, path = path, filename = filename)
326 msg = _('The directory %s was successfully deleted.') % (filename.lower())
327 request.user.message_set.create(message = msg)
328 redirect_url = reverse("fb_browse") + query_helper(query, "", "filename,filetype")
329 return HttpResponseRedirect(redirect_url)
331 # todo: define error message
335 request.user.message_set.create(message = msg)
337 return render_to_response('filebrowser/index.html', {
339 'file': request.GET.get('filename', ''),
341 'settings_var': _get_settings_var(),
342 'breadcrumbs': _get_breadcrumbs(query, dir_name, ''),
343 }, context_instance = Context(request))
344 delete = staff_member_required(never_cache(delete))
348 filebrowser_pre_rename = Signal(providing_args = ["path", "filename"])
349 filebrowser_post_rename = Signal(providing_args = ["path", "filename"])
353 Rename existing File/Directory.
355 Includes renaming existing Image Versions/Thumbnails.
358 from filebrowser.forms import RenameForm
362 path = _get_path(query.get('dir', ''))
363 filename = _get_file(query.get('dir', ''), query.get('filename', ''))
364 if path is None or filename is None:
365 msg = _('Directory/File does not exist.')
366 request.user.message_set.create(message = msg)
367 return HttpResponseRedirect(reverse("fb_browse"))
368 abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
369 file_extension = os.path.splitext(filename)[1].lower()
371 if request.method == 'POST':
372 form = RenameForm(abs_path, file_extension, request.POST)
374 relative_server_path = os.path.join(DIRECTORY, path, filename)
375 new_relative_server_path = os.path.join(DIRECTORY, path, form.cleaned_data['name'] + file_extension)
378 filebrowser_pre_delete.send(sender = request, path = path, filename = filename)
379 # DELETE IMAGE VERSIONS/THUMBNAILS
380 # regenerating versions/thumbs will be done automatically
381 for version in VERSIONS:
383 os.unlink(os.path.join(MEDIA_ROOT, _get_version_path(relative_server_path, version)))
387 os.rename(os.path.join(MEDIA_ROOT, relative_server_path), os.path.join(MEDIA_ROOT, new_relative_server_path))
389 filebrowser_post_delete.send(sender = request, path = path, filename = filename)
391 msg = _('Renaming was successful.')
392 request.user.message_set.create(message = msg)
393 redirect_url = reverse("fb_browse") + query_helper(query, "", "filename")
394 return HttpResponseRedirect(redirect_url)
395 except OSError, (errno, strerror):
396 form.errors['name'] = forms.util.ErrorList([_('Error.')])
398 form = RenameForm(abs_path, file_extension)
400 return render_to_response('filebrowser/rename.html', {
403 'file_extension': file_extension,
404 'title': _(u'Rename "%s"') % filename,
405 'settings_var': _get_settings_var(),
406 'breadcrumbs': _get_breadcrumbs(query, path, _(u'Rename')),
407 }, context_instance = Context(request))
408 rename = staff_member_required(never_cache(rename))
411 def versions(request):
413 Show all Versions for an Image according to ADMIN_VERSIONS.
418 path = _get_path(query.get('dir', ''))
419 filename = _get_file(query.get('dir', ''), query.get('filename', ''))
420 if path is None or filename is None:
421 msg = _('Directory/File does not exist.')
422 request.user.message_set.create(message = msg)
423 return HttpResponseRedirect(reverse("fb_browse"))
424 abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
426 return render_to_response('filebrowser/versions.html', {
427 'original': _path_to_url(os.path.join(DIRECTORY, path, filename)),
429 'title': _(u'Versions for "%s"') % filename,
430 'settings_var': _get_settings_var(),
431 'breadcrumbs': _get_breadcrumbs(query, path, _(u'Versions for "%s"') % filename),
432 }, context_instance = Context(request))
433 versions = staff_member_required(never_cache(versions))