Skip to content

Commit

Permalink
Create all types of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiril Kirkov authored and Kiril Kirkov committed Nov 24, 2024
1 parent 739f212 commit 796aea5
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 9 deletions.
34 changes: 29 additions & 5 deletions src/BaseApiClient.php → src/BaseConverterApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Tigerra;

class BaseApiClient
class BaseConverterApiClient
{
protected $apiUrl = "https://convert.tigerra.com";
protected const API_URL = "https://convert.tigerra.com";
protected $authToken;

public function __construct($authToken)
Expand All @@ -14,7 +14,15 @@ public function __construct($authToken)

protected function sendRequest($method, $endpoint, $params = [], $filePath = null)
{
$url = $this->apiUrl . $endpoint;
if(!extension_loaded('curl')) {
throw new \Exception("cURL extension is not loaded");
}
if(!class_exists('CURLFile')) {
throw new \Exception("CURLFile class is not available");
}

$url = self::API_URL . $endpoint;

$headers = [
"Authorization: Bearer {$this->authToken}"
];
Expand All @@ -24,21 +32,37 @@ protected function sendRequest($method, $endpoint, $params = [], $filePath = nul
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 300); // Set timeout

if ($method === 'POST' && $filePath) {
$file = new \CURLFile($filePath);
if (!file_exists($file->name)) {
throw new \Exception("File not found: {$file->name}");
}

$params['file'] = $file;
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);

curl_close($ch);

if ($httpCode >= 400) {
if ($curlError) {
throw new \Exception("cURL Error: {$curlError}");
}

if ($httpCode !== 200) {
throw new \Exception("HTTP Error: {$httpCode} - Response: {$response}");
}

return json_decode($response, true);
$response = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception("JSON Error: " . json_last_error_msg());
}

return $response;
}
}
8 changes: 7 additions & 1 deletion src/ConversionStatusChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

namespace Tigerra;

class ConversionStatusChecker extends BaseApiClient
class ConversionStatusChecker extends BaseConverterApiClient
{
public const STATUS_PENDING = 'pending';
public const STATUS_COMPLETED = 'completed';
public const STATUS_UPLOAD_ERROR = 'upload_error';
public const STATUS_PROCESS_ERROR = 'process_error';
public const STATUS_CONVERT_ERROR = 'convert_error';

public function checkStatus($pid)
{
$endpoint = "/get-status/{$pid}";
Expand Down
71 changes: 69 additions & 2 deletions src/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,78 @@

namespace Tigerra;

class Converter extends BaseApiClient
class Converter extends BaseConverterApiClient
{
public function convert($conversionType, $filePath)
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';
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';
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';
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';
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';
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';

public function audio($conversionType, $filePath)
{
if (!in_array($conversionType, explode(', ', self::AUDIO_CONVERT_TYPES))) {
throw new \Exception("Invalid conversion type: {$conversionType}");
}

$endpoint = "/do-convert/{$conversionType}";
return $this->sendRequest('POST', $endpoint, [], $filePath);
}

public function video($conversionType, $filePath)
{
if (!in_array($conversionType, explode(', ', self::VIDEO_CONVERT_TYPES))) {
throw new \Exception("Invalid conversion type: {$conversionType}");
}

$endpoint = "/video/{$conversionType}";
return $this->sendRequest('POST', $endpoint, [], $filePath);
}

public function image($conversionType, $filePath)
{
if (!in_array($conversionType, explode(', ', self::IMAGE_CONVERT_TYPES))) {
throw new \Exception("Invalid conversion type: {$conversionType}");
}

$endpoint = "/do-convert/{$conversionType}";
return $this->sendRequest('POST', $endpoint, [], $filePath);
}

public function document($conversionType, $filePath)
{
if (!in_array($conversionType, explode(', ', self::DOCUMENT_CONVERT_TYPES))) {
throw new \Exception("Invalid conversion type: {$conversionType}");
}

$endpoint = "/documents/{$conversionType}";
return $this->sendRequest('POST', $endpoint, [], $filePath);
}

public function font($conversionType, $filePath)
{
if (!in_array($conversionType, explode(', ', self::FONT_CONVERT_TYPES))) {
throw new \Exception("Invalid conversion type: {$conversionType}");
}

$endpoint = "/fonts/{$conversionType}";
return $this->sendRequest('POST', $endpoint, [], $filePath);
}

public function pdf_compress($filePath)
{
$endpoint = "/documents/pdf-compress";
return $this->sendRequest('POST', $endpoint, [], $filePath);
}

public function audio_effect($effectType, $filePath, $params = [])
{
if (!in_array($effectType, explode(', ', self::AUDIO_EFFECTS))) {
throw new \Exception("Invalid audio effect type: {$effectType}");
}

$endpoint = "/{$effectType}";
return $this->sendRequest('POST', $endpoint, $params, $filePath);
}
}
11 changes: 11 additions & 0 deletions src/ConverterDeletePid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Tigerra;

class ConverterDeletePid extends BaseConverterApiClient
{
public function delete($pid)
{
return $this->sendRequest('DELETE', "/delete-pid/{$pid}");
}
}
2 changes: 1 addition & 1 deletion src/ConverterFileDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Tigerra;


class ConverterFileDownloader extends BaseApiClient
class ConverterFileDownloader extends BaseConverterApiClient
{
public function downloadFile($downloadUrl, $outputPath)
{
Expand Down

0 comments on commit 796aea5

Please sign in to comment.