mirror of
https://github.com/Poniverse/Pony.fm.git
synced 2024-11-28 07:37: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
|
@ -58,10 +58,10 @@ class Image extends Model
|
||||||
const SMALL = 4;
|
const SMALL = 4;
|
||||||
|
|
||||||
public static $ImageTypes = [
|
public static $ImageTypes = [
|
||||||
self::NORMAL => ['id' => self::NORMAL, 'name' => 'normal', 'width' => 350, 'height' => 350, 'geometry' => '350'],
|
self::NORMAL => ['id' => self::NORMAL, 'name' => 'normal', 'width' => 350, 'height' => 350, 'geometry' => '350'],
|
||||||
self::ORIGINAL => ['id' => self::ORIGINAL, 'name' => 'original', 'width' => null, 'height' => null, 'geometry' => null],
|
self::ORIGINAL => ['id' => self::ORIGINAL, 'name' => 'original', 'width' => null, 'height' => null, 'geometry' => null],
|
||||||
self::SMALL => ['id' => self::SMALL, 'name' => 'small', 'width' => 100, 'height' => 100, 'geometry' => '100x100^'],
|
self::SMALL => ['id' => self::SMALL, 'name' => 'small', 'width' => 100, 'height' => 100, 'geometry' => '100x100^'],
|
||||||
self::THUMBNAIL => ['id' => self::THUMBNAIL, 'name' => 'thumbnail', 'width' => 50, 'height' => 50, 'geometry' => '50x50^']
|
self::THUMBNAIL => ['id' => self::THUMBNAIL, 'name' => 'thumbnail', 'width' => 50, 'height' => 50, 'geometry' => '50x50^']
|
||||||
];
|
];
|
||||||
|
|
||||||
public static function getImageTypeFromName($name)
|
public static function getImageTypeFromName($name)
|
||||||
|
@ -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