Skip to content

Commit

Permalink
Merge pull request #186 from HanashiDev/fileprocessor
Browse files Browse the repository at this point in the history
FileProcessorFormField
  • Loading branch information
Hanashi authored Oct 2, 2024
2 parents 77c0779 + 401db32 commit ca323e5
Show file tree
Hide file tree
Showing 16 changed files with 275 additions and 87 deletions.
11 changes: 11 additions & 0 deletions files/acp/database/install_dev.hanashi.wsc.discord-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

use wcf\system\database\table\column\BigintDatabaseTableColumn;
use wcf\system\database\table\column\BlobDatabaseTableColumn;
use wcf\system\database\table\column\IntDatabaseTableColumn;
use wcf\system\database\table\column\NotNullInt10DatabaseTableColumn;
use wcf\system\database\table\column\ObjectIdDatabaseTableColumn;
use wcf\system\database\table\column\TextDatabaseTableColumn;
use wcf\system\database\table\column\TinyintDatabaseTableColumn;
use wcf\system\database\table\column\VarcharDatabaseTableColumn;
use wcf\system\database\table\DatabaseTable;
use wcf\system\database\table\index\DatabaseTableForeignKey;
use wcf\system\database\table\index\DatabaseTablePrimaryIndex;
use wcf\system\database\table\PartialDatabaseTable;

Expand Down Expand Up @@ -55,10 +57,19 @@
VarcharDatabaseTableColumn::create('publicKey')
->length(100),
NotNullInt10DatabaseTableColumn::create('botTime'),
IntDatabaseTableColumn::create('webhookIconID')
->length(10),
])
->indices([
DatabaseTablePrimaryIndex::create()
->columns(['botID']),
])
->foreignKeys([
DatabaseTableForeignKey::create()
->columns(['webhookIconID'])
->referencedTable('wcf1_file')
->referencedColumns(['fileID'])
->onDelete('SET NULL'),
]),

// wcf1_discord_webhook
Expand Down
1 change: 0 additions & 1 deletion files/images/discord_webhook/.htaccess

This file was deleted.

11 changes: 5 additions & 6 deletions files/lib/acp/form/DiscordBotAddForm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use wcf\form\AbstractFormBuilderForm;
use wcf\system\discord\DiscordApi;
use wcf\system\form\builder\container\FormContainer;
use wcf\system\form\builder\field\FileProcessorFormField;
use wcf\system\form\builder\field\IntegerFormField;
use wcf\system\form\builder\field\PasswordFormField;
use wcf\system\form\builder\field\TextFormField;
use wcf\system\form\builder\field\UploadFormField;
use wcf\system\form\builder\field\validation\FormFieldValidationError;
use wcf\system\form\builder\field\validation\FormFieldValidator;

Expand Down Expand Up @@ -110,13 +110,12 @@ protected function createForm()
->required()
->maximumLength(50)
->value(PAGE_TITLE),
UploadFormField::create('webhookIcon')
FileProcessorFormField::create('webhookIconID')
->objectType('dev.hanashi.wsc.discord.webhook.avatar')
->label('wcf.acp.discordBotAdd.webhookIcon')
->description('wcf.acp.discordBotAdd.webhookIcon.description')
->maximum(1)
->imageOnly()
->maximumFilesize(8000000)
->setAcceptableFiles(['image/jpeg', 'image/png', 'image/gif']),
->singleFileUpload()
->bigPreview(),
]),
FormContainer::create('oauth2Settings')
->label('wcf.acp.discordBotAdd.oauth2Settings')
Expand Down
13 changes: 13 additions & 0 deletions files/lib/acp/form/DiscordBotEditForm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use wcf\data\discord\bot\DiscordBot;
use wcf\http\Helper;
use wcf\system\exception\IllegalLinkException;
use wcf\system\form\builder\field\FileProcessorFormField;

class DiscordBotEditForm extends DiscordBotAddForm
{
Expand Down Expand Up @@ -38,4 +39,16 @@ public function readParameters()
throw new IllegalLinkException();
}
}

#[Override]
protected function createForm()
{
parent::createForm();

$webhookIconFormField = $this->form->getNodeById('webhookIconID');
\assert($webhookIconFormField instanceof FileProcessorFormField);
$webhookIconFormField->context([
'botID' => $this->formObject->botID,
]);
}
}
9 changes: 9 additions & 0 deletions files/lib/bootstrap/dev.hanashi.wsc.discord-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use wcf\acp\page\DiscordWebhookListPage;
use wcf\event\acp\dashboard\box\PHPExtensionCollecting;
use wcf\event\acp\menu\item\ItemCollecting;
use wcf\event\worker\RebuildWorkerCollecting;
use wcf\system\event\EventHandler;
use wcf\system\menu\acp\AcpMenuItem;
use wcf\system\request\LinkHandler;
use wcf\system\style\FontAwesomeIcon;
use wcf\system\WCF;
use wcf\system\worker\DiscordWebhookAvatarRebuildDataWorker;

return static function (): void {
EventHandler::getInstance()->register(ItemCollecting::class, static function (ItemCollecting $event) {
Expand Down Expand Up @@ -68,4 +70,11 @@ static function (PHPExtensionCollecting $event) {
$event->register('sodium');
}
);

EventHandler::getInstance()->register(
RebuildWorkerCollecting::class,
static function (RebuildWorkerCollecting $event) {
$event->register(DiscordWebhookAvatarRebuildDataWorker::class, 0);
}
);
};
33 changes: 30 additions & 3 deletions files/lib/data/discord/bot/DiscordBot.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace wcf\data\discord\bot;

use wcf\data\DatabaseObject;
use wcf\data\file\File;
use wcf\system\cache\builder\DiscordGuildChannelCacheBuilder;
use wcf\system\discord\DiscordApi;

Expand All @@ -25,6 +26,7 @@
* @property-read string|null $clientSecret
* @property-read string|null $publicKey
* @property-read int $botTime
* @property-read int|null $webhookIconID
*/
final class DiscordBot extends DatabaseObject
{
Expand All @@ -40,6 +42,8 @@ final class DiscordBot extends DatabaseObject

protected DiscordApi $discordApi;

protected ?File $file;

public function getDiscordApi(): DiscordApi
{
if (!isset($this->discordApi)) {
Expand All @@ -53,9 +57,9 @@ public function getWebhookIconUploadFileLocations(): array
{
$files = [];

$filename = \sprintf('%simages/discord_webhook/%s.png', WCF_DIR, $this->botID);
if (\file_exists($filename)) {
$files[] = $filename;
if ($this->webhookIconID !== null) {
$file = new File($this->webhookIconID);
$files[] = $file->getPathname();
}

return $files;
Expand All @@ -68,4 +72,27 @@ public function getCachedDiscordChannel()
'botToken' => $this->botToken,
]);
}

public function getWebhookAvatar(): ?File
{
if ($this->webhookIconID === null) {
return null;
}

if (!isset($this->file)) {
$this->file = new File($this->webhookIconID);
}

return $this->file;
}

public function getWebhookAvatarData(): ?string
{
$file = $this->getWebhookAvatar();
if ($file === null) {
return null;
}

return 'data:' . $file->mimeType . ';base64,' . \base64_encode(\file_get_contents($file->getPathname()));
}
}
54 changes: 1 addition & 53 deletions files/lib/data/discord/bot/DiscordBotAction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use wcf\system\cache\builder\DiscordGuildChannelsCacheBuilder;
use wcf\system\exception\AJAXException;
use wcf\system\exception\PermissionDeniedException;
use wcf\system\file\upload\UploadFile;
use wcf\system\WCF;

/**
Expand Down Expand Up @@ -47,13 +46,7 @@ public function create()
unset($this->parameters['data']['useApplicationCommands']);
}

$discordBot = parent::create();

if (isset($this->parameters['webhookIcon']) && \is_array($this->parameters['webhookIcon'])) {
$this->processWebhookIcon($discordBot->botID);
}

return $discordBot;
return parent::create();
}

#[Override]
Expand All @@ -67,34 +60,6 @@ public function update()
}

parent::update();

foreach ($this->getObjects() as $object) {
if (isset($this->parameters['webhookIcon']) && \is_array($this->parameters['webhookIcon'])) {
if ($this->parameters['webhookIcon'] === '') {
$filename = \sprintf('%simages/discord_webhook/%s.png', WCF_DIR, $object->botID);
if (\file_exists($filename)) {
\unlink($filename);
}
} else {
$this->processWebhookIcon($object->botID);
}
}
}
}

#[Override]
public function delete()
{
$returnValues = parent::delete();

foreach ($this->getObjects() as $object) {
$filename = \sprintf('%simages/discord_webhook/%s.png', WCF_DIR, $object->botID);
if (\file_exists($filename)) {
\unlink($filename);
}
}

return $returnValues;
}

#[Override]
Expand All @@ -103,23 +68,6 @@ protected function resetCache()
DiscordGuildChannelsCacheBuilder::getInstance()->reset();
}

/**
* verarbeitet hochgeladenes Icon
*
* @param mixed $botID
* @return void
*/
protected function processWebhookIcon(int $botID)
{
$iconFile = \reset($this->parameters['webhookIcon']);
if ($iconFile instanceof UploadFile && !$iconFile->isProcessed()) {
$filename = \sprintf('%simages/discord_webhook/%s.png', WCF_DIR, $botID);

\rename($iconFile->getLocation(), $filename);
$iconFile->setProcessed($filename);
}
}

/**
* validiert die Methode getBotToken
*
Expand Down
8 changes: 1 addition & 7 deletions files/lib/system/discord/WebhookHandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use wcf\data\discord\webhook\DiscordWebhookList;
use wcf\system\cache\runtime\DiscordBotRuntimeCache;
use wcf\system\SingletonFactory;
use wcf\util\FileUtil;

final class WebhookHandler extends SingletonFactory
{
Expand All @@ -21,12 +20,7 @@ public function saveWebhooks(int $botID, array $channelIDs, string $usageBy)
}
$api = $bot->getDiscordApi();

$avatar = null;
$avatarFile = \sprintf('%simages/discord_webhook/%s.png', WCF_DIR, $botID);
if (\file_exists($avatarFile)) {
$mimeType = FileUtil::getMimeType($avatarFile);
$avatar = 'data:' . $mimeType . ';base64,' . \base64_encode(\file_get_contents($avatarFile));
}
$avatar = $bot->getWebhookAvatarData();

$list = new DiscordWebhookList();
$list->getConditionBuilder()->add('botID = ?', [$bot->botID]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use wcf\data\discord\webhook\DiscordWebhookAction;
use wcf\data\discord\webhook\DiscordWebhookList;
use wcf\system\exception\UserInputException;
use wcf\util\FileUtil;

class WebhookChannelMultiSelectDiscordType extends ChannelMultiSelectDiscordType
{
Expand Down Expand Up @@ -67,12 +66,7 @@ public function validate($newValue)

if (!isset($webhookChannelIDs[$botID]) || !\in_array($channelID, $webhookChannelIDs[$botID])) {
$discordApi = $discordBots[$botID]->getDiscordApi();
$avatar = null;
$avatarFile = \sprintf('%simages/discord_webhook/%s.png', WCF_DIR, $botID);
if (\file_exists($avatarFile)) {
$mimeType = FileUtil::getMimeType($avatarFile);
$avatar = 'data:' . $mimeType . ';base64,' . \base64_encode(\file_get_contents($avatarFile));
}
$avatar = $discordBots[$botID]->getWebhookAvatarData();
$response = $discordApi->createWebhook($channelID, $discordBots[$botID]->webhookName, $avatar);
if (!$response['error']) {
$action = new DiscordWebhookAction([], 'create', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use wcf\data\discord\webhook\DiscordWebhookAction;
use wcf\data\discord\webhook\DiscordWebhookList;
use wcf\system\exception\UserInputException;
use wcf\util\FileUtil;

class WebhookChannelSelectDiscordType extends ChannelSelectDiscordType
{
Expand Down Expand Up @@ -63,12 +62,7 @@ public function validate($newValue, ?int $maxChannels = null)

if (!isset($webhookChannelIDs[$botID]) || !\in_array($channelID, $webhookChannelIDs[$botID])) {
$discordApi = $discordBots[$botID]->getDiscordApi();
$avatar = null;
$avatarFile = \sprintf('%simages/discord_webhook/%s.png', WCF_DIR, $botID);
if (\file_exists($avatarFile)) {
$mimeType = FileUtil::getMimeType($avatarFile);
$avatar = 'data:' . $mimeType . ';base64,' . \base64_encode(\file_get_contents($avatarFile));
}
$avatar = $discordBots[$botID]->getWebhookAvatarData();
$response = $discordApi->createWebhook($channelID, $discordBots[$botID]->webhookName, $avatar);
if (!$response['error']) {
$action = new DiscordWebhookAction([], 'create', [
Expand Down
Loading

0 comments on commit ca323e5

Please sign in to comment.