From 3f65d123faa3a1fcd0b93921e5d42fe659b79fa7 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon
Date: Wed, 12 Aug 2020 21:33:07 +0200
Subject: [PATCH 6/9] When a file a detected as a binary file, return the raw
file
Basically, with the code until now, if an user was trying to view
a binary file in the UI, it was then prompted to download the file
(as pagure doesn't know how to display binary file), but the file
the users were downloading wasn't the raw file but rather the html
page that would contain the file.
Not ideal.
So with this commit we're now just saying: if the file is binary and
we can't render it, just return the raw file to the user and let
them deal with it.
Fixes https://pagure.io/pagure/issue/4907
Signed-off-by: Pierre-Yves Chibon
---
pagure/ui/repo.py | 8 +++-
tests/test_pagure_flask_ui_fork.py | 18 ++-------
tests/test_pagure_flask_ui_repo.py | 25 ++++---------
tests/test_pagure_flask_ui_repo_view_file.py | 39 +++++++++-----------
4 files changed, 35 insertions(+), 55 deletions(-)
diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py
index b6aacc8e..06afb71d 100644
--- a/pagure/ui/repo.py
+++ b/pagure/ui/repo.py
@@ -639,7 +639,13 @@ def view_file(repo, identifier, filename, username=None, namespace=None):
output_type = "tree"
if output_type == "binary":
- headers[str("Content-Disposition")] = "attachment"
+ return view_raw_file(
+ repo,
+ identifier,
+ filename=filename,
+ username=username,
+ namespace=namespace,
+ )
return flask.Response(
flask.stream_with_context(
diff --git a/tests/test_pagure_flask_ui_fork.py b/tests/test_pagure_flask_ui_fork.py
index 8e8dce1c..1bff134f 100644
--- a/tests/test_pagure_flask_ui_fork.py
+++ b/tests/test_pagure_flask_ui_fork.py
@@ -4676,10 +4676,7 @@ More information
# Check fork-edit doesn't show for binary files
output = self.app.get("/test/blob/master/f/test.jpg")
self.assertEqual(output.status_code, 200)
- self.assertNotIn(
- "Fork and Edit\n \n",
- output.get_data(as_text=True),
- )
+ self.assertNotIn(b"
)
self.assertEqual(output.status_code, 400)
self.assertIn(
- "Cannot edit binary files
",
- output.get_data(as_text=True),
+ b"Cannot edit binary files
", output.data,
)
# Check fork-edit shows when user is not logged in
@@ -4764,10 +4760,7 @@ More information
# Check fork-edit doesn't show for binary
output = self.app.get("/test/blob/master/f/test.jpg")
self.assertEqual(output.status_code, 200)
- self.assertNotIn(
- "Edit in your fork\n \n",
- output.get_data(as_text=True),
- )
+ self.assertNotIn(b"
"/somenamespace/test3/blob/master/f/test.jpg"
)
self.assertEqual(output.status_code, 200)
- self.assertNotIn(
- "Fork and Edit\n \n",
- output.get_data(as_text=True),
- )
+ self.assertNotIn(b"", output_text)
- self.assertIn(
- 'view the raw version',
- output_text,
- )
+ self.assertNotIn(b"", output_text)
- self.assertIn('/f/test.jpg">view the raw version', output_text)
+ self.assertNotIn(b"", output_text)
- self.assertIn('/f/test.jpg">view the raw version', output_text)
+ self.assertNotIn(b"view the raw version', output_text)
- self.assertIn("Binary files cannot be rendered.
", output_text)
+ self.assertNotIn(b"
", output_text)
+ self.assertEqual("foo\n bar", output_text)
def test_view_raw_file(self):
""" Test the view_raw_file endpoint. """
diff --git a/tests/test_pagure_flask_ui_repo_view_file.py b/tests/test_pagure_flask_ui_repo_view_file.py
index 16a1ce18..effa06b6 100644
--- a/tests/test_pagure_flask_ui_repo_view_file.py
+++ b/tests/test_pagure_flask_ui_repo_view_file.py
@@ -135,12 +135,7 @@ class PagureFlaskRepoViewFiletests(LocalBasetests):
# View what's supposed to be an image
output = self.app.get("/test/blob/master/f/test.jpg")
self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn("Binary files cannot be rendered.
", output_text)
- self.assertIn(
- 'view the raw version',
- output_text,
- )
+ self.assertNotIn(b"", output_text)
- self.assertIn('/f/test.jpg">view the raw version', output_text)
+ self.assertNotIn(b"