summaryrefslogtreecommitdiffstats
path: root/g2h/file_page.py
diff options
context:
space:
mode:
Diffstat (limited to 'g2h/file_page.py')
-rw-r--r--g2h/file_page.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/g2h/file_page.py b/g2h/file_page.py
new file mode 100644
index 0000000..b154ee5
--- /dev/null
+++ b/g2h/file_page.py
@@ -0,0 +1,45 @@
+import base64
+import filetype
+import os
+
+from .file import sanitize_file_name, tree_join
+from .page import Page
+from .tree import safe_join
+
+def bytes_to_content(file_bytes):
+ if not file_bytes:
+ return (None, 'empty')
+ try:
+ text = file_bytes.decode('utf-8')
+ return (text, 'text')
+ except UnicodeError:
+ if filetype.is_image(file_bytes):
+ # TODO: Refer to the static file once that's implemented.
+ img = base64.b64encode(file_bytes)
+ return (img.decode('utf-8'), 'image')
+
+ return (None, None)
+
+class FilePage(Page):
+ def __init__(self, out_dir, name, commit, file_name, file_path, content_subdir):
+ tree = safe_join(commit.tree, file_path)
+ super(FilePage, self).__init__('file.md', out_dir, os.path.join(tree_join(commit, file_path), sanitize_file_name(file_name, tree.blobs) + '.md'), name, content_subdir)
+ self.commit = commit
+ self.file_name = file_name
+ self.file_path = file_path
+ self.tree = tree
+
+ def read_file(self):
+ return self.tree.join(self.file_name).data_stream.read()
+
+ def context(self):
+ file_bytes = self.read_file()
+ content, file_type = bytes_to_content(file_bytes)
+
+ return {
+ 'content': content,
+ 'content_subdir': self.content_subdir,
+ 'file_extension': os.path.splitext(self.file_name)[1].lstrip('.'),
+ 'title': self.name + ': ' + self.file_name,
+ 'type': file_type
+ }