Skip to content

Commit a3d545c

Browse files
Kocbackportbot[bot]
authored andcommitted
fix: simplify mime-type checks to support jpg and other image formats #2399
Signed-off-by: Kostiantyn Miakshyn <[email protected]>
1 parent 7587b57 commit a3d545c

File tree

3 files changed

+15
-34
lines changed

3 files changed

+15
-34
lines changed

lib/Controller/ApiController.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ public function newOption(int $formId, int $questionId, array $optionTexts): Dat
750750
$this->logger->debug('This form is archived and can not be modified');
751751
throw new OCSForbiddenException();
752752
}
753-
753+
754754
try {
755755
$question = $this->questionMapper->findById($questionId);
756756
} catch (IMapperException $e) {
@@ -885,7 +885,7 @@ public function deleteOption(int $formId, int $questionId, int $optionId): DataR
885885
$this->logger->debug('This form is archived and can not be modified');
886886
throw new OCSForbiddenException();
887887
}
888-
888+
889889
try {
890890
$option = $this->optionMapper->findById($optionId);
891891
$question = $this->questionMapper->findById($questionId);
@@ -947,14 +947,14 @@ public function reorderOptions(int $formId, int $questionId, array $newOrder) {
947947
$this->logger->debug('The given array contains duplicates');
948948
throw new OCSBadRequestException('The given array contains duplicates');
949949
}
950-
950+
951951
$options = $this->optionMapper->findByQuestion($questionId);
952-
952+
953953
if (sizeof($options) !== sizeof($newOrder)) {
954954
$this->logger->debug('The length of the given array does not match the number of stored options');
955955
throw new OCSBadRequestException('The length of the given array does not match the number of stored options');
956956
}
957-
957+
958958
$options = []; // Clear Array of Entities
959959
$response = []; // Array of ['optionId' => ['order' => newOrder]]
960960

@@ -997,7 +997,7 @@ public function reorderOptions(int $formId, int $questionId, array $newOrder) {
997997
}
998998

999999
$this->formMapper->update($form);
1000-
1000+
10011001
return new DataResponse($response);
10021002
}
10031003

@@ -1351,7 +1351,7 @@ public function uploadFiles(int $formId, int $questionId, string $shareHash = ''
13511351

13521352
$valid = false;
13531353
foreach ($extraSettings['allowedFileTypes'] ?? [] as $allowedFileType) {
1354-
if (str_starts_with($aliases[$mimeType] ?? '', $allowedFileType)) {
1354+
if (str_starts_with($mimeType, $allowedFileType) || str_starts_with($aliases[$mimeType] ?? '', $allowedFileType)) {
13551355
$valid = true;
13561356
break;
13571357
}

lib/Service/FormsService.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
use OCP\AppFramework\Db\DoesNotExistException;
4040
use OCP\AppFramework\Db\IMapperException;
4141
use OCP\EventDispatcher\IEventDispatcher;
42-
use OCP\Files\IMimeTypeDetector;
4342
use OCP\Files\IRootFolder;
4443
use OCP\Files\NotFoundException;
4544
use OCP\IGroup;
@@ -72,7 +71,6 @@ public function __construct(
7271
private CirclesService $circlesService,
7372
private IRootFolder $rootFolder,
7473
private IL10N $l10n,
75-
private IMimeTypeDetector $mimeTypeDetector,
7674
private IEventDispatcher $eventDispatcher,
7775
) {
7876
$this->currentUser = $userSession->getUser();
@@ -124,10 +122,9 @@ public function getQuestions(int $formId): array {
124122
$question['accept'] = [];
125123
if ($question['type'] === Constants::ANSWER_TYPE_FILE) {
126124
if ($question['extraSettings']['allowedFileTypes'] ?? null) {
127-
$question['accept'] = array_keys(array_intersect(
128-
$this->mimeTypeDetector->getAllAliases(),
129-
$question['extraSettings']['allowedFileTypes']
130-
));
125+
$question['accept'] = array_map(function (string $fileType) {
126+
return str_contains($fileType, '/') ? $fileType : $fileType . '/*';
127+
}, $question['extraSettings']['allowedFileTypes']);
131128
}
132129

133130
if ($question['extraSettings']['allowedFileExtensions'] ?? null) {
@@ -161,10 +158,9 @@ public function getQuestion(int $questionId): array {
161158
$question['accept'] = [];
162159
if ($question['type'] === Constants::ANSWER_TYPE_FILE) {
163160
if ($question['extraSettings']['allowedFileTypes'] ?? null) {
164-
$question['accept'] = array_keys(array_intersect(
165-
$this->mimeTypeDetector->getAllAliases(),
166-
$question['extraSettings']['allowedFileTypes']
167-
));
161+
$question['accept'] = array_map(function (string $fileType) {
162+
return str_contains($fileType, '/') ? $fileType : $fileType . '/*';
163+
}, $question['extraSettings']['allowedFileTypes']);
168164
}
169165

170166
if ($question['extraSettings']['allowedFileExtensions'] ?? null) {

tests/Unit/Service/FormsServiceTest.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ function microtime(bool|float $asFloat = false) {
6565
use OCP\AppFramework\Db\DoesNotExistException;
6666
use OCP\EventDispatcher\IEventDispatcher;
6767
use OCP\Files\Folder;
68-
use OCP\Files\IMimeTypeDetector;
6968
use OCP\Files\IRootFolder;
7069
use OCP\Files\NotFoundException;
7170
use OCP\IGroup;
@@ -124,9 +123,6 @@ class FormsServiceTest extends TestCase {
124123
/** @var IL10N|MockObject */
125124
private $l10n;
126125

127-
/** @var IMimeTypeDetector|MockObject */
128-
private $mimeTypeDetector;
129-
130126
public function setUp(): void {
131127
parent::setUp();
132128
$this->activityManager = $this->createMock(ActivityManager::class);
@@ -160,8 +156,6 @@ public function setUp(): void {
160156
return $identity;
161157
}));
162158

163-
$this->mimeTypeDetector = $this->createMock(IMimeTypeDetector::class);
164-
165159
$this->formsService = new FormsService(
166160
$userSession,
167161
$this->activityManager,
@@ -177,7 +171,6 @@ public function setUp(): void {
177171
$this->circlesService,
178172
$this->storage,
179173
$this->l10n,
180-
$this->mimeTypeDetector,
181174
\OCP\Server::get(IEventDispatcher::class),
182175
);
183176
}
@@ -653,7 +646,6 @@ public function testGetPermissions_NotLoggedIn() {
653646
$this->circlesService,
654647
$this->storage,
655648
$this->l10n,
656-
$this->mimeTypeDetector,
657649
\OCP\Server::get(IEventDispatcher::class),
658650
);
659651

@@ -893,7 +885,6 @@ public function testPublicCanSubmit() {
893885
$this->circlesService,
894886
$this->storage,
895887
$this->l10n,
896-
$this->mimeTypeDetector,
897888
\OCP\Server::get(IEventDispatcher::class),
898889
);
899890

@@ -1005,7 +996,6 @@ public function testHasUserAccess_NotLoggedIn() {
1005996
$this->circlesService,
1006997
$this->storage,
1007998
$this->l10n,
1008-
$this->mimeTypeDetector,
1009999
\OCP\Server::get(IEventDispatcher::class),
10101000
);
10111001

@@ -1237,7 +1227,6 @@ public function testNotifyNewSubmission($shares, $shareNotifications) {
12371227
$this->circlesService,
12381228
$this->storage,
12391229
$this->l10n,
1240-
$this->mimeTypeDetector,
12411230
$eventDispatcher,
12421231
])
12431232
->getMock();
@@ -1496,22 +1485,18 @@ public function testGetQuestionsWithVariousQuestionTypes(): void {
14961485
$this->createQuestionEntity(['id' => 1, 'type' => 'text']),
14971486
$this->createQuestionEntity(['id' => 2, 'type' => Constants::ANSWER_TYPE_FILE, 'extraSettings' => [
14981487
'allowedFileTypes' => ['image', 'x-office/document'],
1499-
'allowedFileExtensions' => ['jpg']
1488+
'allowedFileExtensions' => ['pdf']
15001489
]])
15011490
];
15021491

15031492
$this->questionMapper->method('findByForm')->willReturn($questionEntities);
1504-
$this->mimeTypeDetector->method('getAllAliases')->willReturn([
1505-
'application/coreldraw' => 'image',
1506-
'application/msonenote' => 'x-office/document',
1507-
]);
15081493

15091494
$result = $this->formsService->getQuestions(1);
15101495

15111496
$this->assertCount(2, $result);
15121497
$this->assertEquals('text', $result[0]['type']);
15131498
$this->assertEquals(Constants::ANSWER_TYPE_FILE, $result[1]['type']);
1514-
$this->assertEquals(['application/coreldraw', 'application/msonenote', '.jpg'], $result[1]['accept']);
1499+
$this->assertEquals(['image/*', 'x-office/document', '.pdf'], $result[1]['accept']);
15151500
}
15161501

15171502
public function testGetQuestionsHandlesDoesNotExistException(): void {

0 commit comments

Comments
 (0)