1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """File or directory resources on the local filesystem."""
17
18 from os.path import getsize, basename
19
20 from muntjac.service.file_type_resolver import FileTypeResolver
21 from muntjac.terminal.application_resource import IApplicationResource
22 from muntjac.terminal.download_stream import DownloadStream
23 from muntjac.terminal.terminal import IErrorEvent
24
25
27 """C{FileResources} are files or directories on local filesystem. The
28 files and directories are served through URI:s to the client terminal
29 and thus must be registered to an URI context before they can be used.
30 The resource is automatically registered to the application when it is
31 created.
32
33 @author: Vaadin Ltd.
34 @author: Richard Lincoln
35 @version: 1.1.2
36 """
37
38 - def __init__(self, sourceFile, application):
57
58
60 """Gets the resource as stream.
61
62 @see: L{IApplicationResource.getStream}
63 """
64 try:
65 ds = DownloadStream(file(self._sourceFile, 'rb'),
66 self.getMIMEType(),
67 self.getFilename())
68 length = str( getsize(self._sourceFile) )
69 ds.setParameter('Content-Length', length)
70 ds.setCacheTime(self._cacheTime)
71 return ds
72 except IOError:
73
74 class Error(IErrorEvent):
75
76 def getThrowable(self):
77 return self.e
78
79 self.getApplication().getErrorHandler().terminalError( Error() )
80 return None
81
82
84 """Gets the source file.
85
86 @return: the source File.
87 """
88 return self._sourceFile
89
90
92 """Sets the source file.
93
94 @param sourceFile:
95 the source file to set.
96 """
97 self._sourceFile = sourceFile
98
99
101 """@see: L{IApplicationResource.getApplication}"""
102 return self._application
103
104
106 """@see: L{IApplicationResource.getFilename}"""
107 return basename(self._sourceFile)
108
109
113
114
116 """Gets the length of cache expiration time. This gives the adapter
117 the possibility cache streams sent to the client. The caching may be
118 made in adapter or at the client if the client supports caching.
119 Default is C{DownloadStream.DEFAULT_CACHETIME}.
120
121 @return: Cache time in milliseconds.
122 """
123 return self._cacheTime
124
125
127 """Sets the length of cache expiration time. This gives the adapter
128 the possibility cache streams sent to the client. The caching may be
129 made in adapter or at the client if the client supports caching. Zero
130 or negavive value disbales the caching of this stream.
131
132 @param cacheTime:
133 the cache time in milliseconds.
134 """
135 self._cacheTime = cacheTime
136
137
139 return self._bufferSize
140
141
143 """Sets the size of the download buffer used for this resource.
144
145 @param bufferSize:
146 the size of the buffer in bytes.
147 """
148 self._bufferSize = bufferSize
149