changeset 1189:a4e1b955fe07 beta

fixes for rawfile, annotation, download. It will check now if it's a filenode. Thank You googlebot :)
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 30 Mar 2011 12:59:49 +0200
parents 01f37a734fdf
children 0d7a127e6449
files rhodecode/controllers/files.py
diffstat 1 files changed, 31 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/controllers/files.py	Tue Mar 29 00:11:18 2011 +0200
+++ b/rhodecode/controllers/files.py	Wed Mar 30 12:59:49 2011 +0200
@@ -74,6 +74,29 @@
             h.flash(str(e), category='warning')
             redirect(h.url('files_home', repo_name=repo_name, revision='tip'))
 
+
+    def __get_filenode_or_redirect(self, repo_name, cs, path):
+        """
+        Returns file_node, if error occurs or given path is directory,
+        it'll redirect to top level path
+        
+        :param repo_name: repo_name
+        :param cs: given changeset
+        :param path: path to lookup
+        """
+
+
+        try:
+            file_node = cs.get_node(path)
+            if file_node.is_dir():
+                raise RepositoryError('given path is a directory')
+        except RepositoryError, e:
+            h.flash(str(e), category='warning')
+            redirect(h.url('files_home', repo_name=repo_name,
+                           revision=cs.raw_id))
+
+        return file_node
+
     def index(self, repo_name, revision, f_path):
         #reditect to given revision from form if given
         post_revision = request.POST.get('at_rev', None)
@@ -109,7 +132,7 @@
         except (ChangesetDoesNotExistError, VCSError):
             c.url_next = '#'
 
-        #files
+        #files or dirs
         try:
             c.files_list = c.changeset.get_node(f_path)
             c.file_history = self._get_history(c.rhodecode_repo,
@@ -124,39 +147,24 @@
 
     def rawfile(self, repo_name, revision, f_path):
         cs = self.__get_cs_or_redirect(revision, repo_name)
-        try:
-            file_node = cs.get_node(f_path)
-        except RepositoryError, e:
-            h.flash(str(e), category='warning')
-            redirect(h.url('files_home', repo_name=repo_name,
-                           revision=cs.raw_id))
+        file_node = self.__get_filenode_or_redirect(repo_name, cs, f_path)
 
-        fname = f_path.split('/')[-1].encode('utf8', 'replace')
+        response.content_disposition = 'attachment; filename=%s' % \
+            f_path.split('/')[-1].encode('utf8', 'replace')
+
         response.content_type = file_node.mimetype
-        response.content_disposition = 'attachment; filename=%s' % fname
         return file_node.content
 
     def raw(self, repo_name, revision, f_path):
         cs = self.__get_cs_or_redirect(revision, repo_name)
-        try:
-            file_node = cs.get_node(f_path)
-        except RepositoryError, e:
-            h.flash(str(e), category='warning')
-            redirect(h.url('files_home', repo_name=repo_name,
-                           revision=cs.raw_id))
+        file_node = self.__get_filenode_or_redirect(repo_name, cs, f_path)
 
         response.content_type = 'text/plain'
-
         return file_node.content
 
     def annotate(self, repo_name, revision, f_path):
         cs = self.__get_cs_or_redirect(revision, repo_name)
-        try:
-            c.file = cs.get_node(f_path)
-        except RepositoryError, e:
-            h.flash(str(e), category='warning')
-            redirect(h.url('files_home', repo_name=repo_name,
-                           revision=cs.raw_id))
+        c.file = self.__get_filenode_or_redirect(repo_name, cs, f_path)
 
         c.file_history = self._get_history(c.rhodecode_repo,
                                            c.file, f_path)
@@ -268,7 +276,7 @@
         return render('files/file_diff.html')
 
     def _get_history(self, repo, node, f_path):
-        if not node.kind is NodeKind.FILE:
+        if not node.is_file():
             return []
         changesets = node.history
         hist_l = []