Skip to content
Open
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
27 changes: 25 additions & 2 deletions lib/Service/AssistantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ 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'));
$mimeType = $this->detectMimeType($file->fopen('rb'));
$fileName = $file->getName();

$mimes = new \Mimey\MimeTypes;
Expand All @@ -755,6 +755,29 @@ private function getTargetFileName(File $file): string {
return $fileName;
}

/**
* Wraps mime_content_type() to avoid a PHP warning when given a stream
* that does not implement stream_cast() (e.g. Icewind\Streams\CallbackWrapper,
* returned by File::fopen() for some storage backends). The stream is
* drained into a temp file first so mime_content_type() can operate on a
* real path instead of the unsupported stream resource.
*
* @param resource $stream
* @return string|false
*/
private function detectMimeType($stream) {
$tmpFile = tempnam(sys_get_temp_dir(), 'nc_assistant_mime_');
$tmpHandle = fopen($tmpFile, 'wb');
stream_copy_to_stream($stream, $tmpHandle);
fclose($tmpHandle);
if (is_resource($stream)) {
fclose($stream);
}
$mimeType = mime_content_type($tmpFile);
unlink($tmpFile);
return $mimeType;
}

/**
* @param Task $task
* @return array
Expand Down Expand Up @@ -807,7 +830,7 @@ 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'));
$realMime = $this->detectMimeType($taskOutputFile->fopen('rb'));
return $this->previewService->getFilePreviewFile($taskOutputFile, $x, $y, $realMime ?: null);
}

Expand Down
Loading