Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, path, *args, **kwargs):
self.path = path
self.message = "No such file or directory: {}".format(path)
super(NoSuchFile, self).__init__(self.message, *args, **kwargs)
def read(self, path, format):
path_ = self.path(path)
if not self.isfile(path):
raise NoSuchFile(path_)
with self.fs.open(path_, mode="rb") as f:
content = f.read().decode("utf-8")
return content, "text"
def _file_model_from_path(self, path, content=False, format=None):
"""
Build a file model from database record.
"""
model = base_model(path)
model["type"] = "file"
if self.fs.isfile(path):
model["last_modified"] = model["created"] = self.fs.lstat(path)["ST_MTIME"]
else:
model["last_modified"] = model["created"] = DUMMY_CREATED_DATE
if content:
try:
# Get updated format from fs.read()
content, format_ = self.fs.read(path, format)
except NoSuchFile as e:
self.no_such_entity(e.path)
except GenericFSError as e:
self.do_error(str(e), 500)
model["format"] = format_
model["content"] = content
model["mimetype"] = mimetypes.guess_type(path)[0] or "text/plain"
return model
def read(self, path, format):
path_ = self.path(path)
if not self.isfile(path):
raise NoSuchFile(path_)
with self.fs.open(path_, mode="rb") as f:
content = f.read()
if format is None or format == "text":
# Try to interpret as unicode if format is unknown or if unicode
# was explicitly requested.
try:
return content.decode("utf-8"), "text"
except UnicodeError:
if format == "text":
err = "{} is not UTF-8 encoded".format(path_)
self.log.error(err)
raise HTTPError(400, err, reason="bad format")
return base64.b64encode(content).decode("ascii"), "base64"