diff --git a/app/Events/HashingFile.php b/app/Events/HashingFile.php new file mode 100644 index 00000000..e3a31beb --- /dev/null +++ b/app/Events/HashingFile.php @@ -0,0 +1,21 @@ +file = $file; + } +} diff --git a/app/Http/Controllers/SkinlibController.php b/app/Http/Controllers/SkinlibController.php index 6740da72..6502b1d5 100644 --- a/app/Http/Controllers/SkinlibController.php +++ b/app/Http/Controllers/SkinlibController.php @@ -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'); diff --git a/app/Services/Utils.php b/app/Services/Utils.php index f0016d07..ebd8de10 100644 --- a/app/Services/Utils.php +++ b/app/Services/Utils.php @@ -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); diff --git a/app/helpers.php b/app/helpers.php index 43922edb..9840f3c8 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -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()