Skip to content

Commit

Permalink
fix(upload-input): avoid colon in file names
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Aug 13, 2024
1 parent 58cd566 commit ce51105
Showing 1 changed file with 85 additions and 2 deletions.
87 changes: 85 additions & 2 deletions lib/Service/AssistantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use Html2Text\Html2Text;
use OC\User\NoUserException;
use OCA\Assistant\AppInfo\Application;
use OCA\Assistant\Db\TaskNotificationMapper;
use OCA\Assistant\ResponseDefinitions;
Expand All @@ -14,12 +15,14 @@
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\GenericFileException;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\IL10N;
use OCP\ITempManager;
use OCP\Lock\LockedException;
use OCP\PreConditionNotMetException;
use OCP\Share\IManager as IShareManager;
use OCP\Share\IShare;
use OCP\TaskProcessing\EShapeType;
Expand Down Expand Up @@ -201,15 +204,33 @@ public function getAvailableTaskTypes(): array {
return $types;
}

/**
* @param string $userId
* @param string|null $taskTypeId
* @return array
* @throws NotFoundException
* @throws TaskProcessingException
*/
public function getUserTasks(string $userId, ?string $taskTypeId = null): array {
return $this->taskProcessingManager->getUserTasks($userId, $taskTypeId);
}

/**
* @param string $userId
* @param string $tempFileLocation
* @param string|null $filename
* @return array
* @throws NotPermittedException
* @throws InvalidPathException
* @throws \OCP\Files\NotFoundException
*/
public function storeInputFile(string $userId, string $tempFileLocation, ?string $filename = null): array {
$assistantDataFolder = $this->getAssistantDataFolder($userId);

$date = (new DateTime())->format('Y-m-d_H:i:s');
$targetFileName = $filename === null ? $date : ($date . ' ' . $filename);
$formattedDate = (new DateTime())->format('Y-m-d_H.i.s');
$targetFileName = $filename === null
? $formattedDate
: ($formattedDate . ' ' . str_replace(':', '.', $filename));
$targetFile = $assistantDataFolder->newFile($targetFileName, fopen($tempFileLocation, 'rb'));

return [
Expand All @@ -218,6 +239,14 @@ public function storeInputFile(string $userId, string $tempFileLocation, ?string
];
}

/**
* @param string $userId
* @return Folder
* @throws NotPermittedException
* @throws \OCP\Files\NotFoundException
* @throws PreConditionNotMetException
* @throws NoUserException
*/
public function getAssistantDataFolder(string $userId): Folder {
$userFolder = $this->rootFolder->getUserFolder($userId);

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

/**
* @param string $userId
* @param int $try
* @return Folder
* @throws NoUserException
* @throws NotPermittedException
*/
private function createAssistantDataFolder(string $userId, int $try = 0): Folder {
$userFolder = $this->rootFolder->getUserFolder($userId);
if ($try === 0) {
Expand All @@ -254,6 +290,13 @@ private function createAssistantDataFolder(string $userId, int $try = 0): Folder
return $userFolder->newFolder($folderPath);
}

/**
* @param string $userId
* @param int $fileId
* @return File|null
* @throws NoUserException
* @throws NotPermittedException
*/
public function getUserFile(string $userId, int $fileId): ?File {
$userFolder = $this->rootFolder->getUserFolder($userId);
$file = $userFolder->getFirstNodeById($fileId);
Expand All @@ -266,6 +309,15 @@ public function getUserFile(string $userId, int $fileId): ?File {
return null;
}

/**
* @param string $userId
* @param int $fileId
* @return array|null
* @throws InvalidPathException
* @throws NoUserException
* @throws NotPermittedException
* @throws \OCP\Files\NotFoundException
*/
public function getUserFileInfo(string $userId, int $fileId): ?array {
$userFolder = $this->rootFolder->getUserFolder($userId);
$file = $userFolder->getFirstNodeById($fileId);
Expand Down Expand Up @@ -316,6 +368,21 @@ public function getTaskOutputFile(string $userId, int $ocpTaskId, int $fileId):
return $node;
}

/**
* @param string $userId
* @param int $ocpTaskId
* @param int $fileId
* @return string
* @throws Exception
* @throws InvalidPathException
* @throws LockedException
* @throws NoUserException
* @throws NotFoundException
* @throws NotPermittedException
* @throws PreConditionNotMetException
* @throws TaskProcessingException
* @throws \OCP\Files\NotFoundException
*/
public function shareOutputFile(string $userId, int $ocpTaskId, int $fileId): string {
$taskOutputFile = $this->getTaskOutputFile($userId, $ocpTaskId, $fileId);
$assistantDataFolder = $this->getAssistantDataFolder($userId);
Expand Down Expand Up @@ -346,6 +413,12 @@ public function shareOutputFile(string $userId, int $ocpTaskId, int $fileId): st
return $shareToken;
}

/**
* @param File $file
* @return string
* @throws LockedException
* @throws NotPermittedException
*/
private function getTargetFileName(File $file): string {
$mime = mime_content_type($file->fopen('rb'));
$name = $file->getName();
Expand All @@ -362,6 +435,11 @@ private function getTargetFileName(File $file): string {
return $name . $ext;
}

/**
* @param Task $task
* @return array
* @throws NotFoundException
*/
private function extractFileIdsFromTask(Task $task): array {
$ids = [];
$taskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
Expand Down Expand Up @@ -415,6 +493,7 @@ public function getOutputFilePreviewFile(string $userId, int $taskId, int $fileI

/**
* Sanitize inputs for storage based on the input type
*
* @param string $type
* @param array $inputs
* @return array
Expand Down Expand Up @@ -587,6 +666,10 @@ private function parseDocument(string $filePath, string $mimeType): string {
return $outText;
}

/**
* @param string $content
* @return string
*/
private function parseRtfDocument(string $content): string {
// henck/rtf-to-html
$document = new Document($content);
Expand Down

0 comments on commit ce51105

Please sign in to comment.