Browse Source

Merge pull request #298 from tracim/fix/249/preview_generator_stabilization

algooapy 7 years ago
parent
commit
9f2116e854

+ 1 - 1
tracim/setup.py View File

91
         ]
91
         ]
92
     },
92
     },
93
     dependency_links=[
93
     dependency_links=[
94
-        "http://tg.gy/230",
94
+        'http://github.com/algoo/preview-generator/tarball/master#egg=preview_generator-1.0',
95
         ],
95
         ],
96
     zip_safe=False
96
     zip_safe=False
97
 )
97
 )

+ 22 - 2
tracim/tracim/controllers/content.py View File

8
 
8
 
9
 from cgi import FieldStorage
9
 from cgi import FieldStorage
10
 from depot.manager import DepotManager
10
 from depot.manager import DepotManager
11
+from preview_generator.exception import PreviewGeneratorException
11
 from preview_generator.manager import PreviewManager
12
 from preview_generator.manager import PreviewManager
12
 from sqlalchemy.orm.exc import NoResultFound
13
 from sqlalchemy.orm.exc import NoResultFound
13
 import tg
14
 import tg
17
 from tg import predicates
18
 from tg import predicates
18
 from tg.i18n import ugettext as _
19
 from tg.i18n import ugettext as _
19
 from tg.predicates import not_anonymous
20
 from tg.predicates import not_anonymous
21
+from typing import List
20
 
22
 
21
 from tracim.controllers import TIMRestController
23
 from tracim.controllers import TIMRestController
22
 from tracim.controllers import StandardController
24
 from tracim.controllers import StandardController
216
             revision_id = file.revision_id
218
             revision_id = file.revision_id
217
 
219
 
218
         file_path = content_api.get_one_revision_filepath(revision_id)
220
         file_path = content_api.get_one_revision_filepath(revision_id)
219
-        nb_page = preview_manager.get_nb_page(file_path=file_path)
221
+
222
+        nb_page = 0
223
+        try:
224
+            nb_page = preview_manager.get_page_nb(file_path=file_path)
225
+        except PreviewGeneratorException as e:
226
+            # INFO - A.P - Silently intercepts preview exception
227
+            # As preview generation isn't mandatory, just register it
228
+            logger.debug(self, 'Exception: {}'.format(e.__str__))
220
         preview_urls = []
229
         preview_urls = []
221
         for page in range(int(nb_page)):
230
         for page in range(int(nb_page)):
222
             url_str = '/previews/{}/pages/{}?revision_id={}'
231
             url_str = '/previews/{}/pages/{}?revision_id={}'
225
                                  revision_id)
234
                                  revision_id)
226
             preview_urls.append(url)
235
             preview_urls.append(url)
227
 
236
 
237
+        enable_pdf_buttons = False  # type: bool
238
+        try:
239
+            enable_pdf_buttons = \
240
+                preview_manager.has_pdf_preview(file_path=file_path)
241
+        except PreviewGeneratorException as e:
242
+            # INFO - A.P - Silently intercepts preview exception
243
+            # As preview generation isn't mandatory, just register it
244
+            logger.debug(self, 'Exception: {}'.format(e.__str__))
245
+        pdf_available = 'true' if enable_pdf_buttons else 'false'  # type: str
246
+
228
         fake_api_breadcrumb = self.get_breadcrumb(file_id)
247
         fake_api_breadcrumb = self.get_breadcrumb(file_id)
229
         fake_api_content = DictLikeClass(breadcrumb=fake_api_breadcrumb,
248
         fake_api_content = DictLikeClass(breadcrumb=fake_api_breadcrumb,
230
                                          current_user=current_user_content)
249
                                          current_user=current_user_content)
235
         result = DictLikeClass(result=dictified_file,
254
         result = DictLikeClass(result=dictified_file,
236
                                fake_api=fake_api,
255
                                fake_api=fake_api,
237
                                nb_page=nb_page,
256
                                nb_page=nb_page,
238
-                               url=preview_urls)
257
+                               url=preview_urls,
258
+                               pdf_available=pdf_available)
239
         return result
259
         return result
240
 
260
 
241
     @tg.require(current_user_is_reader())
261
     @tg.require(current_user_is_reader())

+ 11 - 6
tracim/tracim/controllers/page.py View File

1
 import tg
1
 import tg
2
 from tg import expose
2
 from tg import expose
3
 from tg import tmpl_context
3
 from tg import tmpl_context
4
+from preview_generator.exception import PreviewGeneratorException
4
 from preview_generator.manager import PreviewManager
5
 from preview_generator.manager import PreviewManager
5
 
6
 
6
 from tracim.config.app_cfg import CFG
7
 from tracim.config.app_cfg import CFG
43
         else:
44
         else:
44
             file = content_api.get_one(file_id, self._item_type)
45
             file = content_api.get_one(file_id, self._item_type)
45
             file_path = content_api.get_one_revision_filepath(file.revision_id)
46
             file_path = content_api.get_one_revision_filepath(file.revision_id)
46
-        path = preview_manager.get_jpeg_preview(file_path=file_path,
47
-                                                page=page,
48
-                                                height=size,
49
-                                                width=size)
50
-        with open(path, 'rb') as large:
51
-            return large.read()
47
+        try:
48
+            path = preview_manager.get_jpeg_preview(file_path=file_path,
49
+                                                    page=page,
50
+                                                    height=size,
51
+                                                    width=size)
52
+            with open(path, 'rb') as large:
53
+                result = large.read()
54
+        except PreviewGeneratorException:
55
+            result = None
56
+        return result
52
 
57
 
53
     @expose(content_type='image/jpeg')
58
     @expose(content_type='image/jpeg')
54
     def high_quality(self,
59
     def high_quality(self,

+ 1 - 1
tracim/tracim/templates/file/getone.mak View File

127
                     modifiedAt: '${h.format_short(created_localized)|n}',
127
                     modifiedAt: '${h.format_short(created_localized)|n}',
128
                     owner: '${result.file.owner.name}',
128
                     owner: '${result.file.owner.name}',
129
                     sourceLink: '${download_url}',
129
                     sourceLink: '${download_url}',
130
-                    pdfAvailable: false // FIXME - Côme - 2017/07/24 - backend should return this information
130
+                    pdfAvailable: ${pdf_available}
131
                   }
131
                   }
132
                 )
132
                 )
133
               })()
133
               })()