Skip to content

Commit

Permalink
Fix internal server error after uploading svg avatar, fixes OpenDomin…
Browse files Browse the repository at this point in the history
  • Loading branch information
WaveHack committed May 16, 2018
1 parent 511e3af commit fb9b148
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
3 changes: 2 additions & 1 deletion app/resources/views/pages/settings.blade.php
Expand Up @@ -66,7 +66,8 @@

<span class="new-avatar-filename" style="padding-left: 8px;"></span>

<p class="help-block">Uploaded avatars will be cropped/resized to 200x200 pixels and converted to png. Upload a square image for best results.</p>
<p class="help-block">Uploaded avatars will be cropped/resized to 200x200 pixels and converted to PNG. Upload a square image for best results.</p>
<p class="help-block">Supported formats are JPG, PNG, WebP and non-animated GIF.</p>
</div>
</div>

Expand Down
19 changes: 15 additions & 4 deletions src/Http/Controllers/SettingsController.php
Expand Up @@ -7,13 +7,16 @@
use Illuminate\Http\UploadedFile;
use Image;
use OpenDominion\Helpers\NotificationHelper;
use OpenDominion\Models\User;
use RuntimeException;
use Storage;
use Throwable;

class SettingsController extends AbstractController
{
public function getIndex()
{
/** @var User $user */
$user = Auth::user();

/** @var NotificationHelper $notificationHelper */
Expand All @@ -29,10 +32,14 @@ public function getIndex()

public function postIndex(Request $request)
{
$user = Auth::user();

if ($newAvatar = $request->file('account_avatar')) {
$this->handleAvatarUpload($newAvatar);
try {
$this->handleAvatarUpload($newAvatar);

} catch (Throwable $e) {
$request->session()->flash('alert-danger', $e->getMessage());
return redirect()->back();
}
}

$this->updateNotifications($request->input());
Expand All @@ -44,6 +51,7 @@ public function postIndex(Request $request)

protected function handleAvatarUpload(UploadedFile $file)
{
/** @var User $user */
$user = Auth::user();

// Convert image
Expand All @@ -57,6 +65,7 @@ protected function handleAvatarUpload(UploadedFile $file)

if (!Storage::disk('public')->put(($path . '/' . $fileName), $data)) {
throw new RuntimeException('Failed to upload avatar');
// todo: notify bugsnag
}

$user->avatar = $fileName;
Expand All @@ -69,14 +78,15 @@ protected function updateNotifications(array $data)
return;
}

/** @var User $user */
$user = Auth::user();

$newNotifications = [];

foreach ($data['notifications'] as $key => $types) {
foreach ($types as $type => $channels) {
foreach ($channels as $channel => $enabled) {
if ($enabled == 'on') {
if ($enabled === 'on') {
array_set($newNotifications, "{$key}.{$type}.{$channel}", true);
}
}
Expand All @@ -92,6 +102,7 @@ protected function updateNotifications(array $data)

protected function updateNotificationSettings(array $data)
{
/** @var User $user */
$user = Auth::user();

$settings = ($user->settings ?? []);
Expand Down

0 comments on commit fb9b148

Please sign in to comment.