Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
joanfabregat committed Dec 11, 2024
1 parent 6506e94 commit e629813
Show file tree
Hide file tree
Showing 23 changed files with 258 additions and 127 deletions.
7 changes: 3 additions & 4 deletions src/Office2Pdf/Office2Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use CodeInc\DocumentCloud\Exception\InvalidResponseException;
use CodeInc\DocumentCloud\Exception\NetworkException;
use CodeInc\DocumentCloud\Exception\UnsupportedFileTypeException;
use CodeInc\DocumentCloud\Util\EndpointUrl;
use CodeInc\DocumentCloud\Util\UrlUtils;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Message\MultipartStream\MultipartStreamBuilder;
use Psr\Http\Client\ClientExceptionInterface;
Expand Down Expand Up @@ -88,7 +88,7 @@ public function convert(mixed $stream, string $filename = 'file', bool $skipType
try {
$response = $this->client->sendRequest(
$this->requestFactory
->createRequest("POST", EndpointUrl::getEndpointUrl($this->apiUrl, "/convert"))
->createRequest("POST", UrlUtils::getEndpointUrl($this->apiUrl, "/convert"))
->withHeader(
"Content-Type",
"multipart/form-data; boundary={$multipartStreamBuilder->getBoundary()}"
Expand All @@ -113,7 +113,6 @@ public function convert(mixed $stream, string $filename = 'file', bool $skipType
return $response->getBody();
}


/**
* Verifies if the client supports a file.
*
Expand Down Expand Up @@ -141,7 +140,7 @@ public function isHealthy(): bool
$response = $this->client->sendRequest(
$this->requestFactory->createRequest(
"GET",
EndpointUrl::getEndpointUrl($this->apiUrl, "/health")
UrlUtils::getEndpointUrl($this->apiUrl, "/health")
)
);

Expand Down
1 change: 1 addition & 0 deletions src/Office2Pdf/Office2PdfSupportedExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ public static function hasExtension(string $extension): bool
return true;
}
}
return false;
}
}
10 changes: 5 additions & 5 deletions src/Pdf2Img/Pdf2Img.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use CodeInc\DocumentCloud\Client;
use CodeInc\DocumentCloud\Exception\InvalidResponseException;
use CodeInc\DocumentCloud\Exception\NetworkException;
use CodeInc\DocumentCloud\Util\EndpointUrl;
use CodeInc\DocumentCloud\Util\UrlUtils;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Message\MultipartStream\MultipartStreamBuilder;
use Psr\Http\Client\ClientExceptionInterface;
Expand Down Expand Up @@ -54,12 +54,12 @@ public function __construct(

/**
* @param StreamInterface|resource|string $stream
* @param ConvertOptions $options
* @param Pdf2ImgConvertOptions $options
* @return StreamInterface
* @throws NetworkException
* @throws InvalidResponseException
*/
public function convert(mixed $stream, ConvertOptions $options = new ConvertOptions()): StreamInterface
public function convert(mixed $stream, Pdf2ImgConvertOptions $options = new Pdf2ImgConvertOptions()): StreamInterface
{
$multipartStreamBuilder = (new MultipartStreamBuilder($this->streamFactory))
->addResource(
Expand All @@ -81,7 +81,7 @@ public function convert(mixed $stream, ConvertOptions $options = new ConvertOpti
try {
$response = $this->client->sendRequest(
$this->requestFactory
->createRequest("POST", EndpointUrl::getEndpointUrl($this->apiUrl, '/convert'))
->createRequest("POST", UrlUtils::getEndpointUrl($this->apiUrl, '/convert'))
->withHeader(
"Content-Type",
"multipart/form-data; boundary={$multipartStreamBuilder->getBoundary()}"
Expand Down Expand Up @@ -115,7 +115,7 @@ public function isHealthy(): bool
$response = $this->client->sendRequest(
$this->requestFactory->createRequest(
"GET",
EndpointUrl::getEndpointUrl($this->apiUrl, '/health')
UrlUtils::getEndpointUrl($this->apiUrl, '/health')
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @author Joan Fabrégat <[email protected]>
* @license https://opensource.org/licenses/MIT MIT
*/
final readonly class ConvertOptions
final readonly class Pdf2ImgConvertOptions
{
/**
* ConvertOptions constructor.
Expand Down
4 changes: 4 additions & 0 deletions src/Pdf2Img/Pdf2ImgOutputFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

namespace CodeInc\DocumentCloud\Pdf2Img;

/**
* @author Joan Fabrégat <[email protected]>
* @license https://opensource.org/licenses/MIT MIT
*/
enum Pdf2ImgOutputFormat
{
case webp;
Expand Down
26 changes: 5 additions & 21 deletions src/Pdf2Txt/Pdf2Txt.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use CodeInc\DocumentCloud\Client;
use CodeInc\DocumentCloud\Exception\InvalidResponseException;
use CodeInc\DocumentCloud\Exception\NetworkException;
use CodeInc\DocumentCloud\Util\EndpointUrl;
use CodeInc\DocumentCloud\Util\UrlUtils;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Message\MultipartStream\MultipartStreamBuilder;
use JsonException;
Expand Down Expand Up @@ -57,12 +57,12 @@ public function __construct(
* Converts a PDF to text using streams and the PDF2TEXT API.
*
* @param StreamInterface|resource|string $stream The PDF content.
* @param ConvertOptions $options The convert options.
* @param Pdf2TxtConvertOptions $options The convert options.
* @return StreamInterface
* @throws InvalidResponseException
* @throws NetworkException
*/
public function extract(mixed $stream, ConvertOptions $options = new ConvertOptions()): StreamInterface
public function extract(mixed $stream, Pdf2TxtConvertOptions $options = new Pdf2TxtConvertOptions()): StreamInterface
{
try {
// building the multipart stream
Expand All @@ -89,7 +89,7 @@ public function extract(mixed $stream, ConvertOptions $options = new ConvertOpti
// sending the request
$response = $this->client->sendRequest(
$this->requestFactory
->createRequest("POST", EndpointUrl::getEndpointUrl($this->apiUrl, '/extract'))
->createRequest("POST", UrlUtils::getEndpointUrl($this->apiUrl, '/extract'))
->withHeader(
"Content-Type",
"multipart/form-data; boundary={$multipartStreamBuilder->getBoundary()}"
Expand All @@ -114,22 +114,6 @@ public function extract(mixed $stream, ConvertOptions $options = new ConvertOpti
return $response->getBody();
}

/**
* Processes a JSON response from the PDF2TEXT API.
*
* @param StreamInterface $response
* @return array
* @throws JsonException
*/
public function processJsonResponse(StreamInterface $response): array
{
return json_decode(
json: (string)$response,
associative: true,
flags: JSON_THROW_ON_ERROR
);
}

/**
* Health check to verify the service is running.
*
Expand All @@ -141,7 +125,7 @@ public function isHealthy(): bool
$response = $this->client->sendRequest(
$this->requestFactory->createRequest(
"GET",
EndpointUrl::getEndpointUrl($this->apiUrl, "/health")
UrlUtils::getEndpointUrl($this->apiUrl, "/health")
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @author Joan Fabrégat <[email protected]>
* @license https://opensource.org/licenses/MIT MIT
*/
final readonly class ConvertOptions
final readonly class Pdf2TxtConvertOptions
{
/**
* ConvertOptions constructor.
Expand Down
71 changes: 71 additions & 0 deletions src/Util/StreamUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/*
* Copyright 2024 Code Inc. <https://www.codeinc.co>
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/

namespace CodeInc\DocumentCloud\Util;

use CodeInc\DocumentCloud\Exception\FileOpenException;
use CodeInc\DocumentCloud\Exception\FileWriteException;
use Http\Discovery\Psr17FactoryDiscovery;
use InvalidArgumentException;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use RuntimeException;

/**
* @author Joan Fabrégat <[email protected]>
* @license https://opensource.org/licenses/MIT MIT
*/
class StreamUtils
{
/**
* Opens a local file and creates a stream from it.
*
* @param string $path The path to the file.
* @param string $openMode The mode used to open the file.
* @param StreamFactoryInterface|null $streamFactory The PSR-17 stream factory.
* @return StreamInterface
* @throws FileOpenException
*/
public static function createStreamFromFile(
string $path,
string $openMode = 'r',
?StreamFactoryInterface $streamFactory = null
): StreamInterface {
$streamFactory ??= Psr17FactoryDiscovery::findStreamFactory();

try {
return $streamFactory->createStreamFromFile($path, $openMode);
} catch (RuntimeException|InvalidArgumentException $exception) {
throw new FileOpenException($exception->getMessage(), previous: $exception);
}
}

/**
* Saves a stream to a local file.
*
* @param StreamInterface $stream
* @param string $path The path to the file.
* @param string $openMode The mode used to open the file.
* @throws FileOpenException
* @throws FileWriteException
*/
public static function saveStreamToFile(StreamInterface $stream, string $path, string $openMode = 'w'): void
{
$f = fopen($path, $openMode);
if ($f === false) {
throw new FileOpenException("The file '$path' could not be opened");
}

if (stream_copy_to_stream($stream->detach(), $f) === false) {
throw new FileWriteException("The stream could not be copied to the file '$path'");
}

fclose($f);
}
}
2 changes: 1 addition & 1 deletion src/Util/EndpointUrl.php → src/Util/UrlUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @author Joan Fabrégat <[email protected]>
* @license https://opensource.org/licenses/MIT MIT
*/
class EndpointUrl
class UrlUtils
{
/**
* Returns the URL of an endpoint.
Expand Down
37 changes: 7 additions & 30 deletions src/Watermarker/Watermarker.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@

use CodeInc\DocumentCloud\Client;
use CodeInc\DocumentCloud\Exception\FileOpenException;
use CodeInc\DocumentCloud\Exception\FileWriteException;
use CodeInc\DocumentCloud\Exception\InvalidResponseException;
use CodeInc\DocumentCloud\Exception\NetworkException;
use CodeInc\DocumentCloud\Util\EndpointUrl;
use CodeInc\DocumentCloud\Util\UrlUtils;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Message\MultipartStream\MultipartStreamBuilder;
use Psr\Http\Client\ClientExceptionInterface;
Expand All @@ -33,8 +32,8 @@
*/
readonly class Watermarker
{
private StreamFactoryInterface $streamFactory;
private RequestFactoryInterface $requestFactory;
public StreamFactoryInterface $streamFactory;
public RequestFactoryInterface $requestFactory;

/**
* Watermarker constructor.
Expand All @@ -59,15 +58,15 @@ public function __construct(
*
* @param StreamInterface|resource|string $imageStream The PDF content.
* @param StreamInterface|resource|string $watermarkStream The watermark content.
* @param ConvertOptions $options The convert options.
* @param WatermarkerConvertOptions $options The convert options.
* @return StreamInterface
* @throws InvalidResponseException
* @throws NetworkException
*/
public function apply(
mixed $imageStream,
mixed $watermarkStream,
ConvertOptions $options = new ConvertOptions()
WatermarkerConvertOptions $options = new WatermarkerConvertOptions()
): StreamInterface {
try {
// building the multipart stream
Expand All @@ -89,7 +88,7 @@ public function apply(
// sending the request
$response = $this->client->sendRequest(
$this->requestFactory
->createRequest("POST", EndpointUrl::getEndpointUrl($this->apiUrl, '/apply'))
->createRequest("POST", UrlUtils::getEndpointUrl($this->apiUrl, '/apply'))
->withHeader(
"Content-Type",
"multipart/form-data; boundary={$multipartStreamBuilder->getBoundary()}"
Expand Down Expand Up @@ -132,28 +131,6 @@ public function createStreamFromFile(string $path, string $openMode = 'r'): Stre
return $this->streamFactory->createStreamFromResource($f);
}

/**
* Saves a stream to a local file.
*
* @param StreamInterface $stream
* @param string $path The path to the file.
* @param string $openMode The mode used to open the file.
* @throws FileOpenException
* @throws FileWriteException
*/
public function saveStreamToFile(StreamInterface $stream, string $path, string $openMode = 'w'): void
{
$f = fopen($path, $openMode);
if ($f === false) {
throw new FileOpenException("The file '$path' could not be opened");
}

if (stream_copy_to_stream($stream->detach(), $f) === false) {
throw new FileWriteException("The stream could not be copied to the file '$path'");
}

fclose($f);
}

/**
* Health check to verify the service is running.
Expand All @@ -166,7 +143,7 @@ public function isHealthy(): bool
$response = $this->client->sendRequest(
$this->requestFactory->createRequest(
"GET",
EndpointUrl::getEndpointUrl($this->apiUrl, "/health")
UrlUtils::getEndpointUrl($this->apiUrl, "/health")
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @author Joan Fabrégat <[email protected]>
* @license https://opensource.org/licenses/MIT MIT
*/
final readonly class ConvertOptions
final readonly class WatermarkerConvertOptions
{
/**
* ConvertOptions constructor.
Expand Down
Loading

0 comments on commit e629813

Please sign in to comment.