1 | from ows_server.lib.base import * |
---|
2 | import os |
---|
3 | |
---|
4 | import pylons |
---|
5 | class FetchController(BaseController): |
---|
6 | def _getMimeType(self, file): |
---|
7 | if os.path.splitext(file)[1]=='.nc': |
---|
8 | return 'application/cf-netcdf' |
---|
9 | else: |
---|
10 | return 'application/unknown' |
---|
11 | |
---|
12 | |
---|
13 | def fetchFile(self, file): |
---|
14 | ''' returns a file from the filestore. Used for the wcs store parameter, but could be used for other services. |
---|
15 | checks the security information in the text file that accompanies the file and checks ndg security''' |
---|
16 | |
---|
17 | #given the filename, these are the paths to the file, and to the accompanying text file |
---|
18 | filePath=request.environ['paste.config']['app_conf']['publish_dir'] + '/' + file |
---|
19 | textFilePath=request.environ['paste.config']['app_conf']['publish_dir']+'/'+os.path.splitext(os.path.basename(file))[0]+'.txt' |
---|
20 | |
---|
21 | #open the text file and read security credentials |
---|
22 | input =open(textFilePath, 'r') |
---|
23 | sec=input.read() |
---|
24 | |
---|
25 | #check current users credentials |
---|
26 | #if they match, return the file |
---|
27 | #TODO, check this properly |
---|
28 | if sec=='No Security': |
---|
29 | match=True #allow |
---|
30 | elif 'ndgSec' in session: |
---|
31 | #if username matches |
---|
32 | if sec == str(session['ndgSec']['u']): |
---|
33 | match=True #allow |
---|
34 | else: |
---|
35 | match=False #deny |
---|
36 | else: |
---|
37 | match=False #deny |
---|
38 | if match: |
---|
39 | #return the file (netcdf) |
---|
40 | fileToReturn=open(filePath, 'r') |
---|
41 | mType=self._getMimeType(file) |
---|
42 | |
---|
43 | response.headers['Content-Type']=mType |
---|
44 | return response.write(fileToReturn) |
---|
45 | else: |
---|
46 | #return access denied message |
---|
47 | c.xml='<div class="error">%s</div>'%'<p> Access Denied </p><p>Not Logged in</p>' |
---|
48 | return render('error') |
---|