-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from nextcloud/enh/scoped-context-chat-task-type
enh: custom UI for scoped context chat
- Loading branch information
Showing
8 changed files
with
644 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* Nextcloud - TpAssistant | ||
* | ||
* This file is licensed under the Affero General Public License version 3 or | ||
* later. See the COPYING file. | ||
* | ||
* @author Julien Veyssier <[email protected]> | ||
* @copyright Julien Veyssier 2022 | ||
*/ | ||
|
||
namespace OCA\TpAssistant\Controller; | ||
|
||
use Exception; | ||
use OCA\TpAssistant\Service\PreviewService; | ||
use OCP\AppFramework\Controller; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\Attribute\NoAdminRequired; | ||
use OCP\AppFramework\Http\Attribute\NoCSRFRequired; | ||
use OCP\AppFramework\Http\DataDownloadResponse; | ||
use OCP\AppFramework\Http\DataResponse; | ||
use OCP\AppFramework\Http\RedirectResponse; | ||
use OCP\AppFramework\Http\Response; | ||
use OCP\IRequest; | ||
use Psr\Log\LoggerInterface; | ||
use Throwable; | ||
|
||
class PreviewController extends Controller { | ||
|
||
public function __construct( | ||
string $appName, | ||
IRequest $request, | ||
private PreviewService $imageService, | ||
private LoggerInterface $logger, | ||
private ?string $userId | ||
) { | ||
parent::__construct($appName, $request); | ||
} | ||
|
||
/** | ||
* @param int $id | ||
* @param int $x | ||
* @param int $y | ||
* @return DataDownloadResponse|DataResponse|RedirectResponse | ||
*/ | ||
#[NoAdminRequired] | ||
#[NoCSRFRequired] | ||
public function getFileImage(int $id, int $x = 100, int $y = 100): Response { | ||
try { | ||
$preview = $this->imageService->getFilePreviewFile($id, $this->userId, $x, $y); | ||
if ($preview === null) { | ||
$this->logger->error('No preview for user "' . $this->userId . '"'); | ||
return new DataResponse('', Http::STATUS_NOT_FOUND); | ||
} | ||
|
||
if ($preview['type'] === 'file') { | ||
return new DataDownloadResponse( | ||
$preview['file']->getContent(), | ||
(string)Http::STATUS_OK, | ||
$preview['file']->getMimeType() | ||
); | ||
} elseif ($preview['type'] === 'icon') { | ||
return new RedirectResponse($preview['icon']); | ||
} | ||
} catch (Exception | Throwable $e) { | ||
$this->logger->error('getImage error', ['exception' => $e]); | ||
return new DataResponse('', Http::STATUS_NOT_FOUND); | ||
} | ||
return new DataResponse('', Http::STATUS_NOT_FOUND); | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
/** | ||
* Nextcloud - TpAssistant | ||
* | ||
* This file is licensed under the Affero General Public License version 3 or | ||
* later. See the COPYING file. | ||
* | ||
* @author Julien Veyssier | ||
* @copyright Julien Veyssier 2022 | ||
*/ | ||
|
||
namespace OCA\TpAssistant\Service; | ||
|
||
use OCP\Files\File; | ||
use OCP\Files\IMimeTypeDetector; | ||
use OCP\Files\IRootFolder; | ||
use OCP\Files\NotFoundException; | ||
use OCP\IPreview; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class PreviewService { | ||
|
||
public function __construct( | ||
private IRootFolder $root, | ||
private LoggerInterface $logger, | ||
private IPreview $previewManager, | ||
private IMimeTypeDetector $mimeTypeDetector, | ||
) { | ||
} | ||
|
||
/** | ||
* @param int $fileId | ||
* @param string $userId | ||
* @param int $x | ||
* @param int $y | ||
* @return array|null | ||
* @throws \OCP\Files\NotPermittedException | ||
* @throws \OC\User\NoUserException | ||
*/ | ||
public function getFilePreviewFile(int $fileId, string $userId, int $x = 100, int $y = 100): ?array { | ||
$userFolder = $this->root->getUserFolder($userId); | ||
$files = $userFolder->getById($fileId); | ||
if (count($files) > 0 && $files[0] instanceof File) { | ||
$file = $files[0]; | ||
if ($this->previewManager->isMimeSupported($file->getMimeType())) { | ||
try { | ||
return [ | ||
'type' => 'file', | ||
'file' => $this->previewManager->getPreview($file, $x, $y), | ||
]; | ||
} catch (NotFoundException $e) { | ||
$this->logger->error('Mimetype is supported but no preview available', ['exception' => $e]); | ||
} | ||
} | ||
// fallback: mimetype icon | ||
return [ | ||
'type' => 'icon', | ||
'icon' => $this->mimeTypeDetector->mimeTypeIcon($file->getMimeType()), | ||
]; | ||
} | ||
return null; | ||
} | ||
} |
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
Oops, something went wrong.