source: tracimagesvgmacro/0.10/imagesvg/web_ui.py

Last change on this file was 4467, checked in by Richard Liao, 15 years ago

Add tracimagesvg macro

File size: 1.5 KB
Line 
1# -*- coding: utf-8 -*-
2#----------------------------------------------------------------------------
3# Name:         web_ui.py
4# Purpose:      The image svg file handler module
5#
6# Author:       Richard Liao <richard.liao.i@gmail.com>
7#
8#----------------------------------------------------------------------------
9
10from trac.core import *
11from trac.web.chrome import *
12from trac.util.html import html
13
14from trac.web import IRequestHandler
15from trac.web.api import RequestDone, HTTPException
16
17from pkg_resources import resource_filename
18
19import sys, os
20import time
21
22__all__ = ['ImageSvg']
23
24class ImageSvg(Component):
25    implements(
26               IRequestHandler, 
27               )
28
29    # IRequestHandler methods
30    def match_request(self, req):
31        return req.path_info.startswith("/svg")
32
33
34    def process_request(self, req):
35        if req.path_info.startswith("/svg"):
36            pathSegs = req.path_info.split("/")
37            image_path = "/".join(pathSegs[2:])
38            f = os.path.join(self.env.path, image_path)
39            try:
40                message = open(f).read()
41            except:
42                raise HTTPException(404)
43
44            req.send_response(200)
45            req.send_header('Cache-control', 'no-cache')
46            req.send_header('Expires', 'Fri, 01 Jan 1999 00:00:00 GMT')
47            req.send_header('Content-Type', 'image/svg+xml')
48            req.send_header('Content-Length', len(message))
49            req.end_headers()
50
51            if req.method != 'HEAD':
52                req.write(message)
53            raise RequestDone
Note: See TracBrowser for help on using the repository browser.