Skip to content

Commit

Permalink
remote upload feature
Browse files Browse the repository at this point in the history
  • Loading branch information
reliq committed Mar 7, 2021
1 parent 7bae0e5 commit 2d24e34
Show file tree
Hide file tree
Showing 9 changed files with 552 additions and 63 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"intervention/image": "^2.4",
"intervention/imagecache": "^2.3",
"reliqarts/laravel-common": "^5.4",
"ext-json": "*"
"ext-json": "*",
"ext-fileinfo": "*"
},
"require-dev": {
"orchestra/testbench": "4 - 6",
Expand Down
78 changes: 33 additions & 45 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
bootstrap="tests/bootstrap.php"
cacheResult="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
verbose="true">

<testsuites>
<testsuite name="Tests">
<directory suffix=".php">tests</directory>
</testsuite>
<testsuite name="Integration">
<directory suffix="Test.php">tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<directory suffix=".php">src/Concerns</directory>
</exclude>
</whitelist>
</filter>
<php>
<ini name="error_reporting" value="E_ALL" />
<ini name="display_errors" value="1" />
<ini name="display_startup_errors" value="1" />

<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="GUIDED_IMAGE_MODEL" value="GuidedImage" />
</php>
<logging>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/coverage.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" beStrictAboutTestsThatDoNotTestAnything="true" bootstrap="tests/bootstrap.php" cacheResult="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" verbose="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory suffix=".php">src/Concerns</directory>
</exclude>
<report>
<clover outputFile="build/coverage.xml"/>
<text outputFile="build/coverage.txt"/>
</report>
</coverage>
<testsuites>
<testsuite name="Tests">
<directory suffix=".php">tests</directory>
</testsuite>
<testsuite name="Integration">
<directory suffix="Test.php">tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">tests/Unit</directory>
</testsuite>
</testsuites>
<php>
<ini name="error_reporting" value="E_ALL"/>
<ini name="display_errors" value="1"/>
<ini name="display_startup_errors" value="1"/>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="GUIDED_IMAGE_MODEL" value="GuidedImage"/>
</php>
<logging/>
</phpunit>
52 changes: 52 additions & 0 deletions src/Contract/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace ReliqArts\GuidedImage\Contract;

use Illuminate\Http\UploadedFile;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;

interface FileHelper
{
public function hashFile(string $filePath): string;
Expand All @@ -12,4 +16,52 @@ public function hashFile(string $filePath): string;
* @return array|false an array with 7 elements, false on failure
*/
public function getImageSize(string $filename, array &$imageInfo = []);

/**
* @param mixed $data
*
* @see file_put_contents()
*/
public function putContents(string $filename, $data): ?int;

/**
* @see file_get_contents()
*/
public function getContents(string $filename, bool $useIncludePath = false): ?string;

/**
* @param resource|null $context
*
* @see unlink()
*/
public function unlink(string $filename, $context = null): bool;

/**
* Returns directory path used for temporary files
*
* @see sys_get_temp_dir()
*/
public function getSystemTempDirectory(): string;

/**
* @return string|null the new temporary file name or null on failure
* @see tmpnam()
*/
public function tempName(string $directory, string $prefix): ?string;

/**
* @see mime_content_type()
*/
public function getMimeType(string $filename): ?string;

/**
* Get associated extension for particular mime type.
*/
public function mime2Ext(string $mime): ?string;

/**
* @throws FileException
* @throws FileNotFoundException
*/
public function createUploadedFile(string $tempFile, string $originalName, string $mimeType): UploadedFile;
}
11 changes: 8 additions & 3 deletions src/Contract/ImageUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
namespace ReliqArts\GuidedImage\Contract;

use Illuminate\Http\UploadedFile;
use ReliqArts\GuidedImage\Exception\UrlUploadFailed;
use ReliqArts\GuidedImage\Result;

interface ImageUploader
{
/**
* Upload and save image.
*/
public function upload(UploadedFile $imageFile, bool $isUrlUpload = false): Result;

/**
* Upload an image from remote. (allow_url_fopen must be enabled).
*
* @param UploadedFile $imageFile File from request
* .e.g. request->file('image');
* @throws UrlUploadFailed
*/
public function upload(UploadedFile $imageFile): Result;
public function uploadFromUrl(string $url): Result;
}
32 changes: 32 additions & 0 deletions src/Exception/UrlUploadFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace ReliqArts\GuidedImage\Exception;

use RuntimeException;
use Throwable;

final class UrlUploadFailed extends RuntimeException
{
private const ERROR_TEMPLATE = 'Failed to upload image from url: %s. %s';

private string $url;

public static function forUrl(string $url, Throwable $previousException): self
{
$instance = new self(
sprintf(self::ERROR_TEMPLATE, $url, $previousException->getMessage()),
$previousException->getCode(),
$previousException
);
$instance->url = $url;

return $instance;
}

public function getUrl(): string
{
return $this->url;
}
}
Loading

0 comments on commit 2d24e34

Please sign in to comment.