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 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')
def upload(request): response = {} try: fileInfo = request.FILES['uploadedFile'] if fileInfo.size > config.FILE_SIZE_MAX: raise Exception('File size is too big') curExt = fileUtils.getFileExt(fileInfo.name) if not docManager.isSupportedExt(curExt): raise Exception('File type is not supported') name = docManager.getCorrectName(fileInfo.name, request) path = docManager.getStoragePath(name, request) docManager.createFile(fileInfo.file, path, request, True) response.setdefault('filename', name) except Exception as e: response.setdefault('error', e.args[0]) return HttpResponse(json.dumps(response), content_type='application/json')
def upload(request): response = {} try: fileInfo = request.FILES['uploadedFile'] if ( (fileInfo.size > config.FILE_SIZE_MAX) | (fileInfo.size <= 0) ): # check if the file size exceeds the maximum size allowed (5242880) raise Exception('File size is incorrect') curExt = fileUtils.getFileExt(fileInfo.name) if not docManager.isSupportedExt( curExt ): # check if the file extension is supported by the document manager raise Exception('File type is not supported') name = docManager.getCorrectName( fileInfo.name, request ) # get file name with an index if such a file name already exists path = docManager.getStoragePath(name, request) docManager.createFile( fileInfo.file, path, request, True) # create file with meta information in the storage directory response.setdefault('filename', name) response.setdefault('documentType', fileUtils.getFileType(name)) except Exception as e: # if an error occurs response.setdefault( 'error', e.args[0]) # save an error message to the response variable return HttpResponse( json.dumps(response), content_type='application/json') # return http response in json format
def saveAs(request): response = {} try: body = json.loads(request.body) saveAsFileUrl = body.get('url') title = body.get('title') filename = docManager.getCorrectName(title, request) path = docManager.getStoragePath(filename, request) resp = requests.get(saveAsFileUrl, verify=config.DOC_SERV_VERIFY_PEER) if ( (len(resp.content) > config.FILE_SIZE_MAX) | (len(resp.content) <= 0) ): # check if the file size exceeds the maximum size allowed (5242880) response.setdefault('error', 'File size is incorrect') raise Exception('File size is incorrect') curExt = fileUtils.getFileExt(filename) if not docManager.isSupportedExt( curExt ): # check if the file extension is supported by the document manager response.setdefault('error', 'File type is not supported') raise Exception('File type is not supported') docManager.saveFileFromUri( saveAsFileUrl, path, request, True) # save the file from the new url in the storage directory response.setdefault('file', filename) except Exception as e: response.setdefault('error', 1) response.setdefault('message', e.args[0]) return HttpResponse(json.dumps(response), content_type='application/json')