mirror of
https://github.com/bs-community/blessing-skin-server.git
synced 2024-12-15 06:09:58 +08:00
83 lines
2.2 KiB
PHP
83 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Exceptions\PrettyPageException;
|
|
use Storage;
|
|
use Log;
|
|
|
|
class Utils
|
|
{
|
|
/**
|
|
* Rename uploaded file
|
|
*
|
|
* @param array $file files uploaded via HTTP POST
|
|
* @return string $hash sha256 hash of file
|
|
*/
|
|
public static function upload($file)
|
|
{
|
|
$path = 'tmp'.time();
|
|
$absolute_path = BASE_DIR."/storage/textures/$path";
|
|
|
|
try {
|
|
if (false === move_uploaded_file($file['tmp_name'], $absolute_path)) {
|
|
throw new Exception('Failed to remove uploaded files, please check the permission', 1);
|
|
}
|
|
} catch (\Exception $e) {
|
|
Log::warning("Failed to move uploaded file, $e");
|
|
} finally {
|
|
if (file_exists($absolute_path)) {
|
|
$hash = hash_file('sha256', $absolute_path);
|
|
|
|
if (!Storage::disk('textures')->has($hash)) {
|
|
Storage::disk('textures')->move($path, $hash);
|
|
}
|
|
|
|
return $hash;
|
|
} else {
|
|
Log::warning("Failed to upload file $path");
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Generate random string
|
|
*
|
|
* @param int $length
|
|
* @param bool $special_chars
|
|
* @return string
|
|
*/
|
|
public static function generateRndString($length, $special_chars = true)
|
|
{
|
|
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
if ($special_chars) $chars .= "!@#$%^&*()-_ []{}<>~`+=,.;:/?|";
|
|
|
|
$rnd_string = '';
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$rnd_string .= $chars[mt_rand(0, strlen($chars) - 1)];
|
|
}
|
|
return $rnd_string;
|
|
}
|
|
|
|
public static function getTimeFormatted($timestamp = 0)
|
|
{
|
|
return ($timestamp == 0) ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s', $timestamp);
|
|
}
|
|
|
|
/**
|
|
* Replace content of string according to given rules
|
|
*
|
|
* @param string $str
|
|
* @param array $rules
|
|
* @return string
|
|
*/
|
|
public static function getStringReplaced($str, $rules)
|
|
{
|
|
foreach ($rules as $search => $replace) {
|
|
$str = str_replace($search, $replace, $str);
|
|
}
|
|
return $str;
|
|
}
|
|
|
|
}
|