1 | ## $Id: views.py 7196 2011-11-25 07:44:52Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """Views for images and the like. |
---|
19 | """ |
---|
20 | import mimetypes |
---|
21 | import grok |
---|
22 | from hurry.file.interfaces import IHurryFile |
---|
23 | |
---|
24 | class HurryFileView(grok.View): |
---|
25 | """A view for hurry files stored in the ZODB. |
---|
26 | |
---|
27 | Normally these files cannot be 'browsed' but with a view we can |
---|
28 | stream them to the browser. We only have to set the appropriate |
---|
29 | MIME type, which happens in the `render` method. |
---|
30 | """ |
---|
31 | |
---|
32 | grok.name('index.html') |
---|
33 | grok.context(IHurryFile) |
---|
34 | |
---|
35 | def render(self): |
---|
36 | """Output the data of context. |
---|
37 | |
---|
38 | Beside this we set the output MIME type according to the |
---|
39 | context's filename. |
---|
40 | """ |
---|
41 | content_type, encoding = mimetypes.guess_type( |
---|
42 | self.context.filename.lower()) |
---|
43 | if content_type is not None: |
---|
44 | self.response.setHeader('Content-Type', content_type) |
---|
45 | fd = self.context.file |
---|
46 | contents = fd.read() |
---|
47 | fd.close() |
---|
48 | return contents |
---|