def default(self, path=b"", date=None, kind=None, usetar=None): self.assertIsInstance(path, bytes) self.assertIsInstance(date, str) self.assertTrue(kind is None or kind in ARCHIVERS) self.assertTrue(usetar is None or isinstance(usetar, str)) logger.debug("restoring [%r][%s]", path, date) # The path wont have leading and trailing "/". (path, file_b) = os.path.split(path) if not path: path = file_b file_b = b"" # Check user access to repo / path. (repo_obj, path_obj) = self.validate_user_path(path) # Get the restore date try: rdw_helpers.rdwTime(int(date)) except: logger.warning("invalid date %s", date) raise cherrypy.HTTPError(400, _("Invalid date.")) # Get if backup in progress if repo_obj.in_progress: raise cherrypy.HTTPError( 500, _("""A backup is currently in progress to this repository. Restores are disabled until this backup is complete.""" )) # Determine the kind. kind = kind or 'zip' if usetar is not None: kind = 'tar.gz' # Restore file(s) filename, fileobj = path_obj.restore(file_b, int(date), kind=kind) # Define content-disposition. cherrypy.response.headers[ "Content-Disposition"] = self._content_disposition(filename) # Stream the data. return _serve_fileobj(fileobj, content_type=None, content_length=None)
def default(self, path=b"", date=None, kind=None, usetar=None): self.assertIsInstance(path, bytes) self.assertIsInstance(date, str) self.assertTrue(kind is None or kind in ARCHIVERS) self.assertTrue(usetar is None or isinstance(usetar, str)) logger.debug("restoring [%r][%s]", path, date) # Check user access to repo / path. (repo_obj, path_obj) = self.validate_user_path(path) # Get the restore date try: RdiffTime(int(date)) except: logger.warning("invalid date %s", date) raise cherrypy.HTTPError(400, _("Invalid date.")) # Get if backup in progress # status = repo_obj.status # if status[0] != 'ok': # raise cherrypy.HTTPError(500, _(status[1] + ' ' + _("""Restores are disabled."""))) # Determine the kind. kind = kind or 'zip' if usetar is not None: kind = 'tar.gz' # Restore file(s) filename, fileobj = path_obj.restore(int(date), kind=kind) # Define content-disposition. cherrypy.response.headers[ "Content-Disposition"] = _content_disposition(filename) # Set content-type based on filename extension content_type = _content_type(filename) cherrypy.response.headers['Content-Type'] = content_type # Stream the data. # Make use of _serve_fileobj() because the fsstat() function on a pipe # return a size of 0 for Content-Length. This behavior brake all the flow. return _serve_fileobj(fileobj, content_type=content_type, content_length=None)
def default(self, path=b"", date=None, kind=None, usetar=None): self.assertIsInstance(path, bytes) self.assertIsInstance(date, str) self.assertTrue(kind is None or kind in ARCHIVERS) self.assertTrue(usetar is None or isinstance(usetar, str)) logger.debug("restoring [%r][%s]", path, date) # Check user access to repo / path. (repo_obj, path_obj) = self.validate_user_path(path) # Get the restore date try: RdiffTime(int(date)) except: logger.warning("invalid date %s", date) raise cherrypy.HTTPError(400, _("Invalid date.")) # Get if backup in progress # status = repo_obj.status # if status[0] != 'ok': # raise cherrypy.HTTPError(500, _(status[1] + ' ' + _("""Restores are disabled."""))) # Determine the kind. kind = kind or 'zip' if usetar is not None: kind = 'tar.gz' # Restore file(s) filename, fileobj = path_obj.restore(int(date), kind=kind) # Define content-disposition. cherrypy.response.headers["Content-Disposition"] = _content_disposition(filename) # Set content-type based on filename extension content_type = _content_type(filename) cherrypy.response.headers['Content-Type'] = content_type # Stream the data. # Make use of _serve_fileobj() because the fsstat() function on a pipe # return a size of 0 for Content-Length. This behavior brake all the flow. return _serve_fileobj(fileobj, content_type=content_type, content_length=None)
def default(self, path=b"", date=None, kind=None, usetar=None): self.assertIsInstance(path, bytes) self.assertIsInstance(date, str) self.assertTrue(kind is None or kind in ARCHIVERS) self.assertTrue(usetar is None or isinstance(usetar, str)) logger.debug("restoring [%r][%s]", path, date) # The path wont have leading and trailing "/". (path, file_b) = os.path.split(path) if not path: path = file_b file_b = b"" # Check user access to repo / path. (repo_obj, path_obj) = self.validate_user_path(path) # Get the restore date try: rdw_helpers.rdwTime(int(date)) except: logger.warning("invalid date %s", date) raise cherrypy.HTTPError(400, _("Invalid date.")) # Get if backup in progress if repo_obj.in_progress: raise cherrypy.HTTPError(500, _("""A backup is currently in progress to this repository. Restores are disabled until this backup is complete.""")) # Determine the kind. kind = kind or 'zip' if usetar is not None: kind = 'tar.gz' # Restore file(s) filename, fileobj = path_obj.restore(file_b, int(date), kind=kind) # Define content-disposition. cherrypy.response.headers["Content-Disposition"] = self._content_disposition(filename) # Stream the data. return _serve_fileobj(fileobj, content_type=None, content_length=None)
def serve_gzip_file(path, fname, content_type=None, disposition=None, name=None, debug=False): """Set status, headers, and body in order to serve the given path. The Content-Type header will be set to the content_type arg, if provided. If not provided, the Content-Type will be guessed by the file extension of the 'path' argument. If disposition is not None, the Content-Disposition header will be set to "<disposition>; filename=<name>". If name is None, it will be set to the basename of path. If disposition is None, no Content-Disposition header will be written. """ response = cherrypy.serving.response # If path is relative, users should fix it by making path absolute. # That is, CherryPy should not guess where the application root is. # It certainly should *not* use cwd (since CP may be invoked from a # variety of paths). If using tools.staticdir, you can make your relative # paths become absolute by supplying a value for "tools.staticdir.root". if not os.path.isabs(path): msg = "'%s' is not an absolute path." % path if debug: cherrypy.log(msg, 'TOOLS.STATICFILE') raise ValueError(msg) try: st = os.stat(path) except OSError: if debug: cherrypy.log('os.stat(%r) failed' % path, 'TOOLS.STATIC') raise cherrypy.NotFound() # Check if path is a directory. if stat.S_ISDIR(st.st_mode): # Let the caller deal with it as they like. if debug: cherrypy.log('%r is a directory' % path, 'TOOLS.STATIC') raise cherrypy.NotFound() # Set the Last-Modified response header, so that # modified-since validation code can work. response.headers['Last-Modified'] = httputil.HTTPDate(st.st_mtime) cptools.validate_since() if content_type is None: # Set content-type based on filename extension ext = "" i = path.rfind('.') if i != -1: ext = path[i:].lower() content_type = mimetypes.types_map.get(ext, None) if content_type is not None: response.headers['Content-Type'] = content_type if debug: cherrypy.log('Content-Type: %r' % content_type, 'TOOLS.STATIC') cd = None if disposition is not None: if name is None: name = os.path.basename(path) cd = '%s; filename="%s"' % (disposition, fname) response.headers["Content-Disposition"] = cd if debug: cherrypy.log('Content-Disposition: %r' % cd, 'TOOLS.STATIC') # Set Content-Length and use an iterable (file object) # this way CP won't load the whole file in memory content_length = getuncompressedsize(path) #content_length = st.st_size fileobj = gzip.open(path, 'rb') return _serve_fileobj(fileobj, content_type, content_length, debug=debug)
def serve_file(path, content_type=None, disposition=None, name=None, content_length=None, debug=False): """Set status, headers, and body in order to serve the given path. The Content-Type header will be set to the content_type arg, if provided. If not provided, the Content-Type will be guessed by the file extension of the 'path' argument. If disposition is not None, the Content-Disposition header will be set to "<disposition>; filename=<name>". If name is None, it will be set to the basename of path. If disposition is None, no Content-Disposition header will be written. """ response = cherrypy.serving.response # If path is relative, users should fix it by making path absolute. # That is, CherryPy should not guess where the application root is. # It certainly should *not* use cwd (since CP may be invoked from a # variety of paths). If using tools.staticdir, you can make your relative # paths become absolute by supplying a value for "tools.staticdir.root". if not os.path.isabs(path): msg = "'%s' is not an absolute path." % path if debug: cherrypy.log(msg, "TOOLS.STATICFILE") raise ValueError(msg) try: st = os.stat(path) except OSError: if debug: cherrypy.log("os.stat(%r) failed" % path, "TOOLS.STATIC") raise cherrypy.NotFound() # Check if path is a directory. if stat.S_ISDIR(st.st_mode): # Let the caller deal with it as they like. if debug: cherrypy.log("%r is a directory" % path, "TOOLS.STATIC") raise cherrypy.NotFound() # Set the Last-Modified response header, so that # modified-since validation code can work. response.headers["Last-Modified"] = httputil.HTTPDate(st.st_mtime) cptools.validate_since() if content_type is None: # Set content-type based on filename extension ext = "" i = path.rfind(".") if i != -1: ext = path[i:].lower() content_type = mimetypes.types_map.get(ext, None) if content_type is not None: response.headers["Content-Type"] = content_type if debug: cherrypy.log("Content-Type: %r" % content_type, "TOOLS.STATIC") cd = None if disposition is not None: if name is None: name = os.path.basename(path) cd = '%s; filename="%s"' % (disposition, name) response.headers["Content-Disposition"] = cd if debug: cherrypy.log("Content-Disposition: %r" % cd, "TOOLS.STATIC") # Set Content-Length and use an iterable (file object) # this way CP won't load the whole file in memory if not content_length: content_length = st.st_size fileobj = open(path, "rb") return _serve_fileobj(fileobj, content_type, content_length, debug=debug)