mirror of
https://github.com/Poniverse/Pony.fm.git
synced 2024-12-01 00:57:59 +01:00
Created Image::clearExisting and refactored upload
This commit is contained in:
parent
2f498f44d3
commit
d8b9795ddc
1 changed files with 26 additions and 15 deletions
|
@ -96,17 +96,7 @@ class Image extends Model
|
||||||
|
|
||||||
if ($image) {
|
if ($image) {
|
||||||
if ($forceReupload) {
|
if ($forceReupload) {
|
||||||
// delete existing versions of the image
|
$image->clearExisting(true);
|
||||||
$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);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return $image;
|
return $image;
|
||||||
}
|
}
|
||||||
|
@ -203,4 +193,25 @@ class Image extends Model
|
||||||
mkdir($destination, 0777, true);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue