-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
271 additions
and
42 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
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
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,47 @@ | ||
<?php | ||
|
||
namespace FoF\Polls\Api\Controllers; | ||
|
||
use Flarum\Http\RequestUtil; | ||
use FoF\Polls\PollOption; | ||
use Illuminate\Contracts\Filesystem\Cloud; | ||
use Illuminate\Contracts\Filesystem\Factory; | ||
use Illuminate\Support\Arr; | ||
use Laminas\Diactoros\Response\EmptyResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
class DeletePollOptionImageController implements RequestHandlerInterface | ||
{ | ||
/** | ||
* @var Cloud | ||
*/ | ||
protected $uploadDir; | ||
|
||
public function __construct(Factory $filesystemFactory) | ||
{ | ||
$this->uploadDir = $filesystemFactory->disk('fof-polls'); | ||
} | ||
|
||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
$actor = RequestUtil::getActor($request); | ||
$optionId = Arr::get($request->getQueryParams(), 'optionId'); | ||
|
||
/** @var PollOption $option */ | ||
$option = PollOption::find($optionId); | ||
|
||
// if the image_url is a fully qualified URL, we just set it to null | ||
if (filter_var($option->image_url, FILTER_VALIDATE_URL)) { | ||
} else { | ||
// otherwise we check and delete it from the filesystem | ||
$this->uploadDir->delete($option->image_url); | ||
} | ||
|
||
$option->image_url = null; | ||
$option->save(); | ||
|
||
return new EmptyResponse(204); | ||
} | ||
} |
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,49 @@ | ||
<?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\PollOption; | ||
use Illuminate\Support\Arr; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class UploadPollOptionImageController extends UploadPollImageController | ||
{ | ||
protected $filenamePrefix = 'pollOptionImage'; | ||
|
||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
$actor = RequestUtil::getActor($request); | ||
$optionId = Arr::get($request->getQueryParams(), 'optionId'); | ||
|
||
if ($actor->cannot('startPoll') || $actor->cannot('startGlobalPoll')) { | ||
throw new PermissionDeniedException('You do not have permission to upload poll option images'); | ||
} | ||
|
||
$file = Arr::get($request->getUploadedFiles(), $this->filenamePrefix); | ||
|
||
$encodedImage = $this->makeImage($file); | ||
|
||
$uploadName = $uploadName = $this->uploadName(); | ||
|
||
$this->uploadDir->put($uploadName, $encodedImage); | ||
|
||
if ($optionId && $option = PollOption::find($optionId)) { | ||
$option->image_url = $uploadName; | ||
$option->save(); | ||
} | ||
|
||
return $this->jsonResponse($uploadName); | ||
} | ||
} |
Oops, something went wrong.