Advertisement
Guest User

Untitled

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