|  | @@ -0,0 +1,140 @@
 | 
	
		
			
			|  | 1 | +import tg
 | 
	
		
			
			|  | 2 | +from preview_generator.manager import PreviewManager
 | 
	
		
			
			|  | 3 | +from tg import expose, tmpl_context
 | 
	
		
			
			|  | 4 | +from tracim.config.app_cfg import CFG
 | 
	
		
			
			|  | 5 | +from tracim.controllers import TIMRestController
 | 
	
		
			
			|  | 6 | +from tracim.lib.content import ContentApi
 | 
	
		
			
			|  | 7 | +from tracim.model.data import ContentType
 | 
	
		
			
			|  | 8 | +
 | 
	
		
			
			|  | 9 | +__all__ = ['PagesController']
 | 
	
		
			
			|  | 10 | +
 | 
	
		
			
			|  | 11 | +class PagesController(TIMRestController):
 | 
	
		
			
			|  | 12 | +
 | 
	
		
			
			|  | 13 | +    @expose()
 | 
	
		
			
			|  | 14 | +    def _default(self):
 | 
	
		
			
			|  | 15 | +        return '<h2> Error Loading Page</h2>'
 | 
	
		
			
			|  | 16 | +
 | 
	
		
			
			|  | 17 | +    @expose()
 | 
	
		
			
			|  | 18 | +    def get_all(self, *args, **kwargs):
 | 
	
		
			
			|  | 19 | +        file_id = int(tg.request.controller_state.routing_args.get('file_id'))
 | 
	
		
			
			|  | 20 | +        return 'all the pages of document {}'.format(file_id)
 | 
	
		
			
			|  | 21 | +
 | 
	
		
			
			|  | 22 | +    @expose(content_type='image/jpeg')
 | 
	
		
			
			|  | 23 | +    def get_one(self, page_id: int, revision_id: int=None, *args, **kwargs):
 | 
	
		
			
			|  | 24 | +        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 | +
 | 
	
		
			
			|  | 29 | +        user = tmpl_context.current_user
 | 
	
		
			
			|  | 30 | +        content_api = ContentApi(
 | 
	
		
			
			|  | 31 | +            user,
 | 
	
		
			
			|  | 32 | +            show_archived=True,
 | 
	
		
			
			|  | 33 | +            show_deleted=True,
 | 
	
		
			
			|  | 34 | +        )
 | 
	
		
			
			|  | 35 | +        if revision_id:
 | 
	
		
			
			|  | 36 | +            file_path = content_api.get_one_revision_filepath(revision_id)
 | 
	
		
			
			|  | 37 | +        else:
 | 
	
		
			
			|  | 38 | +            file = content_api.get_one(file_id, self._item_type)
 | 
	
		
			
			|  | 39 | +            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 | +
 | 
	
		
			
			|  | 52 | +        with open(path, 'rb') as large:
 | 
	
		
			
			|  | 53 | +            return large.read()
 | 
	
		
			
			|  | 54 | +
 | 
	
		
			
			|  | 55 | +    @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()
 | 
	
		
			
			|  | 81 | +
 | 
	
		
			
			|  | 82 | +    @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()
 | 
	
		
			
			|  | 107 | +
 | 
	
		
			
			|  | 108 | +    @expose(content_type='application/pdf')
 | 
	
		
			
			|  | 109 | +    def download_pdf_one(self, page_id: int, *args, **kwargs):
 | 
	
		
			
			|  | 110 | +        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 | +
 | 
	
		
			
			|  | 127 | +        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 | +
 | 
	
		
			
			|  | 133 | +        tg.response.headers['Content-Disposition'] = \
 | 
	
		
			
			|  | 134 | +            str('attachment; filename="{}"'.format(file_name))
 | 
	
		
			
			|  | 135 | +        with open(path, 'rb') as pdf:
 | 
	
		
			
			|  | 136 | +            return pdf.read()
 | 
	
		
			
			|  | 137 | +
 | 
	
		
			
			|  | 138 | +    @property
 | 
	
		
			
			|  | 139 | +    def _item_type(self):
 | 
	
		
			
			|  | 140 | +        return ContentType.File
 |