Skip to content

Commit ba54e5b

Browse files
committed
Implement file reference wiget
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 41b2e17 commit ba54e5b

16 files changed

+371
-4
lines changed

apps/files/composer/composer/autoload_classmap.php

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
'OCA\\Files\\Helper' => $baseDir . '/../lib/Helper.php',
5151
'OCA\\Files\\Listener\\LegacyLoadAdditionalScriptsAdapter' => $baseDir . '/../lib/Listener/LegacyLoadAdditionalScriptsAdapter.php',
5252
'OCA\\Files\\Listener\\LoadSidebarListener' => $baseDir . '/../lib/Listener/LoadSidebarListener.php',
53+
'OCA\\Files\\Listener\\RenderReferenceEventListener' => $baseDir . '/../lib/Listener/RenderReferenceEventListener.php',
5354
'OCA\\Files\\Migration\\Version11301Date20191205150729' => $baseDir . '/../lib/Migration/Version11301Date20191205150729.php',
5455
'OCA\\Files\\Migration\\Version12101Date20221011153334' => $baseDir . '/../lib/Migration/Version12101Date20221011153334.php',
5556
'OCA\\Files\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php',

apps/files/composer/composer/autoload_static.php

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class ComposerStaticInitFiles
6565
'OCA\\Files\\Helper' => __DIR__ . '/..' . '/../lib/Helper.php',
6666
'OCA\\Files\\Listener\\LegacyLoadAdditionalScriptsAdapter' => __DIR__ . '/..' . '/../lib/Listener/LegacyLoadAdditionalScriptsAdapter.php',
6767
'OCA\\Files\\Listener\\LoadSidebarListener' => __DIR__ . '/..' . '/../lib/Listener/LoadSidebarListener.php',
68+
'OCA\\Files\\Listener\\RenderReferenceEventListener' => __DIR__ . '/..' . '/../lib/Listener/RenderReferenceEventListener.php',
6869
'OCA\\Files\\Migration\\Version11301Date20191205150729' => __DIR__ . '/..' . '/../lib/Migration/Version11301Date20191205150729.php',
6970
'OCA\\Files\\Migration\\Version12101Date20221011153334' => __DIR__ . '/..' . '/../lib/Migration/Version12101Date20221011153334.php',
7071
'OCA\\Files\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php',

apps/files/lib/AppInfo/Application.php

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
use OCA\Files\Event\LoadSidebar;
4545
use OCA\Files\Listener\LegacyLoadAdditionalScriptsAdapter;
4646
use OCA\Files\Listener\LoadSidebarListener;
47+
use OCA\Files\Listener\RenderReferenceEventListener;
4748
use OCA\Files\Notification\Notifier;
4849
use OCA\Files\Search\FilesSearchProvider;
4950
use OCA\Files\Service\TagService;
@@ -53,6 +54,7 @@
5354
use OCP\AppFramework\Bootstrap\IBootContext;
5455
use OCP\AppFramework\Bootstrap\IBootstrap;
5556
use OCP\AppFramework\Bootstrap\IRegistrationContext;
57+
use OCP\Collaboration\Reference\RenderReferenceEvent;
5658
use OCP\Collaboration\Resources\IProviderManager;
5759
use OCP\IConfig;
5860
use OCP\IL10N;
@@ -118,6 +120,7 @@ public function register(IRegistrationContext $context): void {
118120

119121
$context->registerEventListener(LoadAdditionalScriptsEvent::class, LegacyLoadAdditionalScriptsAdapter::class);
120122
$context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class);
123+
$context->registerEventListener(RenderReferenceEvent::class, RenderReferenceEventListener::class);
121124

122125
$context->registerSearchProvider(FilesSearchProvider::class);
123126

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
6+
*
7+
* @author Julius Härtl <jus@bitgrid.net>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*/
24+
25+
namespace OCA\Files\Listener;
26+
27+
use OCP\Collaboration\Reference\RenderReferenceEvent;
28+
use OCP\EventDispatcher\Event;
29+
use OCP\EventDispatcher\IEventListener;
30+
31+
class RenderReferenceEventListener implements IEventListener {
32+
public function handle(Event $event): void {
33+
if (!$event instanceof RenderReferenceEvent) {
34+
return;
35+
}
36+
37+
\OCP\Util::addScript('files', 'reference-files');
38+
}
39+
}

apps/files/src/reference-files.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
3+
*
4+
* @author Julius Härtl <jus@bitgrid.net>
5+
*
6+
* @license GNU AGPL version 3 or any later version
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
import Vue from 'vue'
23+
import { translate as t } from '@nextcloud/l10n'
24+
25+
import { registerWidget } from '@nextcloud/vue-richtext'
26+
27+
import FileWidget from './views/ReferenceFileWidget.vue'
28+
29+
Vue.mixin({
30+
methods: {
31+
t,
32+
},
33+
})
34+
35+
registerWidget('file', (el, { richObjectType, richObject, accessible }) => {
36+
const Widget = Vue.extend(FileWidget)
37+
new Widget({
38+
propsData: {
39+
richObjectType,
40+
richObject,
41+
accessible,
42+
},
43+
}).$mount(el)
44+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<!--
2+
- @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
3+
-
4+
- @author Julius Härtl <jus@bitgrid.net>
5+
-
6+
- @license GNU AGPL version 3 or any later version
7+
-
8+
- This program is free software: you can redistribute it and/or modify
9+
- it under the terms of the GNU Affero General Public License as
10+
- published by the Free Software Foundation, either version 3 of the
11+
- License, or (at your option) any later version.
12+
-
13+
- This program is distributed in the hope that it will be useful,
14+
- but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
- GNU Affero General Public License for more details.
17+
-
18+
- You should have received a copy of the GNU Affero General Public License
19+
- along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
-->
21+
22+
<template>
23+
<div v-if="!accessible" class="widget-file widget-file--no-access">
24+
<div class="widget-file--image widget-file--image--icon icon-folder" />
25+
<div class="widget-file--details">
26+
<p class="widget-file--title">{{ t('files', 'File cannot be accessed') }}</p>
27+
<p class="widget-file--description">{{ t('files', 'You might not have have permissions to view it, ask the sender to share it') }}</p>
28+
</div>
29+
</div>
30+
<a v-else
31+
class="widget-file"
32+
:href="richObject.link"
33+
@click.prevent="navigate">
34+
<div class="widget-file--image" :class="filePreviewClass" :style="filePreview" />
35+
<div class="widget-file--details">
36+
<p class="widget-file--title">{{ richObject.name }}</p>
37+
<p class="widget-file--description">{{ fileSize }}<br>{{ fileMtime }}</p>
38+
<p class="widget-file--link">{{ filePath }}</p>
39+
</div>
40+
</a>
41+
</template>
42+
<script>
43+
import { generateUrl } from '@nextcloud/router'
44+
import path from 'path'
45+
46+
export default {
47+
name: 'ReferenceFileWidget',
48+
props: {
49+
richObject: {
50+
type: Object,
51+
required: true,
52+
},
53+
accessible: {
54+
type: Boolean,
55+
default: true,
56+
},
57+
},
58+
data() {
59+
return {
60+
previewUrl: window.OC.MimeType.getIconUrl(this.richObject.mimetype),
61+
}
62+
},
63+
computed: {
64+
fileSize() {
65+
return window.OC.Util.humanFileSize(this.richObject.size)
66+
},
67+
fileMtime() {
68+
return window.OC.Util.relativeModifiedDate(this.richObject.mtime * 1000)
69+
},
70+
filePath() {
71+
return path.dirname(this.richObject.path)
72+
},
73+
filePreview() {
74+
if (this.previewUrl) {
75+
return {
76+
backgroundImage: 'url(' + this.previewUrl + ')',
77+
}
78+
}
79+
80+
return {
81+
backgroundImage: 'url(' + window.OC.MimeType.getIconUrl(this.richObject.mimetype) + ')',
82+
}
83+
84+
},
85+
filePreviewClass() {
86+
if (this.previewUrl) {
87+
return 'widget-file--image--preview'
88+
}
89+
return 'widget-file--image--icon'
90+
91+
},
92+
},
93+
mounted() {
94+
if (this.richObject['preview-available']) {
95+
const previewUrl = generateUrl('/core/preview?fileId={fileId}&x=250&y=250', {
96+
fileId: this.richObject.id,
97+
})
98+
const img = new Image()
99+
img.onload = () => {
100+
this.previewUrl = previewUrl
101+
}
102+
img.onerror = err => {
103+
console.error('could not load recommendation preview', err)
104+
}
105+
img.src = previewUrl
106+
}
107+
},
108+
methods: {
109+
navigate() {
110+
if (OCA.Viewer && OCA.Viewer.mimetypes.indexOf(this.richObject.mimetype) !== -1) {
111+
OCA.Viewer.open({ path: this.richObject.path })
112+
return
113+
}
114+
window.location = generateUrl('/f/' + this.id)
115+
},
116+
},
117+
}
118+
</script>
119+
<style lang="scss" scoped>
120+
.widget-file {
121+
display: flex;
122+
123+
&--image {
124+
min-width: 40%;
125+
background-position: center;
126+
background-size: cover;
127+
background-repeat: no-repeat;
128+
129+
&.widget-file--image--icon {
130+
min-width: 88px;
131+
background-size: 44px;
132+
}
133+
}
134+
135+
&--title {
136+
overflow: hidden;
137+
text-overflow: ellipsis;
138+
white-space: nowrap;
139+
font-weight: bold;
140+
}
141+
142+
&--details {
143+
padding: 12px;
144+
flex-grow: 1;
145+
display: flex;
146+
flex-direction: column;
147+
148+
p {
149+
margin: 0;
150+
padding: 0;
151+
}
152+
}
153+
154+
&--description {
155+
overflow: hidden;
156+
text-overflow: ellipsis;
157+
display: -webkit-box;
158+
-webkit-line-clamp: 3;
159+
line-clamp: 3;
160+
-webkit-box-orient: vertical;
161+
}
162+
163+
&--link {
164+
color: var(--color-text-maxcontrast);
165+
}
166+
167+
&.widget-file--no-access {
168+
padding: 12px;
169+
170+
.widget-file--details {
171+
padding: 0;
172+
}
173+
}
174+
}
175+
</style>

dist/core-common.js

+2-2
Large diffs are not rendered by default.

dist/core-common.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/files-reference-files.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
3+
*
4+
* @author Julius Härtl <jus@bitgrid.net>
5+
*
6+
* @license GNU AGPL version 3 or any later version
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*/

dist/files-reference-files.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/private/Collaboration/Reference/File/FileReferenceProvider.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,10 @@ private function fetchReference(Reference $reference): void {
145145
'id' => $file->getId(),
146146
'name' => $file->getName(),
147147
'size' => $file->getSize(),
148-
'path' => $file->getPath(),
148+
'path' => $userFolder->getRelativePath($file->getPath()),
149149
'link' => $reference->getUrl(),
150150
'mimetype' => $file->getMimetype(),
151+
'mtime' => $file->getMTime(),
151152
'preview-available' => $this->previewManager->isAvailable($file)
152153
]);
153154
} catch (InvalidPathException|NotFoundException|NotPermittedException|NoUserException $e) {

lib/public/RichObjectStrings/Definitions.php

+6
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ class Definitions {
347347
'description' => 'Whether or not a preview is available. If `no` the mimetype icon should be used',
348348
'example' => 'yes',
349349
],
350+
'mtime' => [
351+
'since' => '25.0.0',
352+
'required' => false,
353+
'description' => 'The mtime of the file/folder as unix timestamp',
354+
'example' => '1661854213',
355+
],
350356
],
351357
],
352358
'forms-form' => [

0 commit comments

Comments
 (0)