Subversion Repositories SmartDukaan

Rev

Rev 29337 | Rev 30935 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.smartdukaan.cron.scheduled;

import com.cloudconvert.client.CloudConvertClient;
import com.cloudconvert.client.setttings.StringSettingsProvider;
import com.cloudconvert.dto.request.Base64ImportRequest;
import com.cloudconvert.dto.request.ConvertFilesTaskRequest;
import com.cloudconvert.dto.request.UrlExportRequest;
import com.cloudconvert.dto.response.JobResponse;
import com.cloudconvert.dto.response.TaskResponse;
import com.google.common.collect.ImmutableMap;
import com.twocaptcha.TwoCaptcha;
import com.twocaptcha.captcha.Normal;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Base64;

@Service
public class CaptchaService {

        private static final Logger LOGGER = LogManager.getLogger(CaptchaService.class);

        public String getCaptchaCode(String filePath) throws Exception {
                File inputFile = new File(filePath);
                //File jpgFile = this.getCloudConvertJpgImg(inputFile);
                TwoCaptcha solver = new TwoCaptcha("dbb4583561b064de1c0a48b8232c72e8");
                Normal captcha = new Normal(inputFile.getAbsolutePath());
                System.out.println("Before captcha");
                solver.solve(captcha);
                String captchaCode = captcha.getCode();
                System.out.println("Captcha code - " + captchaCode);
                return captchaCode;
        }

        private File getCloudConvertJpgImg(File avifImg) throws Exception {
                final CloudConvertClient cloudConvertClient = new CloudConvertClient(new StringSettingsProvider(
                                "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiMWU3YmY3ZjllZDY5OGE5Y2I2ZTgyZDJkMDZjMjNhNDM3Zjc5MmY2MjBiMDhlOGU1NzM5ZTZlYWMwZjgwZjBmZjBiMWI3ZDBiNDI3MDdmYTkiLCJpYXQiOjE2NTA2MDgwNzguNjgzMDg2LCJuYmYiOjE2NTA2MDgwNzguNjgzMDg4LCJleHAiOjQ4MDYyODE2NzguNjcwMDA3LCJzdWIiOiI1NzQ5OTY2NCIsInNjb3BlcyI6WyJ1c2VyLnJlYWQiLCJ1c2VyLndyaXRlIiwidGFzay5yZWFkIiwidGFzay53cml0ZSIsIndlYmhvb2sucmVhZCIsInByZXNldC5yZWFkIiwid2ViaG9vay53cml0ZSIsInByZXNldC53cml0ZSJdfQ.KmzPV6tjsE02zdI-7UC_SLjcfkLAbApgSeVK60EP-IsLwSlhLyIoDt4x-MggP3NYUtsQI-IsTGmvSo3djn5nSjvt4ZLor3yhqRK5j4U8rtp47JDvsePZAtOJ7cAz6n7Snqpx5g0Qxi4c0BV8l4xAAgNtxfpmzFwm-Hko8c9eytjrqn064XS5_rHNkiCIc9GKJsL2C4Qnsh-ffxOo02MsSXFLyggkPyG3YZYxY7jrjY3eKuzoDVYF-93Sz2MgSgUfhNCQDVOvw1HvVhuGg3lZACLYTQGogubqbONB4oX9rMr4wgutbc5tsle-17Cuv6KmE7-Hni69doEoUi_7sbUxLlxcNIjYBcl9hQKDOr2TuFRjm4U9W4KPrOEpgRGeWkf-iju5mCW0Ay7rq_TgVxDKCKj2qJXhxOUXvKhIdn068BBhoOlMMFeFMQb8paqXAb-2BLbDf6yEUDiAdDeByp6UP3uiPia8SvgOJoT9C9smEKe3rfv6b0Cf-WldZDb3lA7EzsUR8SngFS50tgUR-GTy2q0jLzJkZLv5NQBlhmfdrBzpDwsiAc_A_kNv9W1WzhuyaDNeNcC9RoZ43minfyQQ86mnEwNCPiZwUBGcqnW1fXJ12FNaalU_GNtNJp1CtagQAA1UPazv_KqYwar_tbATuT1Xdj1aO7Iw6u4NGbMtSqY",
                                "IemxwFEudKWn89TATevY3Ip492gmmgB1", false));

                byte[] fileContent = FileUtils.readFileToByteArray(avifImg);
                String encodedString = Base64.getEncoder().encodeToString(fileContent);

                final JobResponse createJobResponse = cloudConvertClient.jobs()
                                .create(ImmutableMap.of("import-my-file",
                                                new Base64ImportRequest().setFile(encodedString).setFilename(avifImg.getName()),
                                                "convert-my-file",
                                                new ConvertFilesTaskRequest().setInput("import-my-file").setInputFormat("avif").setOutputFormat(
                                                                "jpg"),
                                                "export-my-file", new UrlExportRequest().setInput("convert-my-file")))
                                .getBody();

                LOGGER.info("Create jobResponse - {}", createJobResponse);

                // Get a job id
                final String jobId = createJobResponse.getId();

                // Wait for a job completion
                final JobResponse waitJobResponse = cloudConvertClient.jobs().wait(jobId).getBody();

                final String exportUrlTaskId = waitJobResponse.getTasks().stream()
                                .filter(taskResponse -> taskResponse.getName().equals("export-my-file")).findFirst().get().getId();

                // Wait for an export/url task to be finished
                final TaskResponse waitUrlExportTaskResponse = cloudConvertClient.tasks().wait(exportUrlTaskId).getBody();

                // Get url and filename of export/url task
                final String exportUrl = waitUrlExportTaskResponse.getResult().getFiles().get(0).get("url");
                final String filename = waitUrlExportTaskResponse.getResult().getFiles().get(0).get("filename");

                LOGGER.info(exportUrl, filename);

                // Get file as input stream using url of export/url task
                final InputStream inputStream = cloudConvertClient.files().download(exportUrl).getBody();

                // Save to local file
                File localFile = new File("/tmp/converted.jpg");
                OutputStream outputStream = new FileOutputStream(localFile);
                IOUtils.copy(inputStream, outputStream);

                return localFile;
        }
}