Files
nc_bot_webhooks/lib/Cron/ImageCleanup.php
T
kyle 9d569453de fix(webhooks): improve Nextcloud 33 compatibility and HA REST notify payload handling
- Replace getFolder() with get() and fallback creation to fix LazyFolder compatibility in NC33
- Add support for unwrapping nested 'data' keys in Home Assistant REST notify payloads
- Implement fallback to $_GET when request body is empty
- Refactor buildRichObject to use direct PDO queries for filecache and share creation, bypassing lazy node resolution
- Generate timestamped filenames for uploaded images to prevent overwrites
- Add comprehensive logging for webhook payloads, image processing, and filecache verification
- Update IShareManager imports and usage to align with Nextcloud API changes
- Improve image attachment handling to support URLs, base64, and file paths
- Wrap image processing in try-catch to prevent webhook crashes on image failures
2026-06-13 14:25:22 -07:00

60 lines
1.6 KiB
PHP

<?php
namespace OCA\Ncbotwebhooks\Cron;
use OCA\Ncbotwebhooks\Service\TalkService;
use OCP\BackgroundJob\IJob;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;
class ImageCleanup implements IJob {
private TalkService $talkService;
private IRootFolder $rootFolder;
private IUserManager $userManager;
private LoggerInterface $logger;
public function __construct(
TalkService $talkService,
IRootFolder $rootFolder,
IUserManager $userManager,
LoggerInterface $logger,
) {
$this->talkService = $talkService;
$this->rootFolder = $rootFolder;
$this->userManager = $userManager;
$this->logger = $logger;
}
public function run($argument = null): void {
// Check if bot user exists
$bot = $this->userManager->get('talk-bot');
if (!$bot) {
return;
}
// Check if images directory exists
// LazyFolder in Nextcloud 33 doesn't delegate getFolder() properly
try {
$userFolder = $this->rootFolder->getUserFolder($bot->getUID());
try {
$userFolder->get('nc_bot_webhooks-images');
} catch (\OCP\Files\NotFoundException $e) {
return;
}
} catch (\Exception $e) {
return;
}
$count = $this->talkService->purgeOldImages();
if ($count > 0) {
$this->logger->info(
'NCbotwebhooks: purged ' . $count . ' old image files',
['app' => 'nc_bot_webhooks'],
);
}
}
}