utils_33.py 821 B

1234567891011121314151617181920212223
  1. def _save_large_file(self, os_path, content, format):
  2. """Save content of a generic file."""
  3. if format not in {'text', 'base64'}:
  4. raise web.HTTPError(
  5. 400,
  6. "Must specify format of file contents as 'text' or 'base64'",
  7. )
  8. try:
  9. if format == 'text':
  10. bcontent = content.encode('utf8')
  11. else:
  12. b64_bytes = content.encode('ascii')
  13. bcontent = base64.b64decode(b64_bytes)
  14. except Exception as e:
  15. raise web.HTTPError(
  16. 400, u'Encoding error saving %s: %s' % (os_path, e)
  17. )
  18. with self.perm_to_403(os_path):
  19. if os.path.islink(os_path):
  20. os_path = os.path.join(os.path.dirname(os_path), os.readlink(os_path))
  21. with io.open(os_path, 'ab') as f:
  22. f.write(bcontent)