mirror of
https://github.com/JanGross/quicknotes.git
synced 2025-12-01 07:37:18 +01:00
Add api to upload attachments
This commit is contained in:
@@ -23,6 +23,8 @@
|
||||
|
||||
namespace OCA\QuickNotes\Service;
|
||||
|
||||
use OCP\AppFramework\Utility\ITimeFactory;
|
||||
|
||||
use OCP\IURLGenerator;
|
||||
|
||||
use OCP\Files\File;
|
||||
@@ -31,6 +33,9 @@ use OCP\Files\Node;
|
||||
use OCP\Files\IRootFolder;
|
||||
use OCP\Files\NotFoundException;
|
||||
|
||||
|
||||
use OCA\QuickNotes\Service\SettingsService;
|
||||
|
||||
class FileService {
|
||||
|
||||
/** @var string|null */
|
||||
@@ -40,15 +45,25 @@ class FileService {
|
||||
private $rootFolder;
|
||||
|
||||
/** @var IURLGenerator */
|
||||
protected $urlGenerator;
|
||||
private $urlGenerator;
|
||||
|
||||
/** @var ITimeFactory */
|
||||
private $timeFactory;
|
||||
|
||||
/** @var SettingsService */
|
||||
private SettingsService $settingsService;
|
||||
|
||||
public function __construct($userId,
|
||||
IRootFolder $rootFolder,
|
||||
IURLGenerator $urlGenerator)
|
||||
IRootFolder $rootFolder,
|
||||
IURLGenerator $urlGenerator,
|
||||
ITimeFactory $timeFactory,
|
||||
SettingsService $settingsService)
|
||||
{
|
||||
$this->userId = $userId;
|
||||
$this->rootFolder = $rootFolder;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->userId = $userId;
|
||||
$this->rootFolder = $rootFolder;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->timeFactory = $timeFactory;
|
||||
$this->settingsService = $settingsService;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,4 +103,38 @@ class FileService {
|
||||
return $this->urlGenerator->linkToRoute('files.view.index', $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload attachment and return fileId
|
||||
*/
|
||||
public function upload($fileName, $fileContent): int {
|
||||
$userFolder = $this->rootFolder->getUserFolder($this->userId);
|
||||
|
||||
$ts = $this->timeFactory->getTime();
|
||||
$dt = new \DateTime();
|
||||
$dt->setTimestamp($ts);
|
||||
|
||||
$secureFileName = $dt->format('YmdHis') . ' - ' . $fileName;
|
||||
$attachmentsFolder = $this->getAttachmentsFolder();
|
||||
|
||||
$file = $attachmentsFolder->newFile($secureFileName);
|
||||
$file->putContent($fileContent);
|
||||
|
||||
return $file->getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \OCP\Files\Folder
|
||||
*/
|
||||
private function getAttachmentsFolder() {
|
||||
$userFolder = $this->rootFolder->getUserFolder($this->userId);
|
||||
$attachmentsFolder = $this->settingsService->getAttachmentsFolder();
|
||||
|
||||
try {
|
||||
$attachmentsFolderNode = $userFolder->get($attachmentsFolder);
|
||||
} catch (NotFoundException $e) {
|
||||
$attachmentsFolderNode = $userFolder->newFolder($attachmentsFolder);
|
||||
}
|
||||
|
||||
return $attachmentsFolderNode;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,9 @@ class SettingsService {
|
||||
const COLOR_FOR_NEW_NOTES_KEY = 'default_color';
|
||||
const DEFAULT_COLOR_FOR_NEW_NOTES = '#F7EB96';
|
||||
|
||||
const ATTACHMENTS_FOLDER_KEY = 'attachments_folder';
|
||||
const DEFAULT_ATTACHMENTS_FOLDER = 'Quicknotes';
|
||||
|
||||
/** @var IConfig Config */
|
||||
private $config;
|
||||
|
||||
@@ -62,4 +65,12 @@ class SettingsService {
|
||||
$this->config->setUserValue($this->userId, Application::APP_ID, self::COLOR_FOR_NEW_NOTES_KEY, $color);
|
||||
}
|
||||
|
||||
public function getAttachmentsFolder(): string {
|
||||
return $this->config->getUserValue($this->userId, Application::APP_ID, self::ATTACHMENTS_FOLDER_KEY, self::DEFAULT_ATTACHMENTS_FOLDER);
|
||||
}
|
||||
|
||||
public function setAttachmentsFolder(string $folder) {
|
||||
$this->config->setUserValue($this->userId, Application::APP_ID, self::ATTACHMENTS_FOLDER_KEY, $folder);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user