Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/Controller/AssistantApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,18 @@ public function getOutputFilePreview(int $ocpTaskId, int $fileId, ?int $x = 100,
public function getOutputFile(int $ocpTaskId, int $fileId): DataDownloadResponse|DataResponse {
try {
$taskOutputFile = $this->assistantService->getTaskOutputFile($this->userId, $ocpTaskId, $fileId);
$realMime = mime_content_type($taskOutputFile->fopen('rb'));
$head = fread($taskOutputFile->fopen('rb'), 4096);
$realMime = (new \finfo(FILEINFO_MIME_TYPE))->buffer($head);

$fileName = $ocpTaskId . '-' . $fileId;
$extension = pathinfo($taskOutputFile->getName(), PATHINFO_EXTENSION);
if ($extension !== '') {
$fileName .= '.' . $extension;
}

$response = new DataDownloadResponse(
$taskOutputFile->getContent(),
$ocpTaskId . '-' . $fileId,
$fileName,
$realMime ?: 'application/octet-stream',
);
$response->cacheFor(60 * 60 * 24, false, true);
Expand Down
6 changes: 4 additions & 2 deletions lib/Service/AssistantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,8 @@ public function saveOutputFile(string $userId, int $ocpTaskId, int $fileId): arr
* @throws NotPermittedException
*/
private function getTargetFileName(File $file): string {
$mimeType = mime_content_type($file->fopen('rb'));
$head = fread($file->fopen('rb'), 4096);
$mimeType = (new \finfo(FILEINFO_MIME_TYPE))->buffer($head);
$fileName = $file->getName();

$mimes = new \Mimey\MimeTypes;
Expand Down Expand Up @@ -692,7 +693,8 @@ private function extractFileIdsFromTask(Task $task): array {
*/
public function getOutputFilePreviewFile(string $userId, int $taskId, int $fileId, ?int $x = 100, ?int $y = 100): ?array {
$taskOutputFile = $this->getTaskOutputFile($userId, $taskId, $fileId);
$realMime = mime_content_type($taskOutputFile->fopen('rb'));
$head = fread($taskOutputFile->fopen('rb'), 4096);
$realMime = (new \finfo(FILEINFO_MIME_TYPE))->buffer($head);
return $this->previewService->getFilePreviewFile($taskOutputFile, $x, $y, $realMime ?: null);
}

Expand Down