Last active
July 10, 2021 05:50
-
-
Save tzkmx/0ea5ca43894305bfac2be423774ff4c7 to your computer and use it in GitHub Desktop.
Custom Upload of Avatar in Nova
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// put this in AppServiceProvider::register to make the hash of the filename | |
// correspond to the SHA256 of the *CONTENTS* of the UploadedFile automatically | |
UploadedFile::macro('setHash', function () { | |
/** @var UploadedFile $this */ | |
$this->hashName = hash_file('sha256', | |
$this->getRealPath(), false); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// this is put in the fields of a resource | |
Image::make('Avatar', 'avatar_url') | |
->disk('azure') | |
->store(function (Request $request, UserModel $model) { | |
$corp = $model->corp; | |
$path = '/' . $corp->subdomain; | |
/** @var UploadedFile $attachment */ | |
$attachment = $request->avatar_url; | |
$attachment->setHash(); | |
$url = $attachment->store($path, 'azure'); | |
return [ | |
'avatar_url' => $url, | |
'avatar_size' => $attachment->getSize(), | |
'avatar_filename' => $attachment->getClientOriginalName(), | |
]; | |
}) | |
->download(function($req, $model, $disk, $value) { | |
return Storage::disk($disk)->download($value, $model->avatar_filename); | |
}) | |
// I had to put this on the Field declaration to avoid the previous uploaded file be deleted | |
->prunable(false) | |
->deletable(false), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment