Skip to content

Commit ac409dd

Browse files
authored
Merge pull request #111 from nextcloud/fix/noid/avoid-colon-in-filenames
Fix: avoid colon in file names
2 parents 58cd566 + ce51105 commit ac409dd

File tree

1 file changed

+85
-2
lines changed

1 file changed

+85
-2
lines changed

lib/Service/AssistantService.php

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DateTime;
66
use Html2Text\Html2Text;
7+
use OC\User\NoUserException;
78
use OCA\Assistant\AppInfo\Application;
89
use OCA\Assistant\Db\TaskNotificationMapper;
910
use OCA\Assistant\ResponseDefinitions;
@@ -14,12 +15,14 @@
1415
use OCP\Files\File;
1516
use OCP\Files\Folder;
1617
use OCP\Files\GenericFileException;
18+
use OCP\Files\InvalidPathException;
1719
use OCP\Files\IRootFolder;
1820
use OCP\Files\NotPermittedException;
1921
use OCP\IConfig;
2022
use OCP\IL10N;
2123
use OCP\ITempManager;
2224
use OCP\Lock\LockedException;
25+
use OCP\PreConditionNotMetException;
2326
use OCP\Share\IManager as IShareManager;
2427
use OCP\Share\IShare;
2528
use OCP\TaskProcessing\EShapeType;
@@ -201,15 +204,33 @@ public function getAvailableTaskTypes(): array {
201204
return $types;
202205
}
203206

207+
/**
208+
* @param string $userId
209+
* @param string|null $taskTypeId
210+
* @return array
211+
* @throws NotFoundException
212+
* @throws TaskProcessingException
213+
*/
204214
public function getUserTasks(string $userId, ?string $taskTypeId = null): array {
205215
return $this->taskProcessingManager->getUserTasks($userId, $taskTypeId);
206216
}
207217

218+
/**
219+
* @param string $userId
220+
* @param string $tempFileLocation
221+
* @param string|null $filename
222+
* @return array
223+
* @throws NotPermittedException
224+
* @throws InvalidPathException
225+
* @throws \OCP\Files\NotFoundException
226+
*/
208227
public function storeInputFile(string $userId, string $tempFileLocation, ?string $filename = null): array {
209228
$assistantDataFolder = $this->getAssistantDataFolder($userId);
210229

211-
$date = (new DateTime())->format('Y-m-d_H:i:s');
212-
$targetFileName = $filename === null ? $date : ($date . ' ' . $filename);
230+
$formattedDate = (new DateTime())->format('Y-m-d_H.i.s');
231+
$targetFileName = $filename === null
232+
? $formattedDate
233+
: ($formattedDate . ' ' . str_replace(':', '.', $filename));
213234
$targetFile = $assistantDataFolder->newFile($targetFileName, fopen($tempFileLocation, 'rb'));
214235

215236
return [
@@ -218,6 +239,14 @@ public function storeInputFile(string $userId, string $tempFileLocation, ?string
218239
];
219240
}
220241

242+
/**
243+
* @param string $userId
244+
* @return Folder
245+
* @throws NotPermittedException
246+
* @throws \OCP\Files\NotFoundException
247+
* @throws PreConditionNotMetException
248+
* @throws NoUserException
249+
*/
221250
public function getAssistantDataFolder(string $userId): Folder {
222251
$userFolder = $this->rootFolder->getUserFolder($userId);
223252

@@ -235,6 +264,13 @@ public function getAssistantDataFolder(string $userId): Folder {
235264
return $dataFolder;
236265
}
237266

267+
/**
268+
* @param string $userId
269+
* @param int $try
270+
* @return Folder
271+
* @throws NoUserException
272+
* @throws NotPermittedException
273+
*/
238274
private function createAssistantDataFolder(string $userId, int $try = 0): Folder {
239275
$userFolder = $this->rootFolder->getUserFolder($userId);
240276
if ($try === 0) {
@@ -254,6 +290,13 @@ private function createAssistantDataFolder(string $userId, int $try = 0): Folder
254290
return $userFolder->newFolder($folderPath);
255291
}
256292

293+
/**
294+
* @param string $userId
295+
* @param int $fileId
296+
* @return File|null
297+
* @throws NoUserException
298+
* @throws NotPermittedException
299+
*/
257300
public function getUserFile(string $userId, int $fileId): ?File {
258301
$userFolder = $this->rootFolder->getUserFolder($userId);
259302
$file = $userFolder->getFirstNodeById($fileId);
@@ -266,6 +309,15 @@ public function getUserFile(string $userId, int $fileId): ?File {
266309
return null;
267310
}
268311

312+
/**
313+
* @param string $userId
314+
* @param int $fileId
315+
* @return array|null
316+
* @throws InvalidPathException
317+
* @throws NoUserException
318+
* @throws NotPermittedException
319+
* @throws \OCP\Files\NotFoundException
320+
*/
269321
public function getUserFileInfo(string $userId, int $fileId): ?array {
270322
$userFolder = $this->rootFolder->getUserFolder($userId);
271323
$file = $userFolder->getFirstNodeById($fileId);
@@ -316,6 +368,21 @@ public function getTaskOutputFile(string $userId, int $ocpTaskId, int $fileId):
316368
return $node;
317369
}
318370

371+
/**
372+
* @param string $userId
373+
* @param int $ocpTaskId
374+
* @param int $fileId
375+
* @return string
376+
* @throws Exception
377+
* @throws InvalidPathException
378+
* @throws LockedException
379+
* @throws NoUserException
380+
* @throws NotFoundException
381+
* @throws NotPermittedException
382+
* @throws PreConditionNotMetException
383+
* @throws TaskProcessingException
384+
* @throws \OCP\Files\NotFoundException
385+
*/
319386
public function shareOutputFile(string $userId, int $ocpTaskId, int $fileId): string {
320387
$taskOutputFile = $this->getTaskOutputFile($userId, $ocpTaskId, $fileId);
321388
$assistantDataFolder = $this->getAssistantDataFolder($userId);
@@ -346,6 +413,12 @@ public function shareOutputFile(string $userId, int $ocpTaskId, int $fileId): st
346413
return $shareToken;
347414
}
348415

416+
/**
417+
* @param File $file
418+
* @return string
419+
* @throws LockedException
420+
* @throws NotPermittedException
421+
*/
349422
private function getTargetFileName(File $file): string {
350423
$mime = mime_content_type($file->fopen('rb'));
351424
$name = $file->getName();
@@ -362,6 +435,11 @@ private function getTargetFileName(File $file): string {
362435
return $name . $ext;
363436
}
364437

438+
/**
439+
* @param Task $task
440+
* @return array
441+
* @throws NotFoundException
442+
*/
365443
private function extractFileIdsFromTask(Task $task): array {
366444
$ids = [];
367445
$taskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
@@ -415,6 +493,7 @@ public function getOutputFilePreviewFile(string $userId, int $taskId, int $fileI
415493

416494
/**
417495
* Sanitize inputs for storage based on the input type
496+
*
418497
* @param string $type
419498
* @param array $inputs
420499
* @return array
@@ -587,6 +666,10 @@ private function parseDocument(string $filePath, string $mimeType): string {
587666
return $outText;
588667
}
589668

669+
/**
670+
* @param string $content
671+
* @return string
672+
*/
590673
private function parseRtfDocument(string $content): string {
591674
// henck/rtf-to-html
592675
$document = new Document($content);

0 commit comments

Comments
 (0)