ONLYOFFICE/docs-integration-sdk-java

Question: Is it possible to use the convert service by passing the stream of the file instead of a url to download it from ?

Closed this issue · 2 comments

Sorry I must be really stupid can you give me an example on how to use convertService ? I don't quite understand how to set the stream/byte array/file to convert in the call.

I wanted to avoid having onlyoffice download the file from a url and pass the file stream directly to it. Is this possible ?

Here my full example:

private void transformWithSDK(final String sourceMimetype, final String targetMimetype, final Map<String, String> parameters,
    		final File sourceFile, final File targetFile, TransformManager transformManager) throws Exception
    {
    	logger.info("START TRANSFORMER SDK '"+this.getClass().getSimpleName());
    	String extensionSource = FilenameUtils.getExtension(sourceFile.getName());
    	String extensionTarget = FilenameUtils.getExtension(targetFile.getName());
    	String srcType = extensionSource;
    	String outType = extensionTarget;

    	String url = urlManager.getInnerDocumentServerUrl();
    	Security security = Security.builder()
    			.key(settingsManager.getSecurityKey())
    			.header(settingsManager.getSecurityHeader())
    			.prefix(settingsManager.getSecurityPrefix())
    			.build();

    	HttpClientSettings httpClientSettings = HttpClientSettings.builder()
    			.ignoreSSLCertificate(settingsManager.isIgnoreSSLCertificate())
    			.build();

    	String convertServiceUrl = settingsManager.getDocsIntegrationSdkProperties()
    			.getDocumentServer()
    			.getConvertService()
    			.getUrl();

    	ConvertRequest convertRequest = ConvertRequest.builder()
    			.async(false)
    			.filetype(srcType) // txt
    			.outputtype(outType) // docx
    			.key(new SimpleDateFormat("MMddyyyyHHmmss").format(new Date()))
    			.url(sourceFile.toURI().toURL().toString()) // ????? I MUS CREATE A GET SERVICE FOR ONLYOFFICE ???
    			.build();

    	requestManager.executePostRequest(
    			urlManager.sanitizeUrl(url) + convertServiceUrl,
    			convertRequest,
    			security,
    			httpClientSettings,
    			new RequestManager.Callback< ValidationResult>() {
    				public ValidationResult doWork(final Object response) throws Exception {
    					String content = IOUtils.toString(((HttpEntity) response).getContent(), "utf-8").trim();
    					JSONObject result = new JSONObject(content);

    					if (result.has("error")) {
    						Integer errorCode = result.getInt("error");

    						return ValidationResult.builder()
    								.status(Status.FAILED)
    								.error(ConvertResponse.Error.valueOfCode(errorCode))
    								.build();
    					}

    					String fileUrl = result.getString("fileUrl");

    					return requestManager.executeGetRequest(
    							fileUrl,
    							httpClientSettings,
    							new RequestManager.Callback<ValidationResult>() {
    								@Override
    								public ValidationResult doWork(final Object response) throws IOException {
    									byte[] bytes = EntityUtils.toByteArray((HttpEntity) response);
    									if (bytes.length > 0) {
    										
    										FileUtils.writeByteArrayToFile(targetFile, bytes);
    										
    										return ValidationResult.builder()
    												.status(Status.SUCCESS)
    												.build();
    									} else {
    										return ValidationResult.builder()
    												.status(Status.FAILED)
    												.error(CommonResponse.Error.DOWNLOAD_RESULT)
    												.build();
    									}
    								}
    							});

    				}
    			});

    	logger.info("END TRANSFORMER SDK '"+this.getClass().getSimpleName());
    }

I'm afraid it's not possible to use a stream with the conversion service, this has to be a URL to the file you want to convert: https://api.onlyoffice.com/editors/conversionapi#url.
You can take a look at our Confluence integration to see how the SDK could be used to set up conversion: https://github.com/ONLYOFFICE/onlyoffice-confluence/blob/master/src/main/java/onlyoffice/OnlyOfficeConvertServlet.java.

Ty for the response ! I will prepare a rest service for retrieve the target file then