Skip to content

Commit 06a5be9

Browse files
gthouretmarkharding
authored andcommitted
(chore) Add memory efficient method of getting mime type from a buffer and replace all calls to finfo - #1120
1 parent d562c2a commit 06a5be9

File tree

9 files changed

+49
-84
lines changed

9 files changed

+49
-84
lines changed

Controllers/fs/v1/avatars.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Minds\Core;
1111
use Minds\Entities;
1212
use Minds\Interfaces;
13-
use Minds\Api\Factory;
13+
use Minds\Helpers\File;
1414

1515
class avatars implements Interfaces\FS
1616
{
@@ -59,10 +59,8 @@ public function get($pages)
5959
$contents = file_get_contents($filepath);
6060
}
6161

62-
if ($filepath) {
63-
$finfo = finfo_open(FILEINFO_MIME);
64-
$mimetype = finfo_file($finfo, $filepath);
65-
finfo_close($finfo);
62+
if (!empty($contents)) {
63+
$mimetype = File::getMime($contents);
6664
} else {
6765
$mimetype = 'image/jpeg';
6866
}

Controllers/fs/v1/banners.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Minds\Core;
1111
use Minds\Entities;
1212
use Minds\Interfaces;
13-
use Minds\Api\Factory;
13+
use Minds\Helpers\File;
1414

1515
class banners implements Interfaces\FS
1616
{
@@ -102,9 +102,7 @@ public function get($pages)
102102
}
103103
}
104104

105-
$finfo = finfo_open(FILEINFO_MIME);
106-
$mimetype = finfo_buffer($finfo, $content);
107-
finfo_close($finfo);
105+
$mimetype = File::getMime($content);
108106

109107
header('Content-Type: '.$mimetype);
110108
header('Expires: ' . date('r', time() + 864000));

Controllers/fs/v1/paywall.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Minds\Core;
1111
use Minds\Entities;
1212
use Minds\Interfaces;
13-
use Minds\Api\Factory;
13+
use Minds\Helpers\File;
1414

1515
class paywall implements Interfaces\FS
1616
{
@@ -31,9 +31,7 @@ public function get($pages)
3131
$contents = file_get_contents(Core\Di\Di::_()->get('Config')->get('path') . 'engine/Assets/photos/andromeda-galaxy.jpg');
3232
}
3333

34-
$finfo = finfo_open(FILEINFO_MIME);
35-
$mimetype = finfo_file($finfo, $filepath);
36-
finfo_close($finfo);
34+
$mimetype = File::getMime($contents);
3735
header('Content-Type: '.$mimetype);
3836
header('Expires: ' . date('r', time() + 864000));
3937
header("Pragma: public");

Controllers/fs/v1/thumbnail.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use Minds\Core;
88
use Minds\Core\Di\Di;
9-
use Minds\Entities;
109
use Minds\Interfaces;
1110
use Minds\Core\Features\Manager as FeaturesManager;
11+
use Minds\Helpers\File;
1212

1313
class thumbnail extends Core\page implements Interfaces\page
1414
{
@@ -47,8 +47,7 @@ public function get($pages)
4747
}
4848

4949
try {
50-
$finfo = new \finfo(FILEINFO_MIME);
51-
$contentType = $finfo->buffer($contents) ?: 'image/jpeg';
50+
$contentType = File::getMime($contents);
5251
} catch (\Exception $e) {
5352
error_log($e);
5453
$contentType = 'image/jpeg';

Core/Media/Assets/Image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function validate(array $media)
2020

2121
public function upload(array $media, array $data)
2222
{
23-
$filename = "/image/{$this->entity->batch_guid}/{$this->entity->guid}/master.jpg";
23+
$filename = "image/{$this->entity->batch_guid}/{$this->entity->guid}/master.jpg";
2424

2525
// @note: legacy file handling
2626
$file = new \ElggFile();

Core/Media/Proxy/Download.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Minds\Core\Media\Proxy;
1010

1111
use Minds\Core\Di\Di;
12+
use Minds\Helpers\File;
1213
use Minds\Core\Http\Curl\Client;
1314
use Minds\Traits\MagicAttributes;
1415

@@ -99,8 +100,7 @@ public function downloadBinaryString()
99100
throw new \Exception('Invalid image');
100101
}
101102

102-
$finfo = new \finfo(FILEINFO_MIME);
103-
$mime = $finfo->buffer($content);
103+
$mime = File::getMime($content);
104104

105105
if (!$mime) {
106106
throw new \Exception('Cannot read image MIME');

Core/Storage/Services/S3.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
use Aws\S3\S3Client;
66
use Minds\Core\Config;
77
use Minds\Core\Di\Di;
8+
use Minds\Helpers\File;
89

910
class S3 implements ServiceInterface
1011
{
12+
/** @var S3Client */
1113
public $s3;
1214
public $filepath;
1315
public $mode;
@@ -61,27 +63,17 @@ public function close()
6163

6264
public function write($data)
6365
{
64-
65-
//TODO: check mime performance here
66-
$finfo = new \finfo(FILEINFO_MIME_TYPE);
67-
$mimeType = $finfo->buffer($data);
66+
$mimeType = File::getMimeType($data);
6867

6968
$write = $this->s3->putObject([
7069
// 'ACL' => 'public-read',
7170
'Bucket' => Config::_()->aws['bucket'],
7271
'Key' => $this->filepath,
7372
'ContentType' => $mimeType,
7473
'ContentLength' => strlen($data),
75-
//'ContentLength' => filesize($file),
7674
'Body' => $data,
7775
]);
7876

79-
//also write to disk until full migration
80-
/*$disk = new Disk();
81-
$disk->open($this->filepath, 'write');
82-
$disk->write($data);
83-
$disk->close();*/
84-
8577
return true;
8678
}
8779

@@ -104,18 +96,7 @@ public function read($length = 0)
10496
break;
10597
case "redirect":
10698
default:
107-
//for now, check if the file exists, and fallback to disk if not!
108-
/*if (!$this->s3->doesObjectExist(Config::_()->aws['bucket'], $this->filepath)) {
109-
$disk = new Disk();
110-
$disk->open($this->filepath, 'read');
111-
$content = $disk->read();
112-
$disk->close();
113-
return $content;
114-
}*/
115-
11699
$url = $this->s3->getObjectUrl(Config::_()->aws['bucket'], $this->filepath, "+15 minutes");
117-
//$this->filepath = str_replace('//', '/', $this->filepath);
118-
//$url = Config::_()->aws['cloudfront'] . $this->filepath;
119100
header("Location: $url");
120101
exit;
121102
}

Helpers/File.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Minds\Helpers;
4+
5+
class File
6+
{
7+
const HEADER_LENGTH = 16;
8+
9+
public static function getMimeType(&$data): string
10+
{
11+
return self::getType($data, FILEINFO_MIME_TYPE);
12+
}
13+
14+
public static function getMime(&$data): string
15+
{
16+
return self::getType($data, FILEINFO_MIME);
17+
}
18+
19+
protected static function getType(&$data, int $type): string
20+
{
21+
$header = substr($data, 0, self::HEADER_LENGTH);
22+
$finfo = new \finfo($type);
23+
$type = $finfo->buffer($header);
24+
unset($finfo);
25+
return $type;
26+
}
27+
}

classes/ElggFile.php

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -121,49 +121,6 @@ public function setMimeType($mimetype) {
121121
return $this->mimetype = $mimetype;
122122
}
123123

124-
/**
125-
* Detects mime types based on filename or actual file.
126-
*
127-
* @param mixed $file The full path of the file to check. For uploaded files, use tmp_name.
128-
* @param mixed $default A default. Useful to pass what the browser thinks it is.
129-
* @since 1.7.12
130-
*
131-
* @note If $file is provided, this may be called statically
132-
*
133-
* @return mixed Detected type on success, false on failure.
134-
*/
135-
public function detectMimeType($file = null, $default = null) {
136-
if (!$file) {
137-
if (isset($this) && $this->filename) {
138-
$file = $this->filename;
139-
} else {
140-
return false;
141-
}
142-
}
143-
144-
$mime = false;
145-
146-
// for PHP5 folks.
147-
if (function_exists('finfo_file') && defined('FILEINFO_MIME_TYPE')) {
148-
$resource = finfo_open(FILEINFO_MIME_TYPE);
149-
if ($resource) {
150-
$mime = finfo_file($resource, $file);
151-
}
152-
}
153-
154-
// for everyone else.
155-
if (!$mime && function_exists('mime_content_type')) {
156-
$mime = mime_content_type($file);
157-
}
158-
159-
// default
160-
if (!$mime) {
161-
return $default;
162-
}
163-
164-
return $mime;
165-
}
166-
167124
/**
168125
* Set the optional file description.
169126
*
@@ -363,4 +320,11 @@ protected function getFilestore() {
363320
return $this->filestore;
364321
}
365322

323+
/**
324+
* Executed prior to object serialization
325+
*/
326+
public function __sleep()
327+
{
328+
unset($this->handle);
329+
}
366330
}

0 commit comments

Comments
 (0)