-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'im/global-polls' into im/v2.2-alpha
# Conflicts: # js/dist/forum.js # js/dist/forum.js.map
- Loading branch information
Showing
17 changed files
with
383 additions
and
107 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
import type Mithril from 'mithril'; | ||
import Component from 'flarum/common/Component'; | ||
import Component, { ComponentAttrs } from 'flarum/common/Component'; | ||
|
||
export default class PollImage extends Component { | ||
interface PollImageAttrs extends ComponentAttrs { | ||
imageUrl: string; | ||
alt: string; | ||
} | ||
|
||
export default class PollImage extends Component<PollImageAttrs> { | ||
view(): Mithril.Children { | ||
return; | ||
return ( | ||
<div className="PollImage"> | ||
<img src={this.attrs.imageUrl} alt={this.attrs.alt} className="PollImage-image" /> | ||
</div> | ||
); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
14 changes: 14 additions & 0 deletions
14
migrations/2024_03_06_000000_modify_polls_rename_image_url_to_image.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/polls. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Flarum\Database\Migration; | ||
|
||
return Migration::renameColumn('polls', 'image_url', 'image'); |
26 changes: 26 additions & 0 deletions
26
migrations/2024_03_06_000001_create_polls_image_alt_column.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/polls. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Schema\Builder; | ||
|
||
return [ | ||
'up' => function (Builder $schema) { | ||
$schema->table('polls', function (Blueprint $table) { | ||
$table->string('image_alt')->nullable()->after('image'); | ||
}); | ||
}, | ||
'down' => function (Builder $schema) { | ||
$schema->table('polls', function (Blueprint $table) { | ||
$table->dropColumn('image_alt'); | ||
}); | ||
}, | ||
]; |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/polls. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FoF\Polls\Api\Controllers; | ||
|
||
use Flarum\Http\RequestUtil; | ||
use Flarum\User\Exception\PermissionDeniedException; | ||
use FoF\Polls\Poll; | ||
use Illuminate\Contracts\Filesystem\Cloud; | ||
use Illuminate\Contracts\Filesystem\Factory; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
use Intervention\Image\Constraint; | ||
use Intervention\Image\Image; | ||
use Intervention\Image\ImageManager; | ||
use Laminas\Diactoros\Response\JsonResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\UploadedFileInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
class UploadPollImageController implements RequestHandlerInterface | ||
{ | ||
protected $filenamePrefix = 'pollImage'; | ||
|
||
/** | ||
* @var Cloud | ||
*/ | ||
protected $uploadDir; | ||
|
||
/** | ||
* @var ImageManager | ||
*/ | ||
protected $imageManager; | ||
|
||
public function __construct( | ||
Factory $filesystemFactory, | ||
ImageManager $imageManager | ||
) { | ||
$this->imageManager = $imageManager; | ||
$this->uploadDir = $filesystemFactory->disk('fof-polls'); | ||
} | ||
|
||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
$actor = RequestUtil::getActor($request); | ||
$pollId = Arr::get($request->getQueryParams(), 'pollId'); | ||
|
||
if ($actor->cannot('startPoll') || $actor->cannot('startGlobalPoll')) { | ||
throw new PermissionDeniedException('You do not have permission to upload poll images'); | ||
} | ||
|
||
$file = Arr::get($request->getUploadedFiles(), $this->filenamePrefix); | ||
|
||
$encodedImage = $this->makeImage($file); | ||
|
||
$uploadName = $this->filenamePrefix.'-'.Str::lower(Str::random(8)).'.png'; | ||
|
||
$this->uploadDir->put($uploadName, $encodedImage); | ||
|
||
if ($pollId && $poll = Poll::find($pollId)) { | ||
$poll->image = $uploadName; | ||
$poll->save(); | ||
} | ||
|
||
return new JsonResponse([ | ||
'fileUrl' => $this->uploadDir->url($uploadName), | ||
'fileName' => $uploadName, | ||
]); | ||
} | ||
|
||
protected function makeImage(UploadedFileInterface $file): Image | ||
{ | ||
$encodedImage = $this->imageManager->make($file->getStream()->getMetadata('uri'))->resize(250, 250, function (Constraint $constraint) { | ||
$constraint->aspectRatio(); | ||
$constraint->upsize(); | ||
})->encode('png'); | ||
|
||
return $encodedImage; | ||
} | ||
} |
Oops, something went wrong.