Skip to content

Commit 796aea5

Browse files
Kiril KirkovKiril Kirkov
Kiril Kirkov
authored and
Kiril Kirkov
committed
Create all types of functions
1 parent 739f212 commit 796aea5

5 files changed

+117
-9
lines changed

src/BaseApiClient.php renamed to src/BaseConverterApiClient.php

+29-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Tigerra;
44

5-
class BaseApiClient
5+
class BaseConverterApiClient
66
{
7-
protected $apiUrl = "https://convert.tigerra.com";
7+
protected const API_URL = "https://convert.tigerra.com";
88
protected $authToken;
99

1010
public function __construct($authToken)
@@ -14,7 +14,15 @@ public function __construct($authToken)
1414

1515
protected function sendRequest($method, $endpoint, $params = [], $filePath = null)
1616
{
17-
$url = $this->apiUrl . $endpoint;
17+
if(!extension_loaded('curl')) {
18+
throw new \Exception("cURL extension is not loaded");
19+
}
20+
if(!class_exists('CURLFile')) {
21+
throw new \Exception("CURLFile class is not available");
22+
}
23+
24+
$url = self::API_URL . $endpoint;
25+
1826
$headers = [
1927
"Authorization: Bearer {$this->authToken}"
2028
];
@@ -24,21 +32,37 @@ protected function sendRequest($method, $endpoint, $params = [], $filePath = nul
2432
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2533
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
2634
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
35+
curl_setopt($ch, CURLOPT_TIMEOUT, 300); // Set timeout
2736

2837
if ($method === 'POST' && $filePath) {
2938
$file = new \CURLFile($filePath);
39+
if (!file_exists($file->name)) {
40+
throw new \Exception("File not found: {$file->name}");
41+
}
42+
3043
$params['file'] = $file;
3144
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
3245
}
3346

3447
$response = curl_exec($ch);
3548
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
49+
$curlError = curl_error($ch);
50+
3651
curl_close($ch);
3752

38-
if ($httpCode >= 400) {
53+
if ($curlError) {
54+
throw new \Exception("cURL Error: {$curlError}");
55+
}
56+
57+
if ($httpCode !== 200) {
3958
throw new \Exception("HTTP Error: {$httpCode} - Response: {$response}");
4059
}
4160

42-
return json_decode($response, true);
61+
$response = json_decode($response, true);
62+
if (json_last_error() !== JSON_ERROR_NONE) {
63+
throw new \Exception("JSON Error: " . json_last_error_msg());
64+
}
65+
66+
return $response;
4367
}
4468
}

src/ConversionStatusChecker.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
namespace Tigerra;
44

5-
class ConversionStatusChecker extends BaseApiClient
5+
class ConversionStatusChecker extends BaseConverterApiClient
66
{
7+
public const STATUS_PENDING = 'pending';
8+
public const STATUS_COMPLETED = 'completed';
9+
public const STATUS_UPLOAD_ERROR = 'upload_error';
10+
public const STATUS_PROCESS_ERROR = 'process_error';
11+
public const STATUS_CONVERT_ERROR = 'convert_error';
12+
713
public function checkStatus($pid)
814
{
915
$endpoint = "/get-status/{$pid}";

src/Converter.php

+69-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,78 @@
22

33
namespace Tigerra;
44

5-
class Converter extends BaseApiClient
5+
class Converter extends BaseConverterApiClient
66
{
7-
public function convert($conversionType, $filePath)
7+
private const AUDIO_CONVERT_TYPES = 'aac-to-aiff, aac-to-flac, aac-to-mp3, aac-to-wav, aiff-to-aac, aiff-to-flac, aiff-to-mp3, aiff-to-wav, flac-to-aac, flac-to-aiff, flac-to-mp3, flac-to-wav, mp3-to-aac, mp3-to-aiff, mp3-to-flac, mp3-to-wav, wav-to-aac, wav-to-aiff, wav-to-flac, wav-to-mp3';
8+
private const VIDEO_CONVERT_TYPES = 'mp4-to-webm, avi-to-webm, flv-to-webm, mkv-to-webm, mpeg-to-webm, wmv-to-webm, webm-to-mp4, avi-to-mp4, flv-to-mp4, mkv-to-mp4, mpeg-to-mp4, wmv-to-mp4, webm-to-avi, mp4-to-avi, flv-to-avi, mkv-to-avi, mpeg-to-avi, wmv-to-avi, webm-to-mpeg, avi-to-mpeg, mkv-to-mpeg, flv-to-mpeg, webm-to-flv, avi-to-flv, mkv-to-flv, mpeg-to-flv, wmv-to-flv';
9+
private const IMAGE_CONVERT_TYPES = 'heic-to-jpg, pdf-to-jpg, psd-to-jpg, eps-to-jpg, webp-to-jpg, tiff-to-jpg, heic-to-png, psd-to-png, eps-to-png, webp-to-png, svg-to-png, tiff-to-png, jpg-to-png, jpg-to-webp, jpg-to-tiff, jpg-to-gif, png-to-gif, png-to-jpg, png-to-webp, png-to-svg, png-to-tiff, png-to-eps';
10+
private const DOCUMENT_CONVERT_TYPES = 'word-to-pdf, ppt-to-pdf, excel-to-pdf, odt-to-pdf, ods-to-pdf, odp-to-pdf, html-to-pdf, rtf-to-pdf, csv-to-pdf, pdf-to-html, pdf-to-odg, pdf-to-otg, pdf-to-fodg, pdf-to-docx, pdf-to-txt, html-to-odt, pdf-to-xlsx, txt-to-rtf, text-to-html, txt-to-docx, txt-to-doc, txt-to-odt, txt-to-xml, json-to-xml, xml-to-json, csv-to-json, rtf-to-txt, rtf-to-docx, rtf-to-html, rtf-to-doc, rtf-to-odt, rtf-to-ott';
11+
private const FONT_CONVERT_TYPES = 'ttf-to-otf, ttf-to-eot, ttf-to-woff, ttf-to-woff2, woff-to-otf, woff-to-eot, woff-to-ttf, woff-to-woff2, woff2-to-otf, woff2-to-eot, woff2-to-ttf, woff2-to-woff, eot-to-otf, eot-to-ttf, eot-to-woff, eot-to-woff2, otf-to-ttf, otf-to-eot, otf-to-woff, otf-to-woff2';
12+
private const AUDIO_EFFECTS = 'downmix-track, noice-reduce-track, audio-3d, volume, bass-booster, equalizer, reverse-audio, tempo, stereo-panner, auto-panner, vocal-remover, pitch-shifter, reverb, mono-to-stereo, stereo-to-mono, drunken-loudspeaker, low-frequency-noise';
13+
14+
public function audio($conversionType, $filePath)
15+
{
16+
if (!in_array($conversionType, explode(', ', self::AUDIO_CONVERT_TYPES))) {
17+
throw new \Exception("Invalid conversion type: {$conversionType}");
18+
}
19+
20+
$endpoint = "/do-convert/{$conversionType}";
21+
return $this->sendRequest('POST', $endpoint, [], $filePath);
22+
}
23+
24+
public function video($conversionType, $filePath)
25+
{
26+
if (!in_array($conversionType, explode(', ', self::VIDEO_CONVERT_TYPES))) {
27+
throw new \Exception("Invalid conversion type: {$conversionType}");
28+
}
29+
30+
$endpoint = "/video/{$conversionType}";
31+
return $this->sendRequest('POST', $endpoint, [], $filePath);
32+
}
33+
34+
public function image($conversionType, $filePath)
835
{
36+
if (!in_array($conversionType, explode(', ', self::IMAGE_CONVERT_TYPES))) {
37+
throw new \Exception("Invalid conversion type: {$conversionType}");
38+
}
39+
940
$endpoint = "/do-convert/{$conversionType}";
1041
return $this->sendRequest('POST', $endpoint, [], $filePath);
1142
}
43+
44+
public function document($conversionType, $filePath)
45+
{
46+
if (!in_array($conversionType, explode(', ', self::DOCUMENT_CONVERT_TYPES))) {
47+
throw new \Exception("Invalid conversion type: {$conversionType}");
48+
}
49+
50+
$endpoint = "/documents/{$conversionType}";
51+
return $this->sendRequest('POST', $endpoint, [], $filePath);
52+
}
53+
54+
public function font($conversionType, $filePath)
55+
{
56+
if (!in_array($conversionType, explode(', ', self::FONT_CONVERT_TYPES))) {
57+
throw new \Exception("Invalid conversion type: {$conversionType}");
58+
}
59+
60+
$endpoint = "/fonts/{$conversionType}";
61+
return $this->sendRequest('POST', $endpoint, [], $filePath);
62+
}
63+
64+
public function pdf_compress($filePath)
65+
{
66+
$endpoint = "/documents/pdf-compress";
67+
return $this->sendRequest('POST', $endpoint, [], $filePath);
68+
}
69+
70+
public function audio_effect($effectType, $filePath, $params = [])
71+
{
72+
if (!in_array($effectType, explode(', ', self::AUDIO_EFFECTS))) {
73+
throw new \Exception("Invalid audio effect type: {$effectType}");
74+
}
75+
76+
$endpoint = "/{$effectType}";
77+
return $this->sendRequest('POST', $endpoint, $params, $filePath);
78+
}
1279
}

src/ConverterDeletePid.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Tigerra;
4+
5+
class ConverterDeletePid extends BaseConverterApiClient
6+
{
7+
public function delete($pid)
8+
{
9+
return $this->sendRequest('DELETE', "/delete-pid/{$pid}");
10+
}
11+
}

src/ConverterFileDownloader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Tigerra;
44

55

6-
class ConverterFileDownloader extends BaseApiClient
6+
class ConverterFileDownloader extends BaseConverterApiClient
77
{
88
public function downloadFile($downloadUrl, $outputPath)
99
{

0 commit comments

Comments
 (0)