Skip to content

Commit

Permalink
refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaishiyoku committed Mar 10, 2020
1 parent 279a2af commit 64574c8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
4 changes: 0 additions & 4 deletions app/Console/Commands/CrawlImages.php
Expand Up @@ -3,12 +3,8 @@
namespace App\Console\Commands;

use App\Console\BaseCommand;
use App\Models\Image;
use App\Models\Tag;
use App\Models\UpdateLog;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\DomCrawler\Crawler;

class CrawlImages extends BaseCommand
Expand Down
35 changes: 31 additions & 4 deletions app/helpers.php
Expand Up @@ -149,16 +149,15 @@ function getImageForUri($uri, $verbose = false)
if ($image) {
$image->fill($values);

createImageFileIfNotExists($image);

if ($verbose) {
logInfoWithPrintout(' #' . $externalId . ' - updated');
}
} else {
$image = new Image($values);

$fileExtension = File::extension($image->url);
$fileName = $image->external_id . '.' . $fileExtension;

Storage::disk('local')->put(env('CRAWLER_FILESYSTEM_PATH'). '/' . $fileName, getExternalContent($image->url));
createImageFile($image);

if ($verbose) {
logInfoWithPrintout(' #' . $externalId . ' - created');
Expand All @@ -175,6 +174,34 @@ function getImageForUri($uri, $verbose = false)
}
}

if (!function_exists('createImageFileIfNotExists')) {
function createImageFileIfNotExists($image)
{
try {
$imageFile = Storage::disk('local')->get(getImageFilePathFor($image));
} catch (\Illuminate\Contracts\Filesystem\FileNotFoundException $e) {
createImageFile($image);
}
}
}

if (!function_exists('createImageFile')) {
function createImageFile($image)
{
Storage::disk('local')->put(getImageFilePathFor($image), getExternalContent($image->url));
}
}

if (!function_exists('getImageFilePathFor')) {
function getImageFilePathFor($image)
{
$fileExtension = File::extension($image->url);
$fileName = $image->external_id . '.' . $fileExtension;

return env('CRAWLER_FILESYSTEM_PATH') . '/' . $fileName;
}
}

if (!function_exists('logInfoWithPrintout')) {
function logInfoWithPrintout($line)
{
Expand Down

0 comments on commit 64574c8

Please sign in to comment.