Skip to content

Commit 666e8bd

Browse files
committed
Merge pull request CenterForOpenScience#265 from TomBaxter/feature/svcs-86
[SVCS-86] Add Papaya Renderer for Dicom and NifTi Files
2 parents 8bb2dd4 + a575a0e commit 666e8bd

File tree

17 files changed

+2280
-1
lines changed

17 files changed

+2280
-1
lines changed

mfr/core/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from time import time
2+
from os.path import getctime
3+
14
from stevedore import driver
25

36
from mfr.core import exceptions
@@ -106,3 +109,8 @@ def sizeof_fmt(num, suffix='B'):
106109
return '%3.1f%s%s' % (num, unit, suffix)
107110
num /= 1000.0
108111
return '%.1f%s%s' % (num, 'Y', suffix)
112+
113+
def file_expired(path: str, ttl: int) -> bool:
114+
if (time() - getctime(path)) >= ttl:
115+
return True
116+
return False

mfr/extensions/papaya/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .render import PapayaRenderer # noqa

mfr/extensions/papaya/render.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
import shutil
3+
4+
from mako.lookup import TemplateLookup
5+
6+
from mfr.core import utils
7+
from mfr.core import extension
8+
from mfr.extensions import settings as ext_settings
9+
from mfr.extensions.papaya import settings
10+
11+
12+
class PapayaRenderer(extension.BaseRenderer):
13+
14+
data_dir = settings.DATA_DIR
15+
data_ttl = settings.DATA_TTL
16+
comp_ext = ext_settings.COMPRESSED_EXT
17+
18+
TEMPLATE = TemplateLookup(
19+
directories=[
20+
os.path.join(os.path.dirname(__file__), 'templates')
21+
]).get_template('viewer.mako')
22+
23+
def render(self):
24+
self.remove_old_files()
25+
file_name = os.path.basename(self.file_path)
26+
if self.metadata.ext in self.comp_ext.keys():
27+
second_ext = '.{}'.format(self.metadata.name.split('.')[-1])
28+
if second_ext in self.comp_ext[self.metadata.ext]:
29+
file_name = file_name + second_ext
30+
file_name = file_name + self.metadata.ext
31+
shutil.copyfile(self.file_path, self.data_dir + file_name)
32+
return self.TEMPLATE.render(base=self.assets_url, file_name=file_name)
33+
34+
def remove_old_files(self):
35+
36+
for data_file in os.listdir(self.data_dir):
37+
if utils.file_expired(self.data_dir + data_file, self.data_ttl):
38+
os.unlink(self.data_dir + data_file)
39+
40+
@property
41+
def file_required(self):
42+
return True
43+
44+
@property
45+
def cache_result(self):
46+
return True

mfr/extensions/papaya/settings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from mfr import settings
2+
3+
config = settings.child('PAPAYA_EXTENSION_CONFIG')
4+
5+
# Directory to temporarily store papaya image files
6+
DATA_DIR = 'mfr/extensions/papaya/static/data/'
7+
# Files older then this many seconds will be deleted from DATA_DIR at the beggining of each render.
8+
DATA_TTL = 300
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Currently using a custom built papaya.js with option to remove the ability to open new files from the users desktop.
2+
3+
https://github.com/TomBaxter/Papaya/tree/feature/noNewFiles
4+
5+
I have sent them a PR, hopefully they bite.
6+
7+
https://github.com/rii-mango/Papaya/pull/101
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Ignore everything in this directory
2+
*
3+
# Except this file
4+
!.gitignore

mfr/extensions/papaya/static/papaya.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)