diff options
Diffstat (limited to 'ishtar_common/libreoffice.py')
-rw-r--r-- | ishtar_common/libreoffice.py | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/ishtar_common/libreoffice.py b/ishtar_common/libreoffice.py index 3869ca81a..573d99361 100644 --- a/ishtar_common/libreoffice.py +++ b/ishtar_common/libreoffice.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- import time @@ -8,15 +8,37 @@ from com.sun.star.beans import PropertyValue from com.sun.star.connection import NoConnectException from com.sun.star.sheet.ValidationType import LIST +# nosec: filename used is generated and sanitized +import subprocess # nosec + #from com.sun.star.table import CellRangeAddress, CellAddress -from ishtar_common.utils import num2col +from ishtar_common.utils import num2col, sanitize_filepath from django.conf import settings RETRY = 5 +def get_connection(): + return "socket,host={},port={};urp".format( + settings.LIBREOFFICE_HOST, settings.LIBREOFFICE_PORT + ) + + +def convert_document(document, destination_format): + if not document.lower().endswith(".ods") and not document.lower().endswith(".odt"): + return document + document = sanitize_filepath(document) + connection = get_connection() + ";StarOffice.ComponentContext" + args = [settings.UNOCONV_BINARY, "--connection", connection, "-f", destination_format, + document] + # nosec: filename is generated and sanitized + popen = subprocess.Popen(args) # nosec + popen.wait(timeout=20) + dest_document = document[:-4] + "." + destination_format + return dest_document + class UnoClient: def __init__(self): @@ -30,9 +52,7 @@ class UnoClient: resolver = local_context.ServiceManager.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", local_context) - connection = "socket,host={},port={};urp".format( - settings.LIBREOFFICE_HOST, settings.LIBREOFFICE_PORT - ) + connection = get_connection() try: self.service_manager = resolver.resolve( "uno:{};StarOffice.ServiceManager".format(connection)) |