source: cows_wps/trunk/cows_wps/controllers/download.py @ 5615

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/cows_wps/controllers/download.py@5615
Revision 5615, 2.3 KB checked in by spascoe, 11 years ago (diff)

COWS WPS package copied from
 http://proj.badc.rl.ac.uk/svn/dcip/cows-wps/trunk.

This is a stripped down version of the DDP WPS. Some features are
removed and others have been deactivated until we reimplement them in a
more generic way.

Line 
1
2import os, stat
3
4from cows_wps.controllers import *
5
6from cows_wps.process_handler.context.process_context import ProcessContext
7from cows_wps.model.managers import requestManager
8from cows_wps.model.process_dir import processDirManager
9
10import logging
11log = logging.getLogger(__name__)
12
13#!TODO: make this configurable
14mime_types = {'.png': 'image/png', 
15              '.jpg': 'image/jpeg', 
16              '.pdf': 'application/pdf', 
17              '.ps': 'application/postscript', 
18              '.html': 'text/html', 
19              '.xml': 'text/xml'
20              }
21def _mime(path):
22    root, ext = os.path.splitext(path)
23    return mime_types.get(ext.lower(), 'text/foo')
24   
25
26class DownloadController(BaseController):
27
28    def index(self, jobId, fileName):
29
30        req = requestManager.getRequest(jobId, sgeUpdate=False)
31        self.processDir = req.job.process_dir
32        identifier = req.job.process
33       
34        self.jobId = jobId
35        self.fileName = fileName
36       
37        if processDirManager.isStripedProcess(identifier):
38            return self._serveStriped()
39        else:
40            return self._serveLocal()
41
42    def _serveLocal(self):
43        context = ProcessContext(self.processDir).open()
44       
45        log.debug("Downloading file %s from directory %s." \
46                  % (self.fileName, context.outputDir))
47       
48        fn = os.path.join(context.outputDir, self.fileName)
49        response.content_type = _mime(fn)
50        context.close()
51       
52        if os.path.exists(fn):
53            return stream(fn)
54        else:
55            raise ValueError("File %s not found in Job %s" % (self.fileName, self.jobId))
56   
57    def _serveStriped(self):
58        redirect = processDirManager.getStripedRedirect(self.processDir)
59
60        #!FIXME: Note this hard-codes the name of the outputs folder.  Should be fixed
61        url = '%s/outputs/%s' % (redirect, self.fileName)
62        return redirect_to(str(url)) # remove unicode
63   
64def stream(fileName):
65    """
66    Return a WSGI-compatible return value of the contents of fileName, using iterators if necessary
67    """
68    size = os.stat(fileName)[stat.ST_SIZE]
69    fh = open(fileName)
70   
71    # If over 500K stream
72    if size > 500*1024:
73        s = fh.read(1024)
74        while s:
75            yield s
76            s = fh.read(1024)
77    else:
78        yield fh.read()
Note: See TracBrowser for help on using the repository browser.