Skip to content

Commit

Permalink
Add support for file question
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Myakshin <[email protected]>
  • Loading branch information
Koc committed Apr 25, 2024
1 parent b15eda0 commit 668e892
Show file tree
Hide file tree
Showing 19 changed files with 1,080 additions and 91 deletions.
6 changes: 5 additions & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- **🙋 Get involved!** We have lots of stuff planned like more question types, collaboration on forms, [and much more](https://github.com/nextcloud/forms/milestones)!
]]></description>

<version>4.2.3</version>
<version>4.2.4</version>
<licence>agpl</licence>

<author>Affan Hussain</author>
Expand Down Expand Up @@ -54,6 +54,10 @@
<nextcloud min-version="28" max-version="29" />
</dependencies>

<background-jobs>
<job>OCA\Forms\BackgroundJob\CleanupUploadedFilesJob</job>
</background-jobs>

<settings>
<admin>OCA\Forms\Settings\Settings</admin>
<admin-section>OCA\Forms\Settings\SettingsSection</admin-section>
Expand Down
8 changes: 8 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@
'apiVersion' => 'v2(\.[1-4])?'
]
],
[
'name' => 'api#uploadFiles',
'url' => '/api/{apiVersion}/uploadFiles/{formId}/{questionId}',
'verb' => 'POST',
'requirements' => [
'apiVersion' => 'v2.5'
]
],
[
'name' => 'api#insertSubmission',
'url' => '/api/{apiVersion}/submission/insert',
Expand Down
66 changes: 66 additions & 0 deletions lib/BackgroundJob/CleanupUploadedFilesJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace OCA\Forms\BackgroundJob;

use OCA\Forms\Db\FormMapper;
use OCA\Forms\Db\UploadedFileMapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCP\Files\IRootFolder;
use Psr\Log\LoggerInterface;

class CleanupUploadedFilesJob extends TimedJob {
private const FILE_LIFETIME = '-2 days';

public function __construct(
private IRootFolder $storage,
private FormMapper $formMapper,
private UploadedFileMapper $uploadedFileMapper,
private LoggerInterface $logger,
ITimeFactory $time) {
parent::__construct($time);

$this->setInterval(60 * 60 * 24);

}

/**
* @param array $argument
*/
public function run($argument): void {
$dateTime = new \DateTimeImmutable(self::FILE_LIFETIME);
$this->logger->info('Deleting files that were uploaded before {before} and still not submitted.', [
'before' => $dateTime->format(\DateTimeImmutable::ATOM),
]);

$uploadedFiles = $this->uploadedFileMapper->findUploadedEarlierThan($dateTime);

$deleted = 0;
foreach ($uploadedFiles as $uploadedFile) {
$this->logger->info('Deleting uploaded file "{originalFileName}" for form {formId}.', [
'originalFileName' => $uploadedFile->getOriginalFileName(),
'formId' => $uploadedFile->getFormId(),
]);

$form = $this->formMapper->findById($uploadedFile->getFormId());
$userFolder = $this->storage->getUserFolder($form->getOwnerId());

$nodes = $userFolder->getById($uploadedFile->getFileId());

if (!empty($nodes)) {
$node = $nodes[0];
$node->delete();
} else {
$this->logger->warning('Could not find uploaded file "{fileId}" for deletion.', [
'fileId' => $uploadedFile->getFileId(),
]);
}

$this->uploadedFileMapper->delete($uploadedFile);

$deleted++;
}

$this->logger->info('Deleted {deleted} uploaded files.', ['deleted' => $deleted]);
}
}
23 changes: 22 additions & 1 deletion lib/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class Constants {
public const ANSWER_TYPE_DATE = 'date';
public const ANSWER_TYPE_DATETIME = 'datetime';
public const ANSWER_TYPE_TIME = 'time';
public const ANSWER_TYPE_FILE = 'file';

// All AnswerTypes
public const ANSWER_TYPES = [
Expand All @@ -101,7 +102,8 @@ class Constants {
self::ANSWER_TYPE_LONG,
self::ANSWER_TYPE_DATE,
self::ANSWER_TYPE_DATETIME,
self::ANSWER_TYPE_TIME
self::ANSWER_TYPE_TIME,
self::ANSWER_TYPE_FILE,
];

// AnswerTypes, that need/have predefined Options
Expand Down Expand Up @@ -155,6 +157,21 @@ class Constants {
'validationRegex' => ['string'],
];

public const EXTRA_SETTINGS_FILE = [
'allowedFileTypes',
'allowedFileExtensions',
'maxAllowedFilesCount',
'maxFileSize',
];

// should be in sync with FileTypes.js
public const EXTRA_SETTINGS_ALLOWED_FILE_TYPES = [
'image',
'x-office/document',
'x-office/presentation',
'x-office/spreadsheet',
];

/**
* !! Keep in sync with src/mixins/ShareTypes.js !!
*/
Expand Down Expand Up @@ -204,4 +221,8 @@ class Constants {
];

public const DEFAULT_FILE_FORMAT = 'csv';

public const UNSUBMITTED_FILES_FOLDER = 'forms/unsubmitted';

public const FILES_FOLDER = 'forms';
}
Loading

0 comments on commit 668e892

Please sign in to comment.