mirror of
https://github.com/WerySkok/nativegallery.git
synced 2024-12-23 09:01:03 +03:00
69 lines
2 KiB
PHP
69 lines
2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
class Upload
|
|
|
|
{
|
|
public $type;
|
|
public $src;
|
|
public $size;
|
|
public $name;
|
|
|
|
private static function human_filesize($bytes, $dec = 2): string
|
|
{
|
|
|
|
$size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
|
|
$factor = floor((strlen($bytes) - 1) / 3);
|
|
if ($factor == 0)
|
|
$dec = 0;
|
|
|
|
|
|
return sprintf("%.{$dec}f %s", $bytes / (1024 ** $factor), $size[$factor]);
|
|
}
|
|
public function __construct($file, $location)
|
|
{
|
|
$filecdn = bin2hex(openssl_random_pseudo_bytes(64, $cstrong)) . '.' . pathinfo($file['name'])['extension'];
|
|
$folder = $location . $filecdn;
|
|
$s3 = new \Aws\S3\S3Client([
|
|
'region' => NGALLERY['root']['storage']['s3']['credentials']['region'],
|
|
'version' => NGALLERY['root']['storage']['s3']['credentials']['version'],
|
|
'credentials' => [
|
|
'key' => NGALLERY['root']['storage']['s3']['credentials']['key'],
|
|
'secret' => NGALLERY['root']['storage']['s3']['credentials']['secret'],
|
|
],
|
|
'endpoint' => NGALLERY['root']['storage']['s3']['domains']['gateway'],
|
|
]);
|
|
$cstrong = True;
|
|
$result = $s3->putObject([
|
|
'Bucket' => NGALLERY['root']['storage']['s3']['credentials']['bucket'],
|
|
'Key' => $folder,
|
|
'SourceFile' => $file['tmp_name']
|
|
]);
|
|
$this->type = explode('/', $file['type'])[0];
|
|
$this->src = NGALLERY['root']['storage']['s3']['domains']['public'] . '/' . $location . $filecdn;
|
|
$this->size = self::human_filesize(filesize($file['tmp_name']));
|
|
$this->name = $file['name'];
|
|
}
|
|
public function getType()
|
|
{
|
|
return $this->type;
|
|
}
|
|
|
|
public function getSrc()
|
|
{
|
|
return $this->src;
|
|
}
|
|
|
|
public function getSize()
|
|
{
|
|
return $this->size;
|
|
}
|
|
|
|
public function getName()
|
|
{
|
|
return $this->name;
|
|
}
|
|
}
|
|
|