[UTILS][TemporaryFile] Change way TemporaryFile takes arguments and it's internal implementation

This commit is contained in:
Hugo Sales 2021-05-02 15:02:26 +00:00
parent 3b39046a38
commit 2b83a4b627
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
5 changed files with 34 additions and 38 deletions

View File

@ -92,7 +92,7 @@ class Avatar extends Controller
list(, $mimetype_user, , $encoding_user, $data_user) = $matches;
if ($encoding_user == 'base64') {
$data_user = base64_decode($data_user);
$tempfile = new TemporaryFile('avatar');
$tempfile = new TemporaryFile(['prefix' => 'avatar']);
$path = $tempfile->getRealPath();
file_put_contents($path, $data_user);
$sfile = new SymfonyFile($path);

View File

@ -77,7 +77,7 @@ class ImageEncoder extends Plugin
$type = self::preferredType();
$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);
if ($mimetype != $original_mimetype) {
// If title seems to be a filename with an extension

View File

@ -72,7 +72,7 @@ class VideoEncoder extends Plugin
// FFmpeg can't edit existing files in place,
// 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
// extension for PNG files outputs

View File

@ -100,18 +100,17 @@ class AttachmentThumbnail extends Entity
return $this->filename;
}
public function setModified(\DateTimeInterface $modified): self
public function setModified(DateTimeInterface $modified): self
{
$this->modified = $modified;
return $this;
}
public function getModified(): \DateTimeInterface
public function getModified(): DateTimeInterface
{
return $this->modified;
}
// }}} Autocode
private Attachment $attachment;
@ -140,7 +139,7 @@ class AttachmentThumbnail extends Entity
});
} catch (NotFoundException $e) {
$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()]);
$event_map = ['image' => 'ResizeImagePath', 'video' => 'ResizeVideoPath'];
$major_mime = GSFile::mimetypeMajor($attachment->getMimetype());
@ -221,8 +220,7 @@ class AttachmentThumbnail extends Entity
int $maxW,
int $maxH,
bool $crop
): array
{
): array {
// Cropping data (for original image size). Default values, 0 and null,
// imply no cropping and with preserved aspect ratio (per axis).
$cx = 0; // crop x
@ -255,7 +253,7 @@ class AttachmentThumbnail extends Entity
$rw = ceil($width * $rh / $height);
}
}
return [(int)$rw, (int)$rh];
return [(int) $rw, (int) $rh];
}
public static function schemaDef(): array

View File

@ -35,27 +35,25 @@ class TemporaryFile extends \SplFileInfo
protected $resource;
/**
* @param null|string $prefix The file name will begin with that prefix
* ("php" by default)
* @param null|string $mode File open mode ("w+b" by default)
* @param array $options - ['prefix' => ?string, 'suffix' => ?string, 'mode' => ?string, 'directory' => ?string]
*/
public function __construct(
?string $prefix = null,
?string $suffix = null,
?string $mode = null
) {
$filename = tempnam(sys_get_temp_dir(), $prefix ?? 'gs-php') . ($suffix ?? '');
public function __construct(array $options)
{
$attempts = 16;
for ($count = 0; $count < $attempts; ++$count) {
$filename = uniqid(($options['directory'] ?? (sys_get_temp_dir() . '/')) . ($options['prefix'] ?? 'gs-php')) . ($options['suffix'] ?? '');
if ($filename === false) {
throw new TemporaryFileException('Could not create file: ' . $filename);
$this->resource = @fopen($filename, $options['mode'] ?? 'w+b');
if ($this->resource !== false) {
break;
}
parent::__construct($filename);
if (($this->resource = fopen($filename, $mode ?? 'w+b')) === false) {
}
if ($count == $attempts) {
$this->cleanup();
throw new TemporaryFileException('Could not open file: ' . $filename);
}
parent::__construct($filename);
}
public function __destruct()
@ -100,7 +98,7 @@ class TemporaryFile extends \SplFileInfo
$path = $this->getRealPath();
$this->close();
if (file_exists($path)) {
unlink($path);
@unlink($path);
}
}