Subversion Repositories SmartDukaan

Rev

Rev 30335 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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