rewrite Storage class to improve reusability
This commit is contained in:
parent
b137b88952
commit
ced8e30797
|
|
@ -127,7 +127,7 @@ class SkinlibController extends BaseController
|
||||||
$t->name = $_POST['name'];
|
$t->name = $_POST['name'];
|
||||||
$t->type = $_POST['type'];
|
$t->type = $_POST['type'];
|
||||||
$t->likes = 1;
|
$t->likes = 1;
|
||||||
$t->hash = \Storage::upload($_FILES['file']);
|
$t->hash = Utils::upload($_FILES['file']);
|
||||||
$t->size = ceil($_FILES['file']['size'] / 1024);
|
$t->size = ceil($_FILES['file']['size'] / 1024);
|
||||||
$t->public = ($_POST['public'] == 'true') ? "1" : "0";
|
$t->public = ($_POST['public'] == 'true') ? "1" : "0";
|
||||||
$t->uploader = $this->user->uid;
|
$t->uploader = $this->user->uid;
|
||||||
|
|
|
||||||
|
|
@ -123,7 +123,7 @@ class TextureController extends BaseController
|
||||||
|
|
||||||
if (\Storage::exist($fname)) {
|
if (\Storage::exist($fname)) {
|
||||||
header('Content-Type: image/png');
|
header('Content-Type: image/png');
|
||||||
echo \Storage::fread($fname);
|
echo \Storage::read($fname);
|
||||||
} else {
|
} else {
|
||||||
Http::abort(404, '请求的材质文件已经被删除');
|
Http::abort(404, '请求的材质文件已经被删除');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,7 @@ class Player
|
||||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->getLastModified()).' GMT');
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->getLastModified()).' GMT');
|
||||||
header('Content-Length: '.filesize($filename));
|
header('Content-Length: '.filesize($filename));
|
||||||
|
|
||||||
return \Storage::fread($filename);
|
return \Storage::read($filename);
|
||||||
} else {
|
} else {
|
||||||
\Http::abort(404, '请求的贴图已被删除。');
|
\Http::abort(404, '请求的贴图已被删除。');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,29 +4,19 @@ namespace App\Services;
|
||||||
|
|
||||||
class Storage
|
class Storage
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Rename uploaded file
|
|
||||||
*
|
|
||||||
* @param array $file, files uploaded via HTTP POST
|
|
||||||
* @return string $hash, sha256 hash of file
|
|
||||||
*/
|
|
||||||
public static function upload($file)
|
|
||||||
{
|
|
||||||
move_uploaded_file($file["tmp_name"], BASE_DIR."/textures/tmp.png");
|
|
||||||
$hash = hash_file('sha256', BASE_DIR."/textures/tmp.png");
|
|
||||||
rename(BASE_DIR."/textures/tmp.png", BASE_DIR."/textures/".$hash);
|
|
||||||
return $hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read a file and return bin data
|
* Read a file and return bin data
|
||||||
*
|
*
|
||||||
* @param string $filename
|
* @param string $filename
|
||||||
* @return resource, binary data
|
* @return string|bool
|
||||||
*/
|
*/
|
||||||
public static function fread($filename)
|
public static function read($filename)
|
||||||
{
|
{
|
||||||
return fread(fopen($filename, 'r'), filesize($filename));
|
$result = file_get_contents($filename, 'r');
|
||||||
|
if (false === $result) {
|
||||||
|
throw new \Exception("Failed to read $filename.");
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function exist($filename)
|
public static function exist($filename)
|
||||||
|
|
@ -34,6 +24,19 @@ class Storage
|
||||||
return file_exists($filename);
|
return file_exists($filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function hash($filename, $type = 'sha256')
|
||||||
|
{
|
||||||
|
return hash_file('sha256', $filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function rename($fname, $new_fname)
|
||||||
|
{
|
||||||
|
if (false === rename($fname, $new_fname)) {
|
||||||
|
throw new \Exception("Failed to rename $fname to $new_fname.");
|
||||||
|
}
|
||||||
|
return $new_fname;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a file
|
* Remove a file
|
||||||
*
|
*
|
||||||
|
|
@ -126,8 +129,10 @@ class Storage
|
||||||
*/
|
*/
|
||||||
public static function copyDir($source, $dest)
|
public static function copyDir($source, $dest)
|
||||||
{
|
{
|
||||||
if(!is_dir($source)) return false;
|
if(!is_dir($source))
|
||||||
if(!is_dir($dest)) mkdir($dest, 0777, true);
|
return false;
|
||||||
|
if(!is_dir($dest))
|
||||||
|
mkdir($dest, 0777, true);
|
||||||
|
|
||||||
$handle = dir($source);
|
$handle = dir($source);
|
||||||
|
|
||||||
|
|
@ -138,7 +143,6 @@ class Storage
|
||||||
self::copyDir($source.'/'.$entry, $dest.'/'.$entry);
|
self::copyDir($source.'/'.$entry, $dest.'/'.$entry);
|
||||||
} else {
|
} else {
|
||||||
@copy($source.'/'.$entry, $dest.'/'.$entry);
|
@copy($source.'/'.$entry, $dest.'/'.$entry);
|
||||||
// echo $source.'/'.$entry." => ".$dest.'/'.$entry."<br />";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,8 @@ class Utils
|
||||||
* @param string $string
|
* @param string $string
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function convertString($string) {
|
public static function convertString($string)
|
||||||
|
{
|
||||||
return addslashes(trim($string));
|
return addslashes(trim($string));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -24,17 +25,38 @@ class Utils
|
||||||
* @param string $default
|
* @param string $default
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function getValue($key, $array, $default = "") {
|
public static function getValue($key, $array, $default = "")
|
||||||
|
{
|
||||||
return array_key_exists($key, $array) ? $array[$key] : $default;
|
return array_key_exists($key, $array) ? $array[$key] : $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rename uploaded file
|
||||||
|
*
|
||||||
|
* @param array $file files uploaded via HTTP POST
|
||||||
|
* @return string $hash sha256 hash of file
|
||||||
|
*/
|
||||||
|
public static function upload($file)
|
||||||
|
{
|
||||||
|
$path = BASE_DIR.'/textures/tmp'.time();
|
||||||
|
|
||||||
|
if (false === move_uploaded_file($file['tmp_name'], $path)) {
|
||||||
|
throw new App\Exceptions\E('Failed to remove uploaded files, please check the permission', 1);
|
||||||
|
} else {
|
||||||
|
$hash = Storage::hash($path);
|
||||||
|
Storage::rename($path, BASE_DIR."/textures/$hash");
|
||||||
|
return $hash;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate random string
|
* Generate random string
|
||||||
*
|
*
|
||||||
* @param int $length
|
* @param int $length
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function generateRndString($length) {
|
public static function generateRndString($length)
|
||||||
|
{
|
||||||
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
|
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
|
||||||
$rnd_string = '';
|
$rnd_string = '';
|
||||||
for ($i = 0; $i < $length; $i++) {
|
for ($i = 0; $i < $length; $i++) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user