Created Image::clearExisting and refactored upload

This commit is contained in:
Isaac 2017-11-24 21:02:35 -08:00
parent 2f498f44d3
commit d8b9795ddc

View file

@ -96,17 +96,7 @@ class Image extends Model
if ($image) {
if ($forceReupload) {
// delete existing versions of the image
$filenames = scandir($image->getDirectory());
$imagePrefix = $image->id.'_';
$filenames = array_filter($filenames, function (string $filename) use ($imagePrefix) {
return Str::startsWith($filename, $imagePrefix);
});
foreach ($filenames as $filename) {
unlink($image->getDirectory().'/'.$filename);
}
$image->clearExisting(true);
} else {
return $image;
}
@ -203,4 +193,25 @@ class Image extends Model
mkdir($destination, 0777, true);
}
}
/**
* Deletes any generated files if they exist
* @param bool $includeOriginal Deletes
*/
public function clearExisting($includeOriginal = false) {
$files = scandir($this->getDirectory());
$filePrefix = $this->id.'_';
$files = array_filter($files, function($file) use ($includeOriginal, $filePrefix) {
$originalName = Image::$ImageTypes[Image::ORIGINAL]['name'];
if ($file === $filePrefix.$originalName && !$includeOriginal)
return false;
else return (Str::startsWith($file, $filePrefix));
});
foreach ($files as $file) {
unlink($this->getDirectory().'/'.$file);
}
}
}