|
@@ -1,6 +1,8 @@
|
1
|
1
|
import tg
|
|
2
|
+from tg import expose
|
|
3
|
+from tg import tmpl_context
|
2
|
4
|
from preview_generator.manager import PreviewManager
|
3
|
|
-from tg import expose, tmpl_context
|
|
5
|
+
|
4
|
6
|
from tracim.config.app_cfg import CFG
|
5
|
7
|
from tracim.controllers import TIMRestController
|
6
|
8
|
from tracim.lib.content import ContentApi
|
|
@@ -8,6 +10,7 @@ from tracim.model.data import ContentType
|
8
|
10
|
|
9
|
11
|
__all__ = ['PagesController']
|
10
|
12
|
|
|
13
|
+
|
11
|
14
|
class PagesController(TIMRestController):
|
12
|
15
|
|
13
|
16
|
@expose()
|
|
@@ -20,118 +23,78 @@ class PagesController(TIMRestController):
|
20
|
23
|
return 'all the pages of document {}'.format(file_id)
|
21
|
24
|
|
22
|
25
|
@expose(content_type='image/jpeg')
|
23
|
|
- def get_one(self, page_id: int, revision_id: int=None, *args, **kwargs):
|
|
26
|
+ def get_one(self,
|
|
27
|
+ page_id: str='-1',
|
|
28
|
+ revision_id: str=None,
|
|
29
|
+ size: int=300,
|
|
30
|
+ *args, **kwargs):
|
24
|
31
|
file_id = int(tg.request.controller_state.routing_args.get('file_id'))
|
25
|
|
-
|
26
|
|
- # For now it's done through database content
|
27
|
|
- # but soon it'll be with disk access
|
28
|
|
-
|
|
32
|
+ page = int(page_id)
|
|
33
|
+ revision_id = int(revision_id) if revision_id != 'latest' else None
|
|
34
|
+ cache_path = CFG.get_instance().PREVIEW_CACHE
|
|
35
|
+ preview_manager = PreviewManager(cache_path, create_folder=True)
|
29
|
36
|
user = tmpl_context.current_user
|
30
|
|
- content_api = ContentApi(
|
31
|
|
- user,
|
32
|
|
- show_archived=True,
|
33
|
|
- show_deleted=True,
|
34
|
|
- )
|
|
37
|
+ content_api = ContentApi(user,
|
|
38
|
+ show_archived=True,
|
|
39
|
+ show_deleted=True)
|
35
|
40
|
if revision_id:
|
36
|
41
|
file_path = content_api.get_one_revision_filepath(revision_id)
|
37
|
42
|
else:
|
38
|
43
|
file = content_api.get_one(file_id, self._item_type)
|
39
|
44
|
file_path = content_api.get_one_revision_filepath(file.revision_id)
|
40
|
|
-
|
41
|
|
- cfg = CFG.get_instance()
|
42
|
|
- cache_path = cfg.PREVIEW_CACHE
|
43
|
|
-
|
44
|
|
- preview_manager = PreviewManager(cache_path, create_folder=True)
|
45
|
|
- path = preview_manager.get_jpeg_preview(
|
46
|
|
- file_path=file_path,
|
47
|
|
- page=page_id,
|
48
|
|
- height=500,
|
49
|
|
- width=500
|
50
|
|
- )
|
51
|
|
-
|
|
45
|
+ path = preview_manager.get_jpeg_preview(file_path=file_path,
|
|
46
|
+ page=page,
|
|
47
|
+ height=size,
|
|
48
|
+ width=size)
|
52
|
49
|
with open(path, 'rb') as large:
|
53
|
50
|
return large.read()
|
54
|
51
|
|
55
|
52
|
@expose(content_type='image/jpeg')
|
56
|
|
- def high_quality(self, page_id: int, *args, **kwargs):
|
57
|
|
- file_id = int(tg.request.controller_state.routing_args.get('file_id'))
|
58
|
|
-
|
59
|
|
- # For now it's done through database content
|
60
|
|
- # but soon it'll be with disk access
|
61
|
|
-
|
62
|
|
- user = tmpl_context.current_user
|
63
|
|
- content_api = ContentApi(
|
64
|
|
- user,
|
65
|
|
- show_archived=True,
|
66
|
|
- show_deleted=True,
|
67
|
|
- )
|
68
|
|
- file_name = content_api.get_one(file_id, self._item_type).file_name
|
69
|
|
- cache_path = '/home/alexis/Pictures/cache/'
|
70
|
|
-
|
71
|
|
- preview_manager = PreviewManager(cache_path, create_folder=True)
|
72
|
|
- path = preview_manager.get_jpeg_preview(
|
73
|
|
- file_path='/home/alexis/Pictures/cache/{}'.format(file_name),
|
74
|
|
- page=page_id,
|
75
|
|
- height=5000,
|
76
|
|
- width=5000
|
77
|
|
- )
|
78
|
|
-
|
79
|
|
- with open(path, 'rb') as large:
|
80
|
|
- return large.read()
|
|
53
|
+ def high_quality(self,
|
|
54
|
+ page_id: str='-1',
|
|
55
|
+ revision_id: int=None,
|
|
56
|
+ size: int=1000,
|
|
57
|
+ *args, **kwargs):
|
|
58
|
+ result = self.get_one(page_id=page_id,
|
|
59
|
+ revision_id=revision_id,
|
|
60
|
+ size=size,
|
|
61
|
+ args=args,
|
|
62
|
+ kwargs=kwargs)
|
|
63
|
+ return result
|
81
|
64
|
|
82
|
65
|
@expose(content_type='application/pdf')
|
83
|
|
- def download_pdf_full(self, *args, **kwargs):
|
84
|
|
- file_id = int(tg.request.controller_state.routing_args.get('file_id'))
|
85
|
|
-
|
86
|
|
- # For now it's done through database content
|
87
|
|
- # but soon it'll be with disk access
|
88
|
|
-
|
89
|
|
- user = tmpl_context.current_user
|
90
|
|
- content_api = ContentApi(
|
91
|
|
- user,
|
92
|
|
- show_archived=True,
|
93
|
|
- show_deleted=True,
|
94
|
|
- )
|
95
|
|
- file_name = content_api.get_one(file_id, self._item_type).file_name
|
96
|
|
- cache_path = '/home/alexis/Pictures/cache/'
|
97
|
|
-
|
98
|
|
- preview_manager = PreviewManager(cache_path, create_folder=True)
|
99
|
|
- path = preview_manager.get_pdf_preview(
|
100
|
|
- file_path='/home/alexis/Pictures/cache/{}'.format(file_name),
|
101
|
|
- )
|
102
|
|
-
|
103
|
|
- tg.response.headers['Content-Disposition'] = \
|
104
|
|
- str('attachment; filename="{}"'.format(file_name))
|
105
|
|
- with open(path, 'rb') as pdf:
|
106
|
|
- return pdf.read()
|
|
66
|
+ def download_pdf_full(self,
|
|
67
|
+ page_id: str,
|
|
68
|
+ revision_id: str='-1',
|
|
69
|
+ *args, **kwargs):
|
|
70
|
+ return self.download_pdf_one(page_id='-1',
|
|
71
|
+ revision_id=revision_id,
|
|
72
|
+ args=args, kwargs=kwargs)
|
107
|
73
|
|
108
|
74
|
@expose(content_type='application/pdf')
|
109
|
|
- def download_pdf_one(self, page_id: int, *args, **kwargs):
|
|
75
|
+ def download_pdf_one(self,
|
|
76
|
+ page_id: str,
|
|
77
|
+ revision_id: str=None,
|
|
78
|
+ *args, **kwargs):
|
110
|
79
|
file_id = int(tg.request.controller_state.routing_args.get('file_id'))
|
111
|
|
- page_id = int(page_id)
|
112
|
|
- # page_id = int(tg.request.controller_state.routing_args.get('page_id'))
|
113
|
|
-
|
114
|
|
- # For now it's done through database content
|
115
|
|
- # but soon it'll be with disk access
|
116
|
|
-
|
117
|
|
- user = tmpl_context.current_user
|
118
|
|
- content_api = ContentApi(
|
119
|
|
- user,
|
120
|
|
- show_archived=True,
|
121
|
|
- show_deleted=True,
|
122
|
|
- )
|
123
|
|
- file_name = content_api.get_one(file_id, self._item_type).file_name
|
124
|
|
-
|
125
|
|
- cache_path = '/home/alexis/Pictures/cache/'
|
126
|
|
-
|
|
80
|
+ revision_id = int(revision_id) if revision_id != 'latest' else None
|
|
81
|
+ page = int(page_id)
|
|
82
|
+ cache_path = CFG.get_instance().PREVIEW_CACHE
|
127
|
83
|
preview_manager = PreviewManager(cache_path, create_folder=True)
|
128
|
|
- path = preview_manager.get_pdf_preview(
|
129
|
|
- file_path='/home/alexis/Pictures/cache/{}'.format(file_name),
|
130
|
|
- page=page_id,
|
131
|
|
- )
|
132
|
|
-
|
|
84
|
+ user = tmpl_context.current_user
|
|
85
|
+ content_api = ContentApi(user,
|
|
86
|
+ show_archived=True,
|
|
87
|
+ show_deleted=True)
|
|
88
|
+ file = content_api.get_one(file_id, self._item_type)
|
|
89
|
+ if revision_id:
|
|
90
|
+ file_path = content_api.get_one_revision_filepath(revision_id)
|
|
91
|
+ else:
|
|
92
|
+ file = content_api.get_one(file_id, self._item_type)
|
|
93
|
+ file_path = content_api.get_one_revision_filepath(file.revision_id)
|
|
94
|
+ path = preview_manager.get_pdf_preview(file_path=file_path,
|
|
95
|
+ page=page)
|
133
|
96
|
tg.response.headers['Content-Disposition'] = \
|
134
|
|
- str('attachment; filename="{}"'.format(file_name))
|
|
97
|
+ 'attachment; filename="{}"'.format(file.file_name)
|
135
|
98
|
with open(path, 'rb') as pdf:
|
136
|
99
|
return pdf.read()
|
137
|
100
|
|