-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: prepare AWS S3 upload for avatars #1747
Changes from 3 commits
ea8d946
d27ea90
3be774b
c7af31b
c13517d
5bbf1d1
4016759
1bbeffc
4177c65
2a72c69
21257b1
a27eca6
680fd4e
53fdda8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use Illuminate\Contracts\Filesystem\FileNotFoundException; | ||
use App\Http\Resources\Address\AddressShort as AddressShortResource; | ||
use App\Http\Resources\Contact\ContactShort as ContactShortResource; | ||
use App\Http\Resources\ContactField\ContactField as ContactFieldResource; | ||
|
@@ -937,14 +938,50 @@ public function getAvatarURL($size = 110) | |
return $this->avatar_external_url; | ||
} | ||
|
||
$original_avatar_url = Storage::disk($this->avatar_location)->url($this->avatar_file_name); | ||
$original_avatar_url = $this->avatar_file_name; | ||
$avatar_filename = pathinfo($original_avatar_url, PATHINFO_FILENAME); | ||
$avatar_extension = pathinfo($original_avatar_url, PATHINFO_EXTENSION); | ||
$resized_avatar = 'avatars/'.$avatar_filename.'_'.$size.'.'.$avatar_extension; | ||
|
||
return asset(Storage::disk($this->avatar_location)->url($resized_avatar)); | ||
} | ||
|
||
/** | ||
* Delete avatars files. | ||
*/ | ||
public function deleteAvatars() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why we need to create a function in Contact object just for this, especially if this process will only happen once for this migration. Is it really useful? Can we move this function elsewhere ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When a contact already has an avatar (not gravatar), if you update the image, today the old image stays in the store forever. I'm not sure it's a voluntary behavior. I've created this function to remove the old image, but we can imagine an other behavior... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And today a user can flood us by uploading as much as images he can, and they will stay in the store... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is true. Thanks for the explanation. The idea: have a class called, for instance, What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After a bit reflexion: this does not apply here, even I think Services are great, but it's used for Controller, to handle request the same way for htpp and api. In this case, I already have the $contact object, and I just pass it to the Service: there is no interest. |
||
{ | ||
if ($this->avatar_location == 'external') { | ||
return; | ||
} | ||
|
||
$this->deleteAvatarSize(); | ||
$this->deleteAvatarSize(110); | ||
$this->deleteAvatarSize(174); | ||
} | ||
|
||
/** | ||
* Delete avatar file for one size. | ||
*/ | ||
private function deleteAvatarSize($size = null) | ||
{ | ||
$avatar_file_name = $this->avatar_file_name; | ||
if (! is_null($size)) { | ||
$filename = pathinfo($avatar_file_name, PATHINFO_FILENAME); | ||
$extension = pathinfo($avatar_file_name, PATHINFO_EXTENSION); | ||
$avatar_file_name = 'avatars/'.$filename.'_'.$size.'.'.$extension; | ||
} | ||
|
||
try { | ||
$storage = Storage::disk($this->avatar_location); | ||
if ($storage->exists($avatar_file_name)) { | ||
$storage->delete($avatar_file_name); | ||
} | ||
} catch (FileNotFoundException $e) { | ||
return; | ||
} | ||
} | ||
|
||
/** | ||
* Returns the source of the avatar, or null if avatar is undefined. | ||
*/ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PSR4