[UTILS][TemporaryFile] Change way TemporaryFile takes arguments and it's internal implementation
This commit is contained in:
parent
3b39046a38
commit
2b83a4b627
@ -92,7 +92,7 @@ class Avatar extends Controller
|
|||||||
list(, $mimetype_user, , $encoding_user, $data_user) = $matches;
|
list(, $mimetype_user, , $encoding_user, $data_user) = $matches;
|
||||||
if ($encoding_user == 'base64') {
|
if ($encoding_user == 'base64') {
|
||||||
$data_user = base64_decode($data_user);
|
$data_user = base64_decode($data_user);
|
||||||
$tempfile = new TemporaryFile('avatar');
|
$tempfile = new TemporaryFile(['prefix' => 'avatar']);
|
||||||
$path = $tempfile->getRealPath();
|
$path = $tempfile->getRealPath();
|
||||||
file_put_contents($path, $data_user);
|
file_put_contents($path, $data_user);
|
||||||
$sfile = new SymfonyFile($path);
|
$sfile = new SymfonyFile($path);
|
||||||
|
@ -77,7 +77,7 @@ class ImageEncoder extends Plugin
|
|||||||
|
|
||||||
$type = self::preferredType();
|
$type = self::preferredType();
|
||||||
$extension = image_type_to_extension($type, include_dot: true);
|
$extension = image_type_to_extension($type, include_dot: true);
|
||||||
$temp = new TemporaryFile(prefix: null, suffix: $extension); // This handles deleting the file if some error occurs
|
$temp = new TemporaryFile(['prefix' => 'image', 'suffix' => $extension]); // This handles deleting the file if some error occurs
|
||||||
$mimetype = image_type_to_mime_type($type);
|
$mimetype = image_type_to_mime_type($type);
|
||||||
if ($mimetype != $original_mimetype) {
|
if ($mimetype != $original_mimetype) {
|
||||||
// If title seems to be a filename with an extension
|
// If title seems to be a filename with an extension
|
||||||
|
@ -72,7 +72,7 @@ class VideoEncoder extends Plugin
|
|||||||
|
|
||||||
// FFmpeg can't edit existing files in place,
|
// FFmpeg can't edit existing files in place,
|
||||||
// generate temporary output file to avoid that
|
// generate temporary output file to avoid that
|
||||||
$tempfile = new TemporaryFile('gs-outpath');
|
$tempfile = new TemporaryFile(['prefix' => 'video']);
|
||||||
|
|
||||||
// Generate palette file. FFmpeg explictly needs to be told the
|
// Generate palette file. FFmpeg explictly needs to be told the
|
||||||
// extension for PNG files outputs
|
// extension for PNG files outputs
|
||||||
|
@ -100,18 +100,17 @@ class AttachmentThumbnail extends Entity
|
|||||||
return $this->filename;
|
return $this->filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setModified(\DateTimeInterface $modified): self
|
public function setModified(DateTimeInterface $modified): self
|
||||||
{
|
{
|
||||||
$this->modified = $modified;
|
$this->modified = $modified;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getModified(): \DateTimeInterface
|
public function getModified(): DateTimeInterface
|
||||||
{
|
{
|
||||||
return $this->modified;
|
return $this->modified;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// }}} Autocode
|
// }}} Autocode
|
||||||
|
|
||||||
private Attachment $attachment;
|
private Attachment $attachment;
|
||||||
@ -140,7 +139,7 @@ class AttachmentThumbnail extends Entity
|
|||||||
});
|
});
|
||||||
} catch (NotFoundException $e) {
|
} catch (NotFoundException $e) {
|
||||||
$ext = image_type_to_extension(IMAGETYPE_WEBP, include_dot: true);
|
$ext = image_type_to_extension(IMAGETYPE_WEBP, include_dot: true);
|
||||||
$temp = new TemporaryFile(prefix: null, suffix: $ext);
|
$temp = new TemporaryFile(['prefix' => 'thumbnail', 'suffix' => $ext]);
|
||||||
$thumbnail = self::create(['attachment_id' => $attachment->getId()]);
|
$thumbnail = self::create(['attachment_id' => $attachment->getId()]);
|
||||||
$event_map = ['image' => 'ResizeImagePath', 'video' => 'ResizeVideoPath'];
|
$event_map = ['image' => 'ResizeImagePath', 'video' => 'ResizeVideoPath'];
|
||||||
$major_mime = GSFile::mimetypeMajor($attachment->getMimetype());
|
$major_mime = GSFile::mimetypeMajor($attachment->getMimetype());
|
||||||
@ -221,8 +220,7 @@ class AttachmentThumbnail extends Entity
|
|||||||
int $maxW,
|
int $maxW,
|
||||||
int $maxH,
|
int $maxH,
|
||||||
bool $crop
|
bool $crop
|
||||||
): array
|
): array {
|
||||||
{
|
|
||||||
// Cropping data (for original image size). Default values, 0 and null,
|
// Cropping data (for original image size). Default values, 0 and null,
|
||||||
// imply no cropping and with preserved aspect ratio (per axis).
|
// imply no cropping and with preserved aspect ratio (per axis).
|
||||||
$cx = 0; // crop x
|
$cx = 0; // crop x
|
||||||
@ -255,7 +253,7 @@ class AttachmentThumbnail extends Entity
|
|||||||
$rw = ceil($width * $rh / $height);
|
$rw = ceil($width * $rh / $height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return [(int)$rw, (int)$rh];
|
return [(int) $rw, (int) $rh];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function schemaDef(): array
|
public static function schemaDef(): array
|
||||||
|
@ -35,27 +35,25 @@ class TemporaryFile extends \SplFileInfo
|
|||||||
protected $resource;
|
protected $resource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param null|string $prefix The file name will begin with that prefix
|
* @param array $options - ['prefix' => ?string, 'suffix' => ?string, 'mode' => ?string, 'directory' => ?string]
|
||||||
* ("php" by default)
|
|
||||||
* @param null|string $mode File open mode ("w+b" by default)
|
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(array $options)
|
||||||
?string $prefix = null,
|
{
|
||||||
?string $suffix = null,
|
$attempts = 16;
|
||||||
?string $mode = null
|
for ($count = 0; $count < $attempts; ++$count) {
|
||||||
) {
|
$filename = uniqid(($options['directory'] ?? (sys_get_temp_dir() . '/')) . ($options['prefix'] ?? 'gs-php')) . ($options['suffix'] ?? '');
|
||||||
$filename = tempnam(sys_get_temp_dir(), $prefix ?? 'gs-php') . ($suffix ?? '');
|
|
||||||
|
|
||||||
if ($filename === false) {
|
$this->resource = @fopen($filename, $options['mode'] ?? 'w+b');
|
||||||
throw new TemporaryFileException('Could not create file: ' . $filename);
|
if ($this->resource !== false) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
parent::__construct($filename);
|
if ($count == $attempts) {
|
||||||
|
|
||||||
if (($this->resource = fopen($filename, $mode ?? 'w+b')) === false) {
|
|
||||||
$this->cleanup();
|
$this->cleanup();
|
||||||
throw new TemporaryFileException('Could not open file: ' . $filename);
|
throw new TemporaryFileException('Could not open file: ' . $filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parent::__construct($filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destruct()
|
public function __destruct()
|
||||||
@ -100,7 +98,7 @@ class TemporaryFile extends \SplFileInfo
|
|||||||
$path = $this->getRealPath();
|
$path = $this->getRealPath();
|
||||||
$this->close();
|
$this->close();
|
||||||
if (file_exists($path)) {
|
if (file_exists($path)) {
|
||||||
unlink($path);
|
@unlink($path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user