| [4467] | 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 | |
|---|
| 10 | from trac.core import * |
|---|
| 11 | from trac.web.chrome import * |
|---|
| 12 | from trac.util.html import html |
|---|
| 13 | |
|---|
| 14 | from trac.web import IRequestHandler |
|---|
| 15 | from trac.web.api import RequestDone, HTTPException |
|---|
| 16 | |
|---|
| 17 | from pkg_resources import resource_filename |
|---|
| 18 | |
|---|
| 19 | import sys, os |
|---|
| 20 | import time |
|---|
| 21 | |
|---|
| 22 | __all__ = ['ImageSvg'] |
|---|
| 23 | |
|---|
| 24 | class 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 |
|---|