def convert(request):
    response = {}

    try:
        filename = request.GET['filename']
        fileUri = docManager.getFileUri(filename, request)
        fileExt = fileUtils.getFileExt(filename)
        fileType = fileUtils.getFileType(filename)
        newExt = docManager.getInternalExtension(fileType)

        if docManager.isCanConvert(fileExt):
            key = docManager.generateFileKey(filename, request)

            newUri = serviceConverter.getConverterUri(fileUri, fileExt, newExt,
                                                      key, True)

            if not newUri:
                response.setdefault('step', '0')
                response.setdefault('filename', filename)
            else:
                correctName = docManager.getCorrectName(
                    fileUtils.getFileNameWithoutExt(filename) + newExt,
                    request)
                path = docManager.getStoragePath(correctName, request)
                docManager.saveFileFromUri(newUri, path, request, True)
                docManager.removeFile(filename, request)
                response.setdefault('filename', correctName)
        else:
            response.setdefault('filename', filename)

    except Exception as e:
        response.setdefault('error', e.args[0])

    return HttpResponse(json.dumps(response), content_type='application/json')
def remove(request):
    filename = request.GET['filename']

    response = {}

    docManager.removeFile(filename, request)

    response.setdefault('success', True)
    return HttpResponse(json.dumps(response), content_type='application/json')
Example #3
0
def convert(request):
    response = {}

    try:
        body = json.loads(request.body)
        filename = fileUtils.getFileName(body.get("filename"))
        filePass = body.get("filePass")
        lang = request.COOKIES.get('ulang') if request.COOKIES.get(
            'ulang') else 'en'
        fileUri = docManager.getDownloadUrl(filename, request)
        fileExt = fileUtils.getFileExt(filename)
        fileType = fileUtils.getFileType(filename)
        newExt = docManager.getInternalExtension(
            fileType)  # internal editor extensions: .docx, .xlsx or .pptx

        if docManager.isCanConvert(
                fileExt
        ):  # check if the file extension is available for converting
            key = docManager.generateFileKey(filename,
                                             request)  # generate the file key

            newUri = serviceConverter.getConverterUri(
                fileUri, fileExt, newExt, key, True, filePass,
                lang)  # get the url of the converted file

            if not newUri:  # if the converter url is not received, the original file name is passed to the response
                response.setdefault('step', '0')
                response.setdefault('filename', filename)
            else:
                correctName = docManager.getCorrectName(
                    fileUtils.getFileNameWithoutExt(filename) + newExt, request
                )  # otherwise, create a new name with the necessary extension
                path = docManager.getStoragePath(correctName, request)
                docManager.saveFileFromUri(
                    newUri, path, request, True
                )  # save the file from the new url in the storage directory
                docManager.removeFile(filename,
                                      request)  # remove the original file
                response.setdefault(
                    'filename', correctName
                )  # pass the name of the converted file to the response
        else:
            response.setdefault(
                'filename', filename
            )  # if the file can't be converted, the original file name is passed to the response

    except Exception as e:
        response.setdefault('error', e.args[0])

    return HttpResponse(json.dumps(response), content_type='application/json')