def index(request, dir_name=None): """ Show list of files on a server-directory. """ path = _get_path(dir_name) query = _get_query(request.GET) # INITIAL VARIABLES results_var = {'results_total': 0, 'results_current': 0, 'delete_total': 0, 'change_total': 0, 'imagegenerator_total': 0 } counter = {} for k,v in EXTENSIONS.iteritems(): counter[k] = 0 dir_list = os.listdir(os.path.join(PATH_SERVER, path)) file_list = [] for file in dir_list: # VARIABLES var_filesize_long = '' # filesize var_filesize_str = '' # filesize in B, kB, MB var_date = '' # YYYY-MM-dd var_path_thumb = '' # path to thumbnail var_link = '' # link to file (using URL_WWW), link to folder (using URL_ADMIN) var_select_link = '' # link to file (using URL_WWW) var_save_path = '' # Path to file relative to MEDIA_ROOT var_file_extension = '' # see EXTENSIONS in fb_settings.py var_file_type = '' # Folder, Image, Video, Document, Sound, Code, ... var_image_dimensions = '' # Image Dimensions (width, height) var_thumb_dimensions = '' # Thumbnail Dimensions (width, height) var_flag_makethumb = False # True, if Image has no Thumbnail. var_flag_deletedir = False # True, if Directory is empty. var_image_version = False # True, if Image is generated with ImageGenerator. # DON'T DISPLAY FILES STARTING WITH %THUMB_PREFIX% OR "." if re.compile(THUMB_PREFIX, re.M).search(file) or \ file.startswith('.'): # ... or with a '.' \ continue else: results_var['results_total'] += 1 # SIZE var_filesize_long = os.path.getsize(os.path.join(PATH_SERVER, path, file)) var_filesize_str = _get_filesize(var_filesize_long) # DATE / TIME date_time = os.path.getmtime(os.path.join(PATH_SERVER, path, file)) var_date = strftime("%Y-%m-%d", gmtime(date_time)) # EXTENSION / FLAG_EMPTYDIR / DELETE_TOTAL fn = os.path.join(PATH_SERVER, path, file) var_select_link = var_link = "%s%s%s" % (URL_WWW, path, file) if os.path.isfile(fn): # file var_file_extension = os.path.splitext(file)[1].lower() elif os.path.isdir(fn): # folder var_link = "%s%s%s" % (URL_ADMIN, path, file) if not os.listdir(os.path.join(PATH_SERVER, path, file)): var_flag_deletedir = True # only empty directories are allowed to be deleted # DETERMINE MEDIA SAVE PATH var_save_path = var_select_link if not SAVE_FULL_URL: var_save_path = var_save_path.replace(settings.MEDIA_URL, '').lstrip('/') # FILETYPE / COUNTER var_file_type = _get_file_type(file) if var_file_type: counter[var_file_type] += 1 # DIMENSIONS / MAKETHUMB / SELECT if var_file_type == 'Image': try: im = Image.open(os.path.join(PATH_SERVER, path, file)) var_image_dimensions = im.size var_path_thumb = "%s%s%s%s" % (URL_WWW, path, THUMB_PREFIX, file) try: thumb = Image.open(os.path.join(PATH_SERVER, path, THUMB_PREFIX + file)) var_thumb_dimensions = thumb.size except: # if thumbnail does not exist, show makethumb-Icon instead. var_path_thumb = settings.URL_FILEBROWSER_MEDIA + 'img/filebrowser_Thumb.gif' var_flag_makethumb = True except: # if image is corrupt, change filetype to not defined var_file_type = '' # check, if image is generated with ImageGenerator var_image_version = _is_image_version(file) if var_image_version == False: results_var['imagegenerator_total'] += 1 # FILTER / SEARCH flag_extend = False if query['filter_type'] != '' and query['filter_date'] != '' and var_file_type == query['filter_type'] and _get_filterdate(query['filter_date'], date_time): flag_extend = True elif query['filter_type'] != '' and query['filter_date'] == '' and var_file_type == query['filter_type']: flag_extend = True elif query['filter_type'] == '' and query['filter_date'] != '' and _get_filterdate(query['filter_date'], date_time): flag_extend = True elif query['filter_type'] == '' and query['filter_date'] == '': flag_extend = True if query['q'] and not re.compile(query['q'].lower(), re.M).search(file.lower()): flag_extend = False # APPEND FILE_LIST if flag_extend == True: file_list.append([file, var_filesize_long, var_filesize_str, var_date, var_path_thumb, var_link, var_select_link, var_save_path, var_file_extension, var_file_type, var_image_dimensions, var_thumb_dimensions, file.lower(), var_flag_makethumb, var_flag_deletedir, var_image_version]) # SORT LIST file_list.sort(lambda x, y: cmp(x[int(query['o'])], y[int(query['o'])])) if query['ot'] == "desc": file_list.reverse() # MAKE DICTIONARY (for better readability in the templates) file_dict = _make_filedict(file_list) # RESULTS results_var['results_current'] = len(file_list) for file in file_dict: if file['file_type'] == 'Image': results_var['change_total'] += 1 if file['file_type'] != 'Folder': results_var['delete_total'] += 1 elif file['file_type'] == 'Folder' and file['flag_deletedir'] == True: results_var['delete_total'] += 1 return render_to_response('filebrowser/index.html', { 'dir': dir_name, 'file_dict': file_dict, 'results_var': results_var, 'query': query, 'counter': counter, 'settings_var': _get_settings_var(request.META['HTTP_HOST'], path), 'breadcrumbs': _get_breadcrumbs(_get_query(request.GET), dir_name, ''), 'title': _(u'FileBrowser'), 'root_path': URL_HOME, }, context_instance=Context(request))
var_flag_makethumb = False # True, if Image has no Thumbnail. var_flag_deletedir = False # True, if Directory is empty. var_image_version = False # True, if Image is generated with ImageGenerator. # EXCLUDE FILES MATCHING THUMB_PREFIX OR ANY OF THE EXCLUDE PATTERNS filtered = file.startswith('.') for re_prefix in filter_re: if re_prefix.search(file): filtered = True if filtered: continue results_var['results_total'] += 1 # SIZE var_filesize_long = os.path.getsize(os.path.join(PATH_SERVER, path, file)) var_filesize_str = _get_filesize(var_filesize_long) # DATE / TIME date_time = os.path.getmtime(os.path.join(PATH_SERVER, path, file)) var_date = strftime("%Y-%m-%d", gmtime(date_time)) # EXTENSION / FLAG_EMPTYDIR / DELETE_TOTAL fn = os.path.join(PATH_SERVER, path, file) var_select_link = var_link = "%s%s%s" % (URL_WWW, path, file) if os.path.isfile(fn): # file var_file_extension = os.path.splitext(file)[1].lower() elif os.path.isdir(fn): # folder var_link = "%s%s%s" % (URL_ADMIN, path, file) if not os.listdir(os.path.join(PATH_SERVER, path, file)): var_flag_deletedir = True # only empty directories are allowed to be deleted
def index(request, dir_name=None): """ Show list of files on a server-directory. """ path = _get_path(dir_name) query = _get_query(request.GET) # INITIAL VARIABLES results_var = {'results_total': 0, 'results_current': 0, 'delete_total': 0, 'change_total': 0, 'imagegenerator_total': 0 } counter = {} for k,v in EXTENSIONS.iteritems(): counter[k] = 0 dir_list = os.listdir(os.path.join(PATH_SERVER, path)) file_list = [] for file in dir_list: # VARIABLES var_filesize_long = '' # filesize var_filesize_str = '' # filesize in B, kB, MB var_date = '' # YYYY-MM-dd var_path_thumb = '' # path to thumbnail var_link = '' # link to file (using URL_WWW), link to folder (using URL_ADMIN) var_select_link = '' # link to file (using URL_WWW) var_file_extension = '' # see EXTENSIONS in fb_settings.py var_file_type = '' # Folder, Image, Video, Document, Sound, Code, ... var_image_dimensions = '' # Image Dimensions (width, height) var_thumb_dimensions = '' # Thumbnail Dimensions (width, height) var_flag_makethumb = False # True, if Image has no Thumbnail. var_flag_deletedir = False # True, if Directory is empty. var_image_version = False # True, if Image is generated with ImageGenerator. # DON'T DISPLAY FILES STARTING WITH %THUMB_PREFIX% OR "." if re.compile(THUMB_PREFIX, re.M).search(file) or \ file.startswith('.'): # ... or with a '.' \ continue else: results_var['results_total'] += 1 # SIZE var_filesize_long = os.path.getsize(os.path.join(PATH_SERVER, path, file)) var_filesize_str = _get_filesize(var_filesize_long) # DATE / TIME date_time = os.path.getmtime(os.path.join(PATH_SERVER, path, file)) var_date = strftime("%Y-%m-%d", gmtime(date_time)) # EXTENSION / FLAG_EMPTYDIR / DELETE_TOTAL if os.path.isfile(os.path.join(PATH_SERVER, path, file)): # file var_file_extension = os.path.splitext(file)[1].lower() var_select_link = var_link = "%s%s%s" % (URL_WWW, path, file) elif os.path.isdir(os.path.join(PATH_SERVER, path, file)): # folder var_link = "%s%s%s" % (URL_ADMIN, path, file) var_select_link = "%s%s%s/" % (URL_WWW, path, file) if not os.listdir(os.path.join(PATH_SERVER, path, file)): var_flag_deletedir = True # only empty directories are allowed to be deleted # FILETYPE / COUNTER var_file_type = _get_file_type(file) if var_file_type: counter[var_file_type] += 1 # DIMENSIONS / MAKETHUMB / SELECT if var_file_type == 'Image': try: im = Image.open(os.path.join(PATH_SERVER, path, file)) var_image_dimensions = im.size var_path_thumb = "%s%s%s%s" % (URL_WWW, path, THUMB_PREFIX, file) try: thumb = Image.open(os.path.join(PATH_SERVER, path, THUMB_PREFIX + file)) var_thumb_dimensions = thumb.size except: # if thumbnail does not exist, show makethumb-Icon instead. var_path_thumb = settings.ADMIN_MEDIA_PREFIX + 'filebrowser/img/filebrowser_Thumb.gif' var_flag_makethumb = True except: # if image is corrupt, change filetype to not defined var_file_type = '' # check, if image is generated with ImageGenerator var_image_version = _is_image_version(file) if var_image_version == False: results_var['imagegenerator_total'] += 1 # FILTER / SEARCH flag_extend = False if query['filter_type'] != '' and query['filter_date'] != '' and file_type == query['filter_type'] and _get_filterdate(query['filter_date'], date_time): flag_extend = True elif query['filter_type'] != '' and query['filter_date'] == '' and var_file_type == query['filter_type']: flag_extend = True elif query['filter_type'] == '' and query['filter_date'] != '' and _get_filterdate(query['filter_date'], date_time): flag_extend = True elif query['filter_type'] == '' and query['filter_date'] == '': flag_extend = True if query['q'] and not re.compile(query['q'].lower(), re.M).search(file.lower()): flag_extend = False # APPEND FILE_LIST if flag_extend == True: file_list.append([file, var_filesize_long, var_filesize_str, var_date, var_path_thumb, var_link, var_select_link, var_file_extension, var_file_type, var_image_dimensions, var_thumb_dimensions, file.lower(), var_flag_makethumb, var_flag_deletedir, var_image_version]) # SORT LIST file_list.sort(lambda x, y: cmp(x[int(query['o'])], y[int(query['o'])])) if query['ot'] == "desc": file_list.reverse() # MAKE DICTIONARY (for better readability in the templates) file_dict = _make_filedict(file_list) # RESULTS results_var['results_current'] = len(file_list) for file in file_dict: if file['file_type'] == 'Image': results_var['change_total'] += 1 if file['file_type'] != 'Folder': results_var['delete_total'] += 1 elif file['file_type'] == 'Folder' and file['flag_deletedir'] == True: results_var['delete_total'] += 1 return render_to_response('filebrowser/index.html', { 'dir': dir_name, 'file_dict': file_dict, 'results_var': results_var, 'query': query, 'counter': counter, 'settings_var': _get_settings_var(request.META['HTTP_HOST'], path), 'breadcrumbs': _get_breadcrumbs(_get_query(request.GET), dir_name, ''), 'title': _('FileBrowser'), }, context_instance=Context(request))