mirror of
https://github.com/bs-community/blessing-skin-server.git
synced 2024-12-09 06:00:31 +08:00
Refine hashing uploaded file
Remove Utils::upload method. Add bs_hash_file helper function. Add HashingFile event.
This commit is contained in:
parent
3d9478a75c
commit
40485253ec
21
app/Events/HashingFile.php
Normal file
21
app/Events/HashingFile.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use \Illuminate\Http\UploadedFile;
|
||||
|
||||
class HashingFile extends Event
|
||||
{
|
||||
public $file;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @param UploadedFile $file
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(UploadedFile $file)
|
||||
{
|
||||
$this->file = $file;
|
||||
}
|
||||
}
|
@ -163,7 +163,7 @@ class SkinlibController extends Controller
|
||||
$t->name = $request->input('name');
|
||||
$t->type = $request->input('type');
|
||||
$t->likes = 1;
|
||||
$t->hash = Utils::upload($request->file('file'));
|
||||
$t->hash = bs_hash_file($request->file('file'));
|
||||
$t->size = ceil($request->file('file')->getSize() / 1024);
|
||||
$t->public = ($request->input('public') == 'true') ? "1" : "0";
|
||||
$t->uploader = $this->user->uid;
|
||||
@ -189,6 +189,10 @@ class SkinlibController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
if (! Storage::disk('textures')->exists($t->hash)) {
|
||||
Storage::disk('textures')->put($t->hash, file_get_contents($request->file('file')));
|
||||
}
|
||||
|
||||
$t->save();
|
||||
|
||||
$this->user->setScore($cost, 'minus');
|
||||
|
@ -117,28 +117,6 @@ class Utils
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename uploaded file
|
||||
*
|
||||
* @param \Illuminate\Http\UploadedFile $file The Files uploaded via HTTP POST
|
||||
* @return string $hash The sha256 hash of file
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function upload($file)
|
||||
{
|
||||
$hash = hash_file('sha256', $file);
|
||||
try {
|
||||
$storage = Storage::disk('textures');
|
||||
if (! $storage->exists($hash)) {
|
||||
$storage->put($hash, file_get_contents($file));
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::warning("Failed to upload file {$file->getFilename()}");
|
||||
throw new \Exception("Failed to upload file {$file->getFilename()}");
|
||||
}
|
||||
return $hash;
|
||||
}
|
||||
|
||||
public static function download($url, $path)
|
||||
{
|
||||
@set_time_limit(0);
|
||||
|
@ -96,6 +96,21 @@ if (! function_exists('json')) {
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('bs_hash_file')) {
|
||||
|
||||
function bs_hash_file(Illuminate\Http\UploadedFile $file)
|
||||
{
|
||||
// Try to get hash from event listener
|
||||
$responses = event(new App\Events\HashingFile($file));
|
||||
if (isset($responses[0]) && is_string($responses[0])) {
|
||||
return $responses[0];
|
||||
}
|
||||
|
||||
// Default to sha256 hash
|
||||
return hash_file('sha256', $file);
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('bs_footer_extra')) {
|
||||
|
||||
function bs_footer_extra()
|
||||
|
Loading…
Reference in New Issue
Block a user