1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
<!--
- @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
-
- @author Julius Härtl <jus@bitgrid.net>
-
- @license GNU AGPL version 3 or any later version
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<template>
<div v-if="!accessible" class="widget-file widget-file--no-access">
<span class="widget-file__image widget-file__image--icon">
<FolderIcon v-if="isFolder" :size="88" />
<FileIcon v-else :size="88" />
</span>
<span class="widget-file__details">
<p class="widget-file__title">
{{ t('files', 'File cannot be accessed') }}
</p>
<p class="widget-file__description">
{{ t('files', 'The file could not be found or you do not have permissions to view it. Ask the sender to share it.') }}
</p>
</span>
</div>
<!-- Live preview if a handler is available -->
<component :is="viewerHandler.component"
v-else-if="interactive && viewerHandler && !failedViewer"
:active="false /* prevent video from autoplaying */"
:can-swipe="false"
:can-zoom="false"
:is-embedded="true"
v-bind="viewerFile"
:file-list="[viewerFile]"
:is-full-screen="false"
:is-sidebar-shown="false"
class="widget-file widget-file--interactive"
@error="failedViewer = true" />
<!-- The file is accessible -->
<a v-else
class="widget-file widget-file--link"
:href="richObject.link"
target="_blank"
@click="navigate">
<span class="widget-file__image" :class="filePreviewClass" :style="filePreviewStyle">
<template v-if="!previewUrl">
<FolderIcon v-if="isFolder" :size="88" fill-color="var(--color-primary-element)" />
<FileIcon v-else :size="88" />
</template>
</span>
<span class="widget-file__details">
<p class="widget-file__title">{{ richObject.name }}</p>
<p class="widget-file__description">{{ fileSize }}<br>{{ fileMtime }}</p>
<p class="widget-file__link">{{ filePath }}</p>
</span>
</a>
</template>
<script lang="ts">
import { defineComponent, type Component, type PropType } from 'vue'
import { generateRemoteUrl, generateUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { getFilePickerBuilder } from '@nextcloud/dialogs'
import { Node } from '@nextcloud/files'
import FileIcon from 'vue-material-design-icons/File.vue'
import FolderIcon from 'vue-material-design-icons/Folder.vue'
import path from 'path'
// see lib/private/Collaboration/Reference/File/FileReferenceProvider.php
type Ressource = {
id: number
name: string
size: number
path: string
link: string
mimetype: string
mtime: number // as unix timestamp
'preview-available': boolean
}
type ViewerHandler = {
id: string
group: string
mimes: string[]
component: Component
}
/**
* Minimal mock of the legacy Viewer FileInfo
* TODO: replace by Node object
*/
type ViewerFile = {
filename: string // the path to the root folder
basename: string // the file name
lastmod: Date // the last modification date
size: number // the file size in bytes
type: string
mime: string
fileid: number
failed: boolean
loaded: boolean
davPath: string
source: string
}
export default defineComponent({
name: 'ReferenceFileWidget',
components: {
FolderIcon,
FileIcon,
},
props: {
richObject: {
type: Object as PropType<Ressource>,
required: true,
},
accessible: {
type: Boolean,
default: true,
},
interactive: {
type: Boolean,
default: true,
},
},
data() {
return {
previewUrl: null as string | null,
failedViewer: false,
}
},
computed: {
availableViewerHandlers(): ViewerHandler[] {
return (window?.OCA?.Viewer?.availableHandlers || []) as ViewerHandler[]
},
viewerHandler(): ViewerHandler | undefined {
return this.availableViewerHandlers
.find(handler => handler.mimes.includes(this.richObject.mimetype))
},
viewerFile(): ViewerFile {
const davSource = generateRemoteUrl(`dav/files/${getCurrentUser()?.uid}/${this.richObject.path}`)
.replace(/\/\/$/, '/')
return {
filename: this.richObject.path,
basename: this.richObject.name,
lastmod: new Date(this.richObject.mtime * 1000),
size: this.richObject.size,
type: 'file',
mime: this.richObject.mimetype,
fileid: this.richObject.id,
failed: false,
loaded: true,
davPath: davSource,
source: davSource,
}
},
fileSize() {
return window.OC.Util.humanFileSize(this.richObject.size)
},
fileMtime() {
return window.OC.Util.relativeModifiedDate(this.richObject.mtime * 1000)
},
filePath() {
return path.dirname(this.richObject.path)
},
filePreviewStyle() {
if (this.previewUrl) {
return {
backgroundImage: 'url(' + this.previewUrl + ')',
}
}
return {}
},
filePreviewClass() {
if (this.previewUrl) {
return 'widget-file__image--preview'
}
return 'widget-file__image--icon'
},
isFolder() {
return this.richObject.mimetype === 'httpd/unix-directory'
},
},
mounted() {
if (this.richObject['preview-available']) {
const previewUrl = generateUrl('/core/preview?fileId={fileId}&x=250&y=250', {
fileId: this.richObject.id,
})
const img = new Image()
img.onload = () => {
this.previewUrl = previewUrl
}
img.onerror = err => {
console.error('could not load recommendation preview', err)
}
img.src = previewUrl
}
},
methods: {
navigate(event) {
if (this.isFolder) {
event.stopPropagation()
event.preventDefault()
this.openFilePicker()
} else if (window?.OCA?.Viewer?.mimetypes.indexOf(this.richObject.mimetype) !== -1 && !window?.OCA?.Viewer?.file) {
event.stopPropagation()
event.preventDefault()
window?.OCA?.Viewer?.open({ path: this.richObject.path })
}
},
openFilePicker() {
const picker = getFilePickerBuilder(t('settings', 'Your files'))
.allowDirectories(true)
.setMultiSelect(false)
.addButton({
id: 'open',
label: this.t('settings', 'Open in files'),
callback(nodes: Node[]) {
if (nodes[0]) {
window.open(generateUrl('/f/{fileid}', {
fileid: nodes[0].fileid,
}))
}
},
type: 'primary',
})
.disableNavigation()
.startAt(this.richObject.path)
.build()
picker.pick()
},
},
})
</script>
<style lang="scss" scoped>
.widget-file {
display: flex;
flex-grow: 1;
color: var(--color-main-text) !important;
text-decoration: none !important;
padding: 0 !important;
&__image {
width: 30%;
min-width: 160px;
max-width: 320px;
background-position: center;
background-size: cover;
background-repeat: no-repeat;
&--icon {
min-width: 88px;
max-width: 88px;
padding: 12px;
padding-right: 0;
display: flex;
align-items: center;
justify-content: center;
}
}
&__title {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-weight: bold;
}
&__details {
padding: 12px;
flex-grow: 1;
display: flex;
flex-direction: column;
p {
margin: 0;
padding: 0;
}
}
&__description {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
line-clamp: 3;
-webkit-box-orient: vertical;
}
// No preview, standard link to ressource
&--link {
color: var(--color-text-maxcontrast);
}
&--interactive {
position: relative;
height: 400px;
max-height: 50vh;
margin: 0;
}
}
</style>
|