Skip to content

Commit ab07ffd

Browse files
committed
feat(files): Make Files app reusable
Signed-off-by: Tomasz Trillò <tomasz.trillo@dataport.de> Assisted-by: Copilot:Claude-Sonnet-5
1 parent 0334e76 commit ab07ffd

28 files changed

Lines changed: 876 additions & 219 deletions

‎apps/files/composer/composer/autoload_classmap.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@
7171
'OCA\\Files\\Db\\TransferOwnershipMapper' => $baseDir . '/../lib/Db/TransferOwnershipMapper.php',
7272
'OCA\\Files\\DirectEditingCapabilities' => $baseDir . '/../lib/DirectEditingCapabilities.php',
7373
'OCA\\Files\\Event\\LoadAdditionalScriptsEvent' => $baseDir . '/../lib/Event/LoadAdditionalScriptsEvent.php',
74+
'OCA\\Files\\Event\\LoadFilesApp' => $baseDir . '/../lib/Event/LoadFilesApp.php',
7475
'OCA\\Files\\Event\\LoadSearchPlugins' => $baseDir . '/../lib/Event/LoadSearchPlugins.php',
7576
'OCA\\Files\\Event\\LoadSidebar' => $baseDir . '/../lib/Event/LoadSidebar.php',
7677
'OCA\\Files\\Exception\\TransferOwnershipException' => $baseDir . '/../lib/Exception/TransferOwnershipException.php',
7778
'OCA\\Files\\Helper' => $baseDir . '/../lib/Helper.php',
79+
'OCA\\Files\\Listener\\LoadFilesAppListener' => $baseDir . '/../lib/Listener/LoadFilesAppListener.php',
7880
'OCA\\Files\\Listener\\LoadSearchPluginsListener' => $baseDir . '/../lib/Listener/LoadSearchPluginsListener.php',
7981
'OCA\\Files\\Listener\\LoadSidebarListener' => $baseDir . '/../lib/Listener/LoadSidebarListener.php',
8082
'OCA\\Files\\Listener\\NodeAddedToFavoriteListener' => $baseDir . '/../lib/Listener/NodeAddedToFavoriteListener.php',

‎apps/files/composer/composer/autoload_static.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ class ComposerStaticInitFiles
8686
'OCA\\Files\\Db\\TransferOwnershipMapper' => __DIR__ . '/..' . '/../lib/Db/TransferOwnershipMapper.php',
8787
'OCA\\Files\\DirectEditingCapabilities' => __DIR__ . '/..' . '/../lib/DirectEditingCapabilities.php',
8888
'OCA\\Files\\Event\\LoadAdditionalScriptsEvent' => __DIR__ . '/..' . '/../lib/Event/LoadAdditionalScriptsEvent.php',
89+
'OCA\\Files\\Event\\LoadFilesApp' => __DIR__ . '/..' . '/../lib/Event/LoadFilesApp.php',
8990
'OCA\\Files\\Event\\LoadSearchPlugins' => __DIR__ . '/..' . '/../lib/Event/LoadSearchPlugins.php',
9091
'OCA\\Files\\Event\\LoadSidebar' => __DIR__ . '/..' . '/../lib/Event/LoadSidebar.php',
9192
'OCA\\Files\\Exception\\TransferOwnershipException' => __DIR__ . '/..' . '/../lib/Exception/TransferOwnershipException.php',
9293
'OCA\\Files\\Helper' => __DIR__ . '/..' . '/../lib/Helper.php',
94+
'OCA\\Files\\Listener\\LoadFilesAppListener' => __DIR__ . '/..' . '/../lib/Listener/LoadFilesAppListener.php',
9395
'OCA\\Files\\Listener\\LoadSearchPluginsListener' => __DIR__ . '/..' . '/../lib/Listener/LoadSearchPluginsListener.php',
9496
'OCA\\Files\\Listener\\LoadSidebarListener' => __DIR__ . '/..' . '/../lib/Listener/LoadSidebarListener.php',
9597
'OCA\\Files\\Listener\\NodeAddedToFavoriteListener' => __DIR__ . '/..' . '/../lib/Listener/NodeAddedToFavoriteListener.php',

‎apps/files/lib/AppInfo/Application.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
use OCA\Files\ConfigLexicon;
2525
use OCA\Files\Dashboard\FavoriteWidget;
2626
use OCA\Files\DirectEditingCapabilities;
27+
use OCA\Files\Event\LoadFilesApp;
2728
use OCA\Files\Event\LoadSearchPlugins;
2829
use OCA\Files\Event\LoadSidebar;
30+
use OCA\Files\Listener\LoadFilesAppListener;
2931
use OCA\Files\Listener\LoadSearchPluginsListener;
3032
use OCA\Files\Listener\LoadSidebarListener;
3133
use OCA\Files\Listener\NodeAddedToFavoriteListener;
@@ -80,6 +82,7 @@ public function register(IRegistrationContext $context): void {
8082
$context->registerCapability(AdvancedCapabilities::class);
8183
$context->registerCapability(DirectEditingCapabilities::class);
8284

85+
$context->registerEventListener(LoadFilesApp::class, LoadFilesAppListener::class);
8386
$context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class);
8487
$context->registerEventListener(RenderReferenceEvent::class, RenderReferenceEventListener::class);
8588
$context->registerEventListener(BeforeNodeRenamedEvent::class, SyncLivePhotosListener::class);

‎apps/files/lib/Controller/ViewController.php‎

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,9 @@
88

99
namespace OCA\Files\Controller;
1010

11-
use OC\Files\FilenameValidator;
1211
use OC\Files\Filesystem;
1312
use OCA\Files\AppInfo\Application;
14-
use OCA\Files\ConfigLexicon;
15-
use OCA\Files\Event\LoadAdditionalScriptsEvent;
16-
use OCA\Files\Event\LoadSearchPlugins;
17-
use OCA\Files\Event\LoadSidebar;
18-
use OCA\Files\Service\UserConfig;
19-
use OCA\Files\Service\ViewConfig;
13+
use OCA\Files\Event\LoadFilesApp;
2014
use OCP\App\IAppManager;
2115
use OCP\AppFramework\Controller;
2216
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
@@ -26,21 +20,15 @@
2620
use OCP\AppFramework\Http\RedirectResponse;
2721
use OCP\AppFramework\Http\Response;
2822
use OCP\AppFramework\Http\TemplateResponse;
29-
use OCP\AppFramework\Services\IAppConfig;
3023
use OCP\AppFramework\Services\IInitialState;
31-
use OCP\Authentication\TwoFactorAuth\IRegistry;
32-
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
3324
use OCP\EventDispatcher\IEventDispatcher;
3425
use OCP\Files\Folder;
3526
use OCP\Files\IRootFolder;
3627
use OCP\Files\NotFoundException;
37-
use OCP\Files\Template\ITemplateManager;
38-
use OCP\IConfig;
3928
use OCP\IL10N;
4029
use OCP\IRequest;
4130
use OCP\IURLGenerator;
4231
use OCP\IUserSession;
43-
use OCP\Util;
4432

4533
/**
4634
* @package OCA\Files\Controller
@@ -53,18 +41,11 @@ public function __construct(
5341
IRequest $request,
5442
private IURLGenerator $urlGenerator,
5543
private IL10N $l10n,
56-
private IConfig $config,
5744
private IEventDispatcher $eventDispatcher,
5845
private IUserSession $userSession,
5946
private IAppManager $appManager,
6047
private IRootFolder $rootFolder,
6148
private IInitialState $initialState,
62-
private ITemplateManager $templateManager,
63-
private UserConfig $userConfig,
64-
private ViewConfig $viewConfig,
65-
private FilenameValidator $filenameValidator,
66-
private IRegistry $twoFactorRegistry,
67-
private IAppConfig $appConfig,
6849
) {
6950
parent::__construct($appName, $request);
7051
}
@@ -142,10 +123,6 @@ public function index($dir = '', $view = '', $fileid = null) {
142123
}
143124
}
144125

145-
// Load the files we need
146-
Util::addInitScript('files', 'init');
147-
Util::addScript('files', 'main');
148-
149126
$user = $this->userSession->getUser();
150127
$userId = $user->getUID();
151128

@@ -174,42 +151,8 @@ public function index($dir = '', $view = '', $fileid = null) {
174151
}
175152

176153
$this->initialState->provideInitialState('storageStats', $storageInfo);
177-
$this->initialState->provideInitialState('config', $this->userConfig->getConfigs());
178-
$this->initialState->provideInitialState('viewConfigs', $this->viewConfig->getConfigs());
179-
$this->initialState->provideInitialState('recent_limit', $this->appConfig->getAppValueInt(ConfigLexicon::RECENT_LIMIT, 100));
180-
// Not yet consumed by the frontend, provided for future implementation
181-
$this->initialState->provideInitialState('group_recent_files', $this->appConfig->getAppValueBool(ConfigLexicon::GROUP_RECENT_FILES, false));
182-
$this->initialState->provideInitialState('recent_files_group_mime_types', $this->appConfig->getAppValueArray(ConfigLexicon::RECENT_FILES_GROUP_MIME_TYPES, []));
183-
$this->initialState->provideInitialState('recent_files_group_timespan_minutes', $this->appConfig->getAppValueInt(ConfigLexicon::RECENT_FILES_GROUP_TIMESPAN_MINUTES, 2));
184-
185-
// File sorting user config
186-
$filesSortingConfig = json_decode($this->config->getUserValue($userId, 'files', 'files_sorting_configs', '{}'), true);
187-
$this->initialState->provideInitialState('filesSortingConfig', $filesSortingConfig);
188-
189-
// Forbidden file characters (deprecated use capabilities)
190-
// TODO: Remove with next release of `@nextcloud/files`
191-
$forbiddenCharacters = $this->filenameValidator->getForbiddenCharacters();
192-
$this->initialState->provideInitialState('forbiddenCharacters', $forbiddenCharacters);
193-
194-
$event = new LoadAdditionalScriptsEvent();
195-
$this->eventDispatcher->dispatchTyped($event);
196-
$this->eventDispatcher->dispatchTyped(new ResourcesLoadAdditionalScriptsEvent());
197-
$this->eventDispatcher->dispatchTyped(new LoadSidebar());
198-
$this->eventDispatcher->dispatchTyped(new LoadSearchPlugins());
199-
200-
$this->initialState->provideInitialState('templates_enabled', true);
201-
$this->initialState->provideInitialState('templates_path', $this->templateManager->hasTemplateDirectory() ? $this->templateManager->getTemplatePath() : false);
202-
$this->initialState->provideInitialState('templates', $this->templateManager->listCreators());
203-
$this->initialState->provideInitialState('localClientEnabled', $this->appConfig->getAppValueBool(ConfigLexicon::LOCAL_CLIENT_INTEGRATION));
204-
205-
$isTwoFactorEnabled = false;
206-
foreach ($this->twoFactorRegistry->getProviderStates($user) as $providerId => $providerState) {
207-
if ($providerId !== 'backup_codes' && $providerState === true) {
208-
$isTwoFactorEnabled = true;
209-
}
210-
}
211154

212-
$this->initialState->provideInitialState('isTwoFactorEnabled', $isTwoFactorEnabled);
155+
$this->eventDispatcher->dispatchTyped(new LoadFilesApp());
213156

214157
$response = new TemplateResponse(
215158
Application::APP_ID,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Files\Event;
11+
12+
use OCP\EventDispatcher\Event;
13+
14+
/**
15+
* Dispatch this event from any page that needs to render the Files UI
16+
* (e.g. via `OCP.Files.renderFilesApp()`) but is not served by the Files
17+
* app's own controller. It triggers the same script/initial-state bootstrap
18+
* that `files.view.index` performs.
19+
*/
20+
class LoadFilesApp extends Event {
21+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Files\Listener;
11+
12+
use OC\Files\FilenameValidator;
13+
use OCA\Files\ConfigLexicon;
14+
use OCA\Files\Event\LoadAdditionalScriptsEvent;
15+
use OCA\Files\Event\LoadFilesApp;
16+
use OCA\Files\Event\LoadSearchPlugins;
17+
use OCA\Files\Event\LoadSidebar;
18+
use OCA\Files\Service\UserConfig;
19+
use OCA\Files\Service\ViewConfig;
20+
use OCA\Viewer\Event\LoadViewer;
21+
use OCP\AppFramework\Services\IAppConfig;
22+
use OCP\AppFramework\Services\IInitialState;
23+
use OCP\Authentication\TwoFactorAuth\IRegistry;
24+
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
25+
use OCP\EventDispatcher\Event;
26+
use OCP\EventDispatcher\IEventDispatcher;
27+
use OCP\EventDispatcher\IEventListener;
28+
use OCP\Files\Template\ITemplateManager;
29+
use OCP\IConfig;
30+
use OCP\IUserSession;
31+
use OCP\Util;
32+
33+
/**
34+
* Loads the Files app's frontend scripts and generic initial state so it can
35+
* be rendered on any page, be it the Files app's own `files.view.index`
36+
* route or a foreign page calling `OCP.Files.renderFilesApp()`.
37+
*
38+
* @template-implements IEventListener<LoadFilesApp>
39+
*/
40+
class LoadFilesAppListener implements IEventListener {
41+
public function __construct(
42+
private IEventDispatcher $eventDispatcher,
43+
private IInitialState $initialState,
44+
private IConfig $config,
45+
private IUserSession $userSession,
46+
private UserConfig $userConfig,
47+
private ViewConfig $viewConfig,
48+
private FilenameValidator $filenameValidator,
49+
private IRegistry $twoFactorRegistry,
50+
private IAppConfig $appConfig,
51+
private ITemplateManager $templateManager,
52+
) {
53+
}
54+
55+
#[\Override]
56+
public function handle(Event $event): void {
57+
if (!($event instanceof LoadFilesApp)) {
58+
return;
59+
}
60+
61+
Util::addInitScript('files', 'init');
62+
Util::addScript('files', 'main');
63+
64+
$user = $this->userSession->getUser();
65+
$userId = $user->getUID();
66+
67+
$this->initialState->provideInitialState('config', $this->userConfig->getConfigs());
68+
$this->initialState->provideInitialState('viewConfigs', $this->viewConfig->getConfigs());
69+
$this->initialState->provideInitialState('recent_limit', $this->appConfig->getAppValueInt(ConfigLexicon::RECENT_LIMIT, 100));
70+
// Not yet consumed by the frontend, provided for future implementation
71+
$this->initialState->provideInitialState('group_recent_files', $this->appConfig->getAppValueBool(ConfigLexicon::GROUP_RECENT_FILES, false));
72+
$this->initialState->provideInitialState('recent_files_group_mime_types', $this->appConfig->getAppValueArray(ConfigLexicon::RECENT_FILES_GROUP_MIME_TYPES, []));
73+
$this->initialState->provideInitialState('recent_files_group_timespan_minutes', $this->appConfig->getAppValueInt(ConfigLexicon::RECENT_FILES_GROUP_TIMESPAN_MINUTES, 2));
74+
75+
// File sorting user config
76+
$filesSortingConfig = json_decode($this->config->getUserValue($userId, 'files', 'files_sorting_configs', '{}'), true);
77+
$this->initialState->provideInitialState('filesSortingConfig', $filesSortingConfig);
78+
79+
// Forbidden file characters (deprecated use capabilities)
80+
// TODO: Remove with next release of `@nextcloud/files`
81+
$forbiddenCharacters = $this->filenameValidator->getForbiddenCharacters();
82+
$this->initialState->provideInitialState('forbiddenCharacters', $forbiddenCharacters);
83+
84+
$this->eventDispatcher->dispatchTyped(new LoadAdditionalScriptsEvent());
85+
$this->eventDispatcher->dispatchTyped(new ResourcesLoadAdditionalScriptsEvent());
86+
$this->eventDispatcher->dispatchTyped(new LoadSidebar());
87+
$this->eventDispatcher->dispatchTyped(new LoadSearchPlugins());
88+
// Load Viewer scripts
89+
if (class_exists(LoadViewer::class)) {
90+
$this->eventDispatcher->dispatchTyped(new LoadViewer());
91+
}
92+
93+
$this->initialState->provideInitialState('templates_enabled', true);
94+
$this->initialState->provideInitialState('templates_path', $this->templateManager->hasTemplateDirectory() ? $this->templateManager->getTemplatePath() : false);
95+
$this->initialState->provideInitialState('templates', $this->templateManager->listCreators());
96+
$this->initialState->provideInitialState('localClientEnabled', $this->appConfig->getAppValueBool(ConfigLexicon::LOCAL_CLIENT_INTEGRATION));
97+
98+
$isTwoFactorEnabled = false;
99+
foreach ($this->twoFactorRegistry->getProviderStates($user) as $providerId => $providerState) {
100+
if ($providerId !== 'backup_codes' && $providerState === true) {
101+
$isTwoFactorEnabled = true;
102+
}
103+
}
104+
105+
$this->initialState->provideInitialState('isTwoFactorEnabled', $isTwoFactorEnabled);
106+
}
107+
}

‎apps/files/src/components/BreadCrumbs.vue‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import { useFilesStore } from '../store/files.ts'
8383
import { usePathsStore } from '../store/paths.ts'
8484
import { useSelectionStore } from '../store/selection.ts'
8585
import { useUploaderStore } from '../store/uploader.ts'
86+
import { getBreadcrumbDirs } from '../utils/breadcrumbs.ts'
8687
import { logger } from '../utils/logger.ts'
8788
8889
export default defineComponent({
@@ -100,6 +101,12 @@ export default defineComponent({
100101
type: String,
101102
default: '/',
102103
},
104+
105+
/** The directory to start the breadcrumbs with */
106+
root: {
107+
type: String,
108+
default: '/',
109+
},
103110
},
104111
105112
emits: ['reload'],
@@ -159,11 +166,7 @@ export default defineComponent({
159166
160167
computed: {
161168
dirs(): string[] {
162-
const cumulativePath = (acc: string) => (value: string) => (acc += `${value}/`)
163-
// Generate a cumulative path for each path segment: ['/', '/foo', '/foo/bar', ...] etc
164-
const paths: string[] = this.path.split('/').filter(Boolean).map(cumulativePath('/'))
165-
// Strip away trailing slash
166-
return ['/', ...paths.map((path: string) => path.replace(/^(.+)\/$/, '$1'))]
169+
return getBreadcrumbDirs(this.path, this.root)
167170
},
168171
169172
sections() {

‎apps/files/src/components/FileEntry/FileEntryActions.vue‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ export default defineComponent({
374374
<style lang="scss">
375375
// Allow right click to define the position of the menu
376376
// only if defined
377-
main.app-content[style*="mouse-pos-x"] .v-popper__popper {
377+
.app-content[style*="mouse-pos-x"] .v-popper__popper {
378378
transform: translate3d(var(--mouse-pos-x), var(--mouse-pos-y), 0px) !important;
379379
380380
// If the menu is too close to the bottom, we move it up

‎apps/files/src/components/FileEntryGrid.spec.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { shallowMount } from '@vue/test-utils'
99
import { beforeEach, describe, expect, test, vi } from 'vitest'
1010
import { nextTick, toRaw } from 'vue'
1111
import FileEntryGrid from './FileEntryGrid.vue'
12-
import { router } from '../router/router.ts'
12+
import { getFilesAppRouter } from '../router/router.ts'
1313
import { useActiveStore } from '../store/active.ts'
1414

1515
// useFileListWidth builds its ResizeObserver while the module is evaluated, so
@@ -25,6 +25,8 @@ vi.hoisted(() => {
2525

2626
vi.mock('@nextcloud/auth')
2727

28+
const router = getFilesAppRouter()
29+
2830
const source = new File({
2931
id: 42,
3032
source: 'http://nextcloud.local/remote.php/dav/files/test/Deep/Nested/report.pdf',

‎apps/files/src/components/FileEntryMixin.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ export default defineComponent({
275275
if (this.actionsMenuStore.opened === null) {
276276
// Reset any right menu position potentially set
277277
logger.debug('All actions menu closed, resetting right menu position...')
278-
const root = this.$el?.closest('main.app-content') as HTMLElement
278+
const root = this.$el?.closest('.app-content') as HTMLElement
279279
if (root !== null) {
280280
root.style.removeProperty('--mouse-pos-x')
281281
root.style.removeProperty('--mouse-pos-y')
@@ -313,7 +313,7 @@ export default defineComponent({
313313
// the actions menu mouse position
314314
if (!this.gridMode) {
315315
// Actions menu is contained within the app content
316-
const root = this.$el?.closest('main.app-content') as HTMLElement
316+
const root = this.$el?.closest('.app-content') as HTMLElement
317317
const contentRect = root.getBoundingClientRect()
318318
// Using Math.min/max to prevent the menu from going out of the AppContent
319319
// 200 = max width of the menu
@@ -322,7 +322,7 @@ export default defineComponent({
322322
root.style.setProperty('--mouse-pos-y', Math.max(0, event.clientY - contentRect.top) + 'px')
323323
} else {
324324
// Reset any right menu position potentially set
325-
const root = this.$el?.closest('main.app-content') as HTMLElement
325+
const root = this.$el?.closest('.app-content') as HTMLElement
326326
root.style.removeProperty('--mouse-pos-x')
327327
root.style.removeProperty('--mouse-pos-y')
328328
}

0 commit comments

Comments
 (0)