def ImagesView(request, id): """ Gallery view template :template:`iviewer/images.html` """ if request.user.is_anonymous() and not settings.LIMITED_ANONYMOUS: return HttpResponseRedirect('%s?next=%s' % (settings.LOGIN_URL, request.path)) lib_id = int(id) path = request.GET.get('p', '') if FilePath.check(path, norm=True) == False: logger.error(u"Files. Path check fail. home_id:{0}, path:{1}".format( lib_id, path)) return RenderError(request, u"IOError, Permission denied") try: home = get_home(request.user, lib_id) patharr = split_path(path) File = home.lib.getStorage() files = [] for item in File.listdir(path): tmp = item["name"].lower() if tmp.endswith(".jpg") or tmp.endswith(".jpeg"): files.append(item) small_image = ResizeOptions(settings.LVIEWER_SMALL_IMAGE) big_image = ResizeOptions(settings.LVIEWER_BIG_IMAGE) except ObjectDoesNotExist: logger.error( u"Files. No such file lib or you don't have permissions. home_id:{0}, path:{1}" .format(lib_id, path)) return RenderError(request, u"No such file lib or you don't have permissions") except FileError as e: logger.error(u"Files. {0}. home_id:{1}, path:{2}".format( e, lib_id, path)) return RenderError(request, e) return render( request, u"lviewer/images.html", { 'pathname': request.path, 'path': path, 'patharr': patharr, 'home_id': lib_id, 'home': home.lib.name, 'permission': home.permission, 'files': files, 'small_image': small_image, 'big_image': big_image, })
def TrashView( request, id ): """ Trash folder browser, with only move and delete actions and root directory template :template:`limited/trash.html` """ user = request.user if user.is_anonymous( ) and not settings.LIMITED_ANONYMOUS: return HttpResponseRedirect( '%s?next=%s' % (settings.LOGIN_URL, request.path) ) lib_id = int( id ) try: home = get_home(user, lib_id ) history = History.objects.\ select_related( 'user' ).\ only( 'lib', 'type', 'files', 'path', 'extra', 'user__username' ).\ filter( lib=lib_id ).\ order_by( '-id' )[0:5] patharr = split_path( 'Trash' ) File = home.lib.getStorage( ) files = File.trash.listdir( ) rss_token = None if user.is_anonymous() else Profile.objects.get(user=user).rss_token except ObjectDoesNotExist: logger.error( u"Trash. No such file lib or you don't have permissions. home_id:{0}".format( lib_id ) ) return RenderError( request, u"No such file lib or you don't have permissions" ) except FileError as e: logger.error( u"Trash. {0}. home_id:{1}".format( e, lib_id ) ) return RenderError( request, e ) return render( request, u"limited/trash.html", { 'pathname': request.path, 'path': settings.LIMITED_TRASH_PATH, 'patharr': patharr, 'history': history, 'home_id': lib_id, 'home': home.lib.name, 'permission': home.permission, 'files': files, 'rss_token': rss_token, } )
def test_split_path(self): assert split_path('') == [] assert split_path('/root') == [('root', '/root')] assert split_path('/root/path1') == [('root', '/root'), ('path1', '/root/path1')] assert split_path('/root/path1/path2') == [('root', '/root'), ('path1', '/root/path1'), ('path2', '/root/path1/path2')] assert split_path('/root/path/path') == [('root', '/root'), ('path', '/root/path'), ('path', '/root/path/path')] # test rigth order assert split_path('/c/b/a') == [('c', '/c'), ('b', '/c/b'), ('a', '/c/b/a')]
def HistoryView( request, id ): """ Fool history browser template :template:`limited/history.html` """ user = request.user if user.is_anonymous( ) and not settings.LIMITED_ANONYMOUS: return HttpResponseRedirect( '%s?next=%s' % (settings.LOGIN_URL, request.path) ) lib_id = int( id ) try: home = get_home( user, lib_id ) history = History.objects.\ select_related( 'user' ).\ only( 'lib', 'type', 'files', 'path', 'extra', 'time', 'user__username' ).\ filter( lib=lib_id ).\ order_by( '-id' )[0:30] patharr = split_path( u"History" ) rss_token = None if user.is_anonymous() else Profile.objects.get(user=user).rss_token except ObjectDoesNotExist: logger.error( u"History. No such file lib or you don't have permissions. home_id:{0}".format( lib_id ) ) return RenderError( request, u"No such file lib or you don't have permissions" ) return render( request, u"limited/history.html", { 'pathname': request.path, 'patharr': patharr, 'history': list( history ), 'home_id': lib_id, 'home': home.lib.name, 'permission': home.permission, 'rss_token': rss_token, } )
def FilesView( request, id ): """ Main browser and history widget template :template:`limited/browser.html` """ user = request.user if user.is_anonymous( ) and not settings.LIMITED_ANONYMOUS: return HttpResponseRedirect( '%s?next=%s' % (settings.LOGIN_URL, request.path) ) lib_id = int( id ) path = request.GET.get( 'p', '' ) if not FilePath.check(path, norm=True): logger.error( u"Files. Path check fail. home_id:{0}, path:{1}".format( lib_id, path ) ) return RenderError( request, u"IOError, Permission denied" ) try: home = get_home( user, lib_id ) history = History.objects.\ select_related( 'user' ).\ only( 'lib', 'type', 'files', 'path', 'extra', 'user__username' ).\ filter( lib=lib_id ).\ order_by( '-id' )[0:8] patharr = split_path( path ) File = home.lib.getStorage( ) files = File.listdir( path ) # Check if iViewer is enable and there is at least 2 jpg lViewer = False if settings.LIMITED_LVIEWER: images = 0 for file in files: tmp = file["name"].lower( ) if tmp.endswith( ".jpg" ) or tmp.endswith( ".jpeg" ): images += 1 lViewer = images > 1 allowed = {} allowed['only'] = '|'.join( settings.LIMITED_FILES_ALLOWED["ONLY"] ).replace( '\\', '\\\\' ) allowed['except'] = '|'.join( settings.LIMITED_FILES_ALLOWED["EXCEPT"] ).replace( '\\', '\\\\' ) allowed['message'] = settings.LIMITED_FILES_MESSAGE rss_token = None if user.is_anonymous() else Profile.objects.get(user=user).rss_token except ObjectDoesNotExist: logger.error( u"Files. No such file lib or you don't have permissions. home_id:{0}, path:{1}".format( lib_id, path ) ) return RenderError( request, u"No such file lib or you don't have permissions" ) except FileError as e: logger.error( u"Files. {0}. home_id:{1}, path:{2}".format( e, lib_id, path ) ) return RenderError( request, e ) return render( request, u"limited/files.html", { 'pathname': request.path, 'path': path, 'patharr': patharr, 'history': history, 'home_id': lib_id, 'home': home.lib.name, 'permission': home.permission, 'files': files, 'allowed': allowed, 'rss_token': rss_token, 'lviewer': lViewer, } )