Exemplo n.º 1
0
 def post(self):
     body = request.get_json()
     upload_id = str(uuid4())
     filename = body['filename']
     filepath = os.path.join(config.download_folder, filename)
     if filename.endswith('.pdf'):
         res = CustomResponse(Status.SUCCESS.value, filename)
         return res.getres()
     try:
         result = convert_to(os.path.join(config.download_folder, 'pdf',
                                          upload_id),
                             filepath,
                             timeout=60)
         copyfile(result,
                  os.path.join(config.download_folder, upload_id + '.pdf'))
         userfile = UserFiles(created_by=request.headers.get('ad-userid'),
                              filename=upload_id + '.pdf',
                              created_on=datetime.now())
         userfile.save()
     except LibreOfficeError as e:
         raise InternalServerErrorError(
             {'message': 'Error when converting file to PDF'})
     except TimeoutExpired:
         raise InternalServerErrorError(
             {'message': 'Timeout when converting file to PDF'})
     res = CustomResponse(Status.SUCCESS.value, upload_id + '.pdf')
     return res.getres()
def convert_to_pdf():
    body = request.get_json()
    upload_id = str(uuid4())
    filename = body['filename']
    filepath = os.path.join(NGINX_FOLDER, filename)
    try:
        result = convert_to(os.path.join(NGINX_FOLDER, 'pdf', upload_id), filepath, timeout=15)
        copyfile(result, os.path.join(NGINX_FOLDER, upload_id+'.pdf'))
    except LibreOfficeError:
        raise InternalServerErrorError({'message': 'Error when converting file to PDF'})
    except TimeoutExpired:
        raise InternalServerErrorError({'message': 'Timeout when converting file to PDF'})
    res = CustomResponse(Status.SUCCESS.value, upload_id+'.pdf')
    return res.getres()
Exemplo n.º 3
0
def uploads():
    upload_id = str(uuid4())
    target = os.path.join(APP_ROOT, 'pdfs/')
    source = os.path.join(APP_ROOT, 'docs/')
    print(target)

    if not os.path.isdir(target):
        os.mkdir(target)

    for file in request.files.getlist("file"):
        print(file)
        filename = file.filename
        destination = "/".join([target, filename])
        previous = "/".join([source, filename])
        print(previous)

        try:
            result = convert_to(source, previous, timeout=15)
        except LibreOfficeError:
            raise InternalServerErrorError(
                {'message': 'Error when converting file to PDF'})

        print(result)

    return render_template("complete.html")
Exemplo n.º 4
0
def upload_file():
    upload_id = str(uuid4())
    source = save_to(os.path.join(
        config['uploads_dir'], 'source', upload_id), request.files['file'])

    try:
        result = convert_to(os.path.join(
            config['uploads_dir'], 'pdf', upload_id), source, timeout=15)

        return send_file(uploads_url(result))

    except LibreOfficeError:
        raise InternalServerErrorError(
            {'message': 'Error when converting file to PDF'})
    except TimeoutExpired:
        raise InternalServerErrorError(
            {'message': 'Timeout when converting file to PDF'})

    return jsonify({'result': {'source': uploads_url(source), 'pdf': uploads_url(result)}})
Exemplo n.º 5
0
def handle_500_error():
    return InternalServerErrorError().to_response()
Exemplo n.º 6
0
def libreoffice_exec():
    # TODO: Provide support for more platforms
    if sys.platform == 'darwin':
        raise InternalServerErrorError({'message': 'only for unix system'})
    return 'libreoffice'