Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Last active July 10, 2021 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzkmx/0ea5ca43894305bfac2be423774ff4c7 to your computer and use it in GitHub Desktop.
Save tzkmx/0ea5ca43894305bfac2be423774ff4c7 to your computer and use it in GitHub Desktop.
Custom Upload of Avatar in Nova
<?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);
});
<?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