Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
body = (
b"\n"
b"\n \n <title>Test</title>\n\n"
b"<h1>Example</h1><p>This is an <a>example</a> text document.\n"
b"It should be <blink>converted</blink>.</p>\n\n"
b"<p>It has another paragraph <strong>here</strong>, "
b"images: <img src="\"here.png\"" width="\"50\""> "
b"<img width="\"30\"" src="\"/over/there.png\"" title="\"important\""> "
b"<img class="\"a\"" src="\"http://and/the/last/one.png\"">, and "
b"links: <a href="\"here\"">1</a> "
b"<a href="\"/over/there\"" title="\"important\"">2</a> "
b"<a class="\"a\"" href="\"http://and/the/last/one\"">3</a></p>\n"
b"\n"
)
with mock.patch('tornado.process.Subprocess', object()):
body = await convert.to_html(body, 'text/html', 'test.html',
self.config)
self.assertEqual(
body,
"<h1>Example</h1><p>This is an example text document.\n"
"It should be converted.</p>\n\n"
async def post(self, project_id):
project = self.get_project(project_id)
name = self.get_body_argument('name')
description = self.get_body_argument('description')
file = self.request.files['file'][0]
content_type = file.content_type
filename = secure_filename(file.filename)
try:
body = await convert.to_html_chunks(file.body, content_type,
filename)
except convert.ConversionError as err:
self.set_status(400)
self.send_json({
'error': str(err),
})
else:
doc = database.Document(
name=name,
description=description,
filename=filename,
project=project,
contents=body,
)
self.db.add(doc)
self.db.flush() # Need to flush to get doc.id
self.set_status(403)
return self.send_json({'error': "Unauthorized"})
try:
name = self.get_body_argument('name')
validate.document_name(name)
description = self.get_body_argument('description')
validate.document_description(description)
try:
file = self.request.files['file'][0]
except (KeyError, IndexError):
raise MissingArgumentError('file')
content_type = file.content_type
filename = validate.filename(file.filename)
try:
body = await convert.to_html_chunks(
file.body, content_type, filename,
self.application.config,
)
except convert.ConversionError as err:
self.set_status(400)
return self.send_json({
'error': str(err),
})
else:
doc = database.Document(
name=name,
description=description,
filename=filename,
project=project,
contents=body,
)
sheet.set_column(1, 1, 15.0)
sheet.set_column(2, 2, 15.0)
sheet.set_column(3, 3, 80.0)
row = 1
for hl in highlights:
tags = hl.tags
assert all(isinstance(t, database.Tag) for t in tags)
if tags:
tags = [t.path for t in tags]
else:
tags = ['']
for tag_path in tags:
sheet.write(row, 0, str(hl.id))
sheet.write(row, 1, hl.document.name)
sheet.write(row, 2, tag_path)
sheet.write(row, 3, convert.html_to_plaintext(hl.snippet))
row += 1
workbook.close()
with open(filename, 'rb') as fp:
chunk = fp.read(4096)
self.write(chunk)
while len(chunk) == 4096:
chunk = fp.read(4096)
if chunk:
self.write(chunk)
return self.finish()
finally:
shutil.rmtree(tmp)
def init_PROM_EXPORT(w):
for e in convert.html_to_extensions:
PROM_EXPORT.labels(w, e).inc(0)
async def wrapper(self, *args):
ext = args[-1]
ext = ext.lower()
name, html = wrapped(self, *args)
try:
mimetype, contents = convert.html_to(
html, ext,
self.application.config,
)
except convert.UnsupportedFormat:
self.set_status(404)
self.set_header('Content-Type', 'text/plain')
return self.finish("Unsupported format: %s" % ext)
self.set_header('Content-Type', mimetype)
if name:
self.set_header('Content-Disposition',
'attachment; filename="%s.%s"' % (name, ext))
else:
self.set_header('Content-Disposition', 'attachment')
for chunk in await contents:
self.write(chunk)
return self.finish()
async def wrapper(self, *args):
ext = args[-1]
ext = ext.lower()
name, html = wrapped(self, *args)
try:
mimetype, contents = convert.html_to(
html, ext,
self.application.config,
)
except convert.UnsupportedFormat:
self.set_status(404)
self.set_header('Content-Type', 'text/plain')
return self.finish("Unsupported format: %s" % ext)
self.set_header('Content-Type', mimetype)
if name:
self.set_header('Content-Disposition',
'attachment; filename="%s.%s"' % (name, ext))
else:
self.set_header('Content-Disposition', 'attachment')
for chunk in await contents:
self.write(chunk)
return self.finish()
return wrapper
async def wrapper(self, *args):
args, ext = args[:-1], args[-1]
ext = ext.lower()
name, html = wrapped(self, *args)
mimetype, contents = convert.html_to(html, ext)
self.set_header('Content-Type', mimetype)
self.set_header('Content-Disposition',
'attachment; filename="%s.%s"' % (name, ext))
for chunk in await contents:
self.write(chunk)
self.finish()
return wrapper