forked from GNUsocial/gnu-social
[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;
|
||||||
@ -139,10 +138,10 @@ class AttachmentThumbnail extends Entity
|
|||||||
return DB::findOneBy('attachment_thumbnail', ['attachment_id' => $attachment->getId(), 'width' => $predicted_width, 'height' => $predicted_height]);
|
return DB::findOneBy('attachment_thumbnail', ['attachment_id' => $attachment->getId(), 'width' => $predicted_width, 'height' => $predicted_height]);
|
||||||
});
|
});
|
||||||
} 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());
|
||||||
if (in_array($major_mime, array_keys($event_map))) {
|
if (in_array($major_mime, array_keys($event_map))) {
|
||||||
Event::handle($event_map[$major_mime], [$attachment->getPath(), $temp->getRealPath(), &$width, &$height, $crop, &$mimetype]);
|
Event::handle($event_map[$major_mime], [$attachment->getPath(), $temp->getRealPath(), &$width, &$height, $crop, &$mimetype]);
|
||||||
@ -178,8 +177,8 @@ class AttachmentThumbnail extends Entity
|
|||||||
{
|
{
|
||||||
$attrs = [
|
$attrs = [
|
||||||
'height' => $this->getHeight(),
|
'height' => $this->getHeight(),
|
||||||
'width' => $this->getWidth(),
|
'width' => $this->getWidth(),
|
||||||
'src' => $this->getUrl(),
|
'src' => $this->getUrl(),
|
||||||
];
|
];
|
||||||
return $overwrite ? array_merge($orig, $attrs) : array_merge($attrs, $orig);
|
return $overwrite ? array_merge($orig, $attrs) : array_merge($attrs, $orig);
|
||||||
}
|
}
|
||||||
@ -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
|
||||||
@ -232,7 +230,7 @@ class AttachmentThumbnail extends Entity
|
|||||||
|
|
||||||
if ($crop) {
|
if ($crop) {
|
||||||
$s_ar = $width / $height;
|
$s_ar = $width / $height;
|
||||||
$t_ar = $maxW / $maxH;
|
$t_ar = $maxW / $maxH;
|
||||||
|
|
||||||
$rw = $maxW;
|
$rw = $maxW;
|
||||||
$rh = $maxH;
|
$rh = $maxH;
|
||||||
@ -255,22 +253,22 @@ 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
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'name' => 'attachment_thumbnail',
|
'name' => 'attachment_thumbnail',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'attachment_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Attachment.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'thumbnail for what attachment'],
|
'attachment_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Attachment.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'thumbnail for what attachment'],
|
||||||
'width' => ['type' => 'int', 'not null' => true, 'description' => 'width of thumbnail'],
|
'width' => ['type' => 'int', 'not null' => true, 'description' => 'width of thumbnail'],
|
||||||
'height' => ['type' => 'int', 'not null' => true, 'description' => 'height of thumbnail'],
|
'height' => ['type' => 'int', 'not null' => true, 'description' => 'height of thumbnail'],
|
||||||
'filename' => ['type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'thubmnail filename'],
|
'filename' => ['type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'thubmnail filename'],
|
||||||
'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
|
'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
|
||||||
],
|
],
|
||||||
'primary key' => ['attachment_id', 'width', 'height'],
|
'primary key' => ['attachment_id', 'width', 'height'],
|
||||||
'indexes' => [
|
'indexes' => [
|
||||||
'attachment_thumbnail_attachment_id_idx' => ['attachment_id'],
|
'attachment_thumbnail_attachment_id_idx' => ['attachment_id'],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if ($count == $attempts) {
|
||||||
parent::__construct($filename);
|
|
||||||
|
|
||||||
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