| Line 1... |
Line 1... |
| 1 |
package com.smartdukaan.cron.scheduled;
|
1 |
package com.smartdukaan.cron.scheduled;
|
| 2 |
|
2 |
|
| 3 |
import com.cloudconvert.client.CloudConvertClient;
|
3 |
import com.spice.profitmandi.common.web.client.RestClient;
|
| 4 |
import com.cloudconvert.client.setttings.StringSettingsProvider;
|
- |
|
| 5 |
import com.cloudconvert.dto.request.Base64ImportRequest;
|
- |
|
| 6 |
import com.cloudconvert.dto.request.ConvertFilesTaskRequest;
|
- |
|
| 7 |
import com.cloudconvert.dto.request.UrlExportRequest;
|
- |
|
| 8 |
import com.cloudconvert.dto.response.JobResponse;
|
- |
|
| 9 |
import com.cloudconvert.dto.response.TaskResponse;
|
- |
|
| 10 |
import com.google.common.collect.ImmutableMap;
|
- |
|
| 11 |
import com.twocaptcha.TwoCaptcha;
|
- |
|
| 12 |
import com.twocaptcha.captcha.Normal;
|
- |
|
| 13 |
import org.apache.commons.io.FileUtils;
|
4 |
import org.apache.commons.io.FileUtils;
|
| 14 |
import org.apache.commons.io.IOUtils;
|
- |
|
| 15 |
import org.apache.logging.log4j.LogManager;
|
5 |
import org.apache.logging.log4j.LogManager;
|
| 16 |
import org.apache.logging.log4j.Logger;
|
6 |
import org.apache.logging.log4j.Logger;
|
| - |
|
7 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 17 |
import org.springframework.stereotype.Service;
|
8 |
import org.springframework.stereotype.Service;
|
| 18 |
|
9 |
|
| 19 |
import java.io.File;
|
10 |
import java.io.File;
|
| 20 |
import java.io.FileOutputStream;
|
- |
|
| 21 |
import java.io.InputStream;
|
- |
|
| 22 |
import java.io.OutputStream;
|
- |
|
| 23 |
import java.util.Base64;
|
11 |
import java.util.Base64;
|
| - |
|
12 |
import java.util.HashMap;
|
| 24 |
|
13 |
|
| 25 |
@Service
|
14 |
@Service
|
| 26 |
public class CaptchaService {
|
15 |
public class CaptchaService {
|
| 27 |
|
16 |
|
| - |
|
17 |
@Autowired
|
| - |
|
18 |
RestClient restClient;
|
| 28 |
private static final Logger LOGGER = LogManager.getLogger(CaptchaService.class);
|
19 |
private static final Logger LOGGER = LogManager.getLogger(CaptchaService.class);
|
| 29 |
|
20 |
|
| 30 |
public String getCaptchaCode(String filePath) throws Exception {
|
21 |
public String getCaptchaCode(String filePath) throws Exception {
|
| 31 |
File inputFile = new File(filePath);
|
- |
|
| 32 |
//File jpgFile = this.getCloudConvertJpgImg(inputFile);
|
- |
|
| 33 |
TwoCaptcha solver = new TwoCaptcha("dbb4583561b064de1c0a48b8232c72e8");
|
- |
|
| 34 |
Normal captcha = new Normal(inputFile.getAbsolutePath());
|
- |
|
| 35 |
System.out.println("Before captcha");
|
- |
|
| 36 |
solver.solve(captcha);
|
- |
|
| 37 |
String captchaCode = captcha.getCode();
|
- |
|
| 38 |
System.out.println("Captcha code - " + captchaCode);
|
- |
|
| 39 |
return captchaCode;
|
- |
|
| 40 |
}
|
- |
|
| 41 |
|
- |
|
| 42 |
private File getCloudConvertJpgImg(File avifImg) throws Exception {
|
- |
|
| 43 |
final CloudConvertClient cloudConvertClient = new CloudConvertClient(new StringSettingsProvider(
|
- |
|
| 44 |
"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",
|
22 |
byte[] fileContent = FileUtils.readFileToByteArray(new File(filePath));
|
| 45 |
"IemxwFEudKWn89TATevY3Ip492gmmgB1", false));
|
- |
|
| 46 |
|
- |
|
| 47 |
byte[] fileContent = FileUtils.readFileToByteArray(avifImg);
|
- |
|
| 48 |
String encodedString = Base64.getEncoder().encodeToString(fileContent);
|
23 |
String encodedString = Base64.getEncoder().encodeToString(fileContent);
|
| - |
|
24 |
System.out.println(encodedString);
|
| - |
|
25 |
Base64Image base64Image = new Base64Image();
|
| - |
|
26 |
base64Image.setImage(encodedString);
|
| - |
|
27 |
return restClient.postJson("http://45.79.121.178:3031/uploader", base64Image, new HashMap<>());
|
| - |
|
28 |
}
|
| 49 |
|
29 |
|
| 50 |
final JobResponse createJobResponse = cloudConvertClient.jobs()
|
- |
|
| 51 |
.create(ImmutableMap.of("import-my-file",
|
- |
|
| 52 |
new Base64ImportRequest().setFile(encodedString).setFilename(avifImg.getName()),
|
- |
|
| 53 |
"convert-my-file",
|
- |
|
| 54 |
new ConvertFilesTaskRequest().setInput("import-my-file").setInputFormat("avif").setOutputFormat(
|
- |
|
| 55 |
"jpg"),
|
- |
|
| 56 |
"export-my-file", new UrlExportRequest().setInput("convert-my-file")))
|
- |
|
| 57 |
.getBody();
|
- |
|
| 58 |
|
- |
|
| 59 |
LOGGER.info("Create jobResponse - {}", createJobResponse);
|
- |
|
| 60 |
|
- |
|
| 61 |
// Get a job id
|
30 |
class Base64Image {
|
| 62 |
final String jobId = createJobResponse.getId();
|
- |
|
| 63 |
|
- |
|
| 64 |
// Wait for a job completion
|
- |
|
| 65 |
final JobResponse waitJobResponse = cloudConvertClient.jobs().wait(jobId).getBody();
|
- |
|
| 66 |
|
- |
|
| 67 |
final String exportUrlTaskId = waitJobResponse.getTasks().stream()
|
- |
|
| 68 |
.filter(taskResponse -> taskResponse.getName().equals("export-my-file")).findFirst().get().getId();
|
- |
|
| 69 |
|
- |
|
| 70 |
// Wait for an export/url task to be finished
|
- |
|
| 71 |
final TaskResponse waitUrlExportTaskResponse = cloudConvertClient.tasks().wait(exportUrlTaskId).getBody();
|
- |
|
| 72 |
|
- |
|
| 73 |
// Get url and filename of export/url task
|
- |
|
| 74 |
final String exportUrl = waitUrlExportTaskResponse.getResult().getFiles().get(0).get("url");
|
- |
|
| 75 |
final String filename = waitUrlExportTaskResponse.getResult().getFiles().get(0).get("filename");
|
- |
|
| 76 |
|
- |
|
| 77 |
LOGGER.info(exportUrl, filename);
|
- |
|
| 78 |
|
- |
|
| 79 |
// Get file as input stream using url of export/url task
|
- |
|
| 80 |
final InputStream inputStream = cloudConvertClient.files().download(exportUrl).getBody();
|
- |
|
| 81 |
|
- |
|
| 82 |
// Save to local file
|
31 |
private String image;
|
| 83 |
File localFile = new File("/tmp/converted.jpg");
|
- |
|
| 84 |
OutputStream outputStream = new FileOutputStream(localFile);
|
- |
|
| 85 |
IOUtils.copy(inputStream, outputStream);
|
- |
|
| 86 |
|
32 |
|
| - |
|
33 |
public String getImage() {
|
| 87 |
return localFile;
|
34 |
return image;
|
| - |
|
35 |
}
|
| - |
|
36 |
|
| - |
|
37 |
public void setImage(String image) {
|
| - |
|
38 |
this.image = image;
|
| - |
|
39 |
}
|
| 88 |
}
|
40 |
}
|
| - |
|
41 |
|
| 89 |
}
|
42 |
}
|