Skip to content

Commit 9d79035

Browse files
Merge pull request #7229 from nextcloud/fix-default-file-extension-in-direct-editing
Respect default extension setting in direct editing
2 parents 6764a5e + 8ebe61f commit 9d79035

5 files changed

Lines changed: 113 additions & 10 deletions

File tree

composer/autoload.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
echo $err;
1515
}
1616
}
17-
trigger_error(
18-
$err,
19-
E_USER_ERROR
20-
);
17+
throw new RuntimeException($err);
2118
}
2219

2320
require_once __DIR__ . '/composer/autoload_real.php';

composer/composer/InstalledVersions.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
*/
2727
class InstalledVersions
2828
{
29+
/**
30+
* @var string|null if set (by reflection by Composer), this should be set to the path where this class is being copied to
31+
* @internal
32+
*/
33+
private static $selfDir = null;
34+
2935
/**
3036
* @var mixed[]|null
3137
* @psalm-var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}|array{}|null
@@ -322,6 +328,18 @@ public static function reload($data)
322328
self::$installedIsLocalDir = false;
323329
}
324330

331+
/**
332+
* @return string
333+
*/
334+
private static function getSelfDir()
335+
{
336+
if (self::$selfDir === null) {
337+
self::$selfDir = strtr(__DIR__, '\\', '/');
338+
}
339+
340+
return self::$selfDir;
341+
}
342+
325343
/**
326344
* @return array[]
327345
* @psalm-return list<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
@@ -336,7 +354,7 @@ private static function getInstalled()
336354
$copiedLocalDir = false;
337355

338356
if (self::$canGetVendors) {
339-
$selfDir = strtr(__DIR__, '\\', '/');
357+
$selfDir = self::getSelfDir();
340358
foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
341359
$vendorDir = strtr($vendorDir, '\\', '/');
342360
if (isset(self::$installedByVendor[$vendorDir])) {

lib/DirectEditing/TextDirectEditor.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use OCP\Files\InvalidPathException;
1818
use OCP\Files\NotFoundException;
1919
use OCP\Files\NotPermittedException;
20+
use OCP\IAppConfig;
2021
use OCP\IL10N;
2122
use OCP\Util;
2223

@@ -31,10 +32,16 @@ class TextDirectEditor implements IEditor {
3132
/** @var ApiService */
3233
private $apiService;
3334

34-
public function __construct(IL10N $l10n, InitialStateProvider $initialStateProvider, ApiService $apiService) {
35+
/**
36+
* @var IAppConfig
37+
*/
38+
private $appConfig;
39+
40+
public function __construct(IL10N $l10n, InitialStateProvider $initialStateProvider, ApiService $apiService, IAppConfig $appConfig) {
3541
$this->l10n = $l10n;
3642
$this->initialStateProvider = $initialStateProvider;
3743
$this->apiService = $apiService;
44+
$this->appConfig = $appConfig;
3845
}
3946

4047
/**
@@ -109,7 +116,7 @@ public function getMimetypesOptional(): array {
109116
*/
110117
public function getCreators(): array {
111118
return [
112-
new TextDocumentCreator($this->l10n),
119+
new TextDocumentCreator($this->l10n, $this->appConfig),
113120
];
114121
}
115122

lib/DirectEditing/TextDocumentCreator.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace OCA\Text\DirectEditing;
88

99
use OCP\DirectEditing\ACreateEmpty;
10+
use OCP\IAppConfig;
1011
use OCP\IL10N;
1112

1213
class TextDocumentCreator extends ACreateEmpty {
@@ -17,8 +18,14 @@ class TextDocumentCreator extends ACreateEmpty {
1718
*/
1819
private $l10n;
1920

20-
public function __construct(IL10N $l10n) {
21+
/**
22+
* @var IAppConfig
23+
*/
24+
private $appConfig;
25+
26+
public function __construct(IL10N $l10n, IAppConfig $appConfig) {
2127
$this->l10n = $l10n;
28+
$this->appConfig = $appConfig;
2229
}
2330

2431
public function getId(): string {
@@ -30,10 +37,16 @@ public function getName(): string {
3037
}
3138

3239
public function getExtension(): string {
33-
return 'md';
40+
return $this->appConfig->getValueString('text', 'default_file_extension', 'md');
3441
}
3542

3643
public function getMimetype(): string {
37-
return 'text/markdown';
44+
switch ($this->getExtension()) {
45+
case 'txt':
46+
return 'text/plain';
47+
case 'md':
48+
default:
49+
return 'text/markdown';
50+
}
3851
}
3952
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
namespace OCA\Text\Tests;
9+
10+
use OCA\Text\DirectEditing\TextDocumentCreator;
11+
use OCP\IAppConfig;
12+
use OCP\IL10N;
13+
14+
class TextDocumentCreatorTest extends \PHPUnit\Framework\TestCase {
15+
private TextDocumentCreator $textDocumentCreator;
16+
17+
private IL10N $l10n;
18+
19+
private IAppConfig $appConfig;
20+
21+
protected function setUp(): void {
22+
$this->l10n = $this->createMock(IL10N::class);
23+
$this->appConfig = $this->createMock(IAppConfig::class);
24+
$this->textDocumentCreator = new TextDocumentCreator($this->l10n, $this->appConfig);
25+
}
26+
27+
public function testGetId(): void {
28+
$this->assertEquals('textdocument', $this->textDocumentCreator->getId());
29+
}
30+
31+
public function testGetName(): void {
32+
$this->l10n->expects($this->once())
33+
->method('t')
34+
->with('text document')
35+
->willReturn('text document');
36+
$this->assertEquals('text document', $this->textDocumentCreator->getName());
37+
}
38+
39+
public function testGetDefaultExtension(): void {
40+
$this->appConfig->expects($this->once())
41+
->method('getValueString')
42+
->with('text', 'default_file_extension', 'md')
43+
->willReturn('md');
44+
$this->assertEquals('md', $this->textDocumentCreator->getExtension());
45+
}
46+
47+
public function testGetExtensionFromConfig(): void {
48+
$this->appConfig->expects($this->once())
49+
->method('getValueString')
50+
->with('text', 'default_file_extension', 'md')
51+
->willReturn('txt');
52+
53+
$this->assertEquals('txt', $this->textDocumentCreator->getExtension());
54+
}
55+
56+
public function testGetDefaultMimetype(): void {
57+
$this->assertEquals('text/markdown', $this->textDocumentCreator->getMimetype());
58+
}
59+
60+
public function testGetMimetypeFromConfig(): void {
61+
$this->appConfig->expects($this->once())
62+
->method('getValueString')
63+
->with('text', 'default_file_extension', 'md')
64+
->willReturn('txt');
65+
66+
$this->assertEquals('text/plain', $this->textDocumentCreator->getMimetype());
67+
}
68+
}

0 commit comments

Comments
 (0)