Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def download(self,time,file):
- if c.user:#checked to see if logged in. Can also do more checks
- rootdir = pylons.config['ofs.storage_dir'] + '/pairtree_root' #path to the file store of resources
- bucketdir = pylons.config['ckan.storage.bucket']
- bucketdir = [bucketdir[i:i + 2] for i in range(0, len(bucketdir), 2)]
- bucket = ''
- for i in bucketdir:
- bucket = bucket+'/'+i
- bucket += '/obj/' #random folder that is added in the filestore?
- filepath = str(rootdir+bucket + time.replace('%3A', ':') + '/' + file)# colon is a key word in address bar
- return self._send_file_response(filepath) #return file
- else:
- h.flash_error("You need to be logged in to download files")
- return render('home/index.html')
- def _send_file_response(self, filepath): #function taken from http://stackoverflow.com/questions/2413707/stream-a-file-to-the-http-response-in-pylons
- user_filename = '_'.join(filepath.split('/')[-1:])
- file_size = os.path.getsize(filepath)
- headers = [('Content-Disposition', 'attachment; filename=\"' + user_filename + '\"'),
- ('Content-Type', 'text/plain'),
- ('Content-Length', str(file_size))]
- from paste.fileapp import FileApp
- fapp = FileApp(filepath, headers=headers)
- return fapp(request.environ, self.start_response)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement