Skip to content

Commit

Permalink
Fix Truncated Name
Browse files Browse the repository at this point in the history
  • Loading branch information
SupianIDz committed Jan 30, 2023
1 parent 20ab535 commit 05a2d4c
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions app/Modules/Downloader/ImageFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,34 @@ public function handle() : mixed

$name = $this->name($image);

// TODO : optimize this
$this->output->task($name, function () use ($name, $image) {
if (! $this->storage) {
Storage::disk('image')->put($name, $this->http->fetch(
$image->url
));
if (Storage::disk('image')->exists($name)) {
$image->update([
'status' => true,
]);
} else {
Storage::disk('image')->put($name, $this->http->fetch(
$image->url
));
}
} else {
$directory = preg_replace('/\/+/', '/', $this->storage . '/' . dirname($name));

if (! is_dir($directory)) {
mkdir($directory, 0777, true);
}

file_put_contents($this->storage . '/' . $name, $this->http->fetch(
$image->url
));
if (file_exists($this->storage . '/' . $name)) {
$image->update([
'status' => true,
]);
} else {
file_put_contents($this->storage . '/' . $name, $this->http->fetch(
$image->url
));
}
}

$image->update([
Expand All @@ -76,15 +89,17 @@ private function name(Image $image) : string
$image->page->main->url, PHP_URL_PATH
));

$name = preg_replace('/[-_]{2,}/', '-', $name);

if (strlen($name) > 100) {
$name = substr($name, 0, 10);
$name = substr($name, 0, 100);
}

$name .= '/';
$name .= basename(parse_url(
$image->url, PHP_URL_PATH
));

return $name;
return preg_replace('/\/+/', '/', $name);
}
}

0 comments on commit 05a2d4c

Please sign in to comment.