Skip to content

Commit 39be7f7

Browse files
committed
feat: add support for more file types to file actions
Signed-off-by: Lukas Schaefer <lukas@lschaefer.xyz>
1 parent bb07161 commit 39be7f7

File tree

4 files changed

+19
-28
lines changed

4 files changed

+19
-28
lines changed

lib/Service/TaskProcessingService.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace OCA\Assistant\Service;
99

10-
use OC\User\NoUserException;
1110
use OCA\Assistant\AppInfo\Application;
1211
use OCP\Files\File;
1312
use OCP\Files\GenericFileException;
@@ -32,6 +31,7 @@ public function __construct(
3231
private IManager $taskProcessingManager,
3332
private IRootFolder $rootFolder,
3433
private LoggerInterface $logger,
34+
private AssistantService $assistantService,
3535
) {
3636
}
3737

@@ -105,22 +105,11 @@ public function runFileAction(string $userId, int $fileId, string $taskTypeId):
105105
if (!$this->isFileActionTaskTypeSupported($taskTypeId)) {
106106
throw new Exception('Invalid task type for file action');
107107
}
108-
try {
109-
$userFolder = $this->rootFolder->getUserFolder($userId);
110-
} catch (NoUserException|NotPermittedException $e) {
111-
$this->logger->warning('Assistant runFileAction, the user folder could not be obtained', ['exception' => $e]);
112-
throw new Exception('The user folder could not be obtained');
113-
}
114-
$file = $userFolder->getFirstNodeById($fileId);
115-
if (!$file instanceof File) {
116-
$this->logger->warning('Assistant runFileAction, the input file is not a file', ['file_id' => $fileId]);
117-
throw new NotFoundException('File is not a file');
118-
}
119108
try {
120109
$input = $taskTypeId === AudioToText::ID
121110
? ['input' => $fileId]
122-
: ['input' => $file->getContent()];
123-
} catch (NotPermittedException|GenericFileException|LockedException $e) {
111+
: ['input' => $this->assistantService->parseTextFromFile($userId, fileId: $fileId)];
112+
} catch (NotPermittedException|GenericFileException|LockedException|\OCP\Files\NotFoundException|Exception $e) {
124113
$this->logger->warning('Assistant runFileAction, impossible to read the file action input file', ['exception' => $e]);
125114
throw new Exception('Impossible to read the file action input file');
126115
}

src/components/fields/TextInput.vue

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,10 @@ import isMobile from '../../mixins/isMobile.js'
5555
import axios from '@nextcloud/axios'
5656
import { getFilePickerBuilder, showError } from '@nextcloud/dialogs'
5757
import { generateOcsUrl } from '@nextcloud/router'
58-
59-
const VALID_MIME_TYPES = [
60-
'text/rtf',
61-
'text/plain',
62-
'text/markdown',
63-
'application/msword', // doc
64-
'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // docx
65-
'application/vnd.oasis.opendocument.text', // odt
66-
'application/pdf', // pdf
67-
]
58+
import { VALID_TEXT_MIME_TYPES } from '../../constants.js'
6859
6960
const picker = (callback, target) => getFilePickerBuilder(t('assistant', 'Choose a text file'))
70-
.setMimeTypeFilter(VALID_MIME_TYPES)
61+
.setMimeTypeFilter(VALID_TEXT_MIME_TYPES)
7162
.setMultiSelect(false)
7263
.allowDirectories(false)
7364
.addButton({

src/constants.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,13 @@ export const VALID_IMAGE_MIME_TYPES = [
7878
export const VALID_VIDEO_MIME_TYPES = [
7979
'video/*',
8080
]
81+
82+
export const VALID_TEXT_MIME_TYPES = [
83+
'text/rtf',
84+
'text/plain',
85+
'text/markdown',
86+
'application/msword', // doc
87+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // docx
88+
'application/vnd.oasis.opendocument.text', // odt
89+
'application/pdf', // pdf
90+
]

src/files/fileActions.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import CreationSvgIcon from '@mdi/svg/svg/creation.svg?raw'
1010
import SummarizeSymbol from '@material-symbols/svg-700/outlined/summarize.svg?raw'
1111
import TTSSymbol from '@material-symbols/svg-700/outlined/text_to_speech.svg?raw'
1212
import STTSymbol from '@material-symbols/svg-700/outlined/speech_to_text.svg?raw'
13+
import { VALID_AUDIO_MIME_TYPES, VALID_TEXT_MIME_TYPES } from '../constants.js'
1314

1415
const actionIgnoreLists = [
1516
'trashbin',
@@ -50,7 +51,7 @@ function registerSummarizeAction() {
5051
&& nodes.length === 1
5152
&& !nodes.some(({ permissions }) => (permissions & Permission.READ) === 0)
5253
&& nodes.every(({ type }) => type === FileType.File)
53-
&& nodes.every(({ mime }) => ['text/plain', 'text/markdown'].includes(mime))
54+
&& nodes.every(({ mime }) => VALID_TEXT_MIME_TYPES.includes(mime))
5455
},
5556
iconSvgInline: () => SummarizeSymbol,
5657
order: 0,
@@ -89,7 +90,7 @@ function registerTtsAction() {
8990
&& nodes.length === 1
9091
&& !nodes.some(({ permissions }) => (permissions & Permission.READ) === 0)
9192
&& nodes.every(({ type }) => type === FileType.File)
92-
&& nodes.every(({ mime }) => ['text/plain', 'text/markdown'].includes(mime))
93+
&& nodes.every(({ mime }) => VALID_TEXT_MIME_TYPES.includes(mime))
9394
},
9495
iconSvgInline: () => TTSSymbol,
9596
order: 0,
@@ -128,7 +129,7 @@ function registerSttAction() {
128129
&& nodes.length === 1
129130
&& !nodes.some(({ permissions }) => (permissions & Permission.READ) === 0)
130131
&& nodes.every(({ type }) => type === FileType.File)
131-
&& nodes.every(({ mime }) => ['audio/mpeg', 'audio/wav', 'audio/mp3'].includes(mime))
132+
&& nodes.every(({ mime }) => VALID_AUDIO_MIME_TYPES.includes(mime))
132133
},
133134
iconSvgInline: () => STTSymbol,
134135
order: 0,

0 commit comments

Comments
 (0)