Welcome to mirror list, hosted at ThFree Co, Russian Federation.

share.js « src « files_sharing « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a7660a29f58d22a8e7d245c463e911f6534b81f1 (plain)
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/**
 * Copyright (c) 2014
 *
 * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
 * @author Joas Schilling <coding@schilljs.com>
 * @author John Molakvoæ <skjnldsv@protonmail.com>
 * @author Julius Härtl <jus@bitgrid.net>
 * @author Maxence Lange <maxence@nextcloud.com>
 * @author Michael Jobst <mjobst+github@tecratech.de>
 * @author Michael Jobst <mjobst@necls.com>
 * @author Morris Jobke <hey@morrisjobke.de>
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 * @author Samuel <faust64@gmail.com>
 * @author Vincent Petry <vincent@nextcloud.com>
 *
 * @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/>.
 *
 */

/* eslint-disable */
import escapeHTML from 'escape-html'

(function() {

	_.extend(OC.Files.Client, {
		PROPERTY_SHARE_TYPES:	'{' + OC.Files.Client.NS_OWNCLOUD + '}share-types',
		PROPERTY_OWNER_ID:	'{' + OC.Files.Client.NS_OWNCLOUD + '}owner-id',
		PROPERTY_OWNER_DISPLAY_NAME:	'{' + OC.Files.Client.NS_OWNCLOUD + '}owner-display-name'
	})

	if (!OCA.Sharing) {
		OCA.Sharing = {}
	}

	/**
	 * @namespace
	 */
	OCA.Sharing.Util = {

		/**
		 * Regular expression for splitting parts of remote share owners:
		 * "user@example.com/"
		 * "user@example.com/path/to/owncloud"
		 * "user@anotherexample.com@example.com/path/to/owncloud
		 */
		_REMOTE_OWNER_REGEXP: new RegExp('^(([^@]*)@(([^@^/\\s]*)@)?)((https://)?[^[\\s/]*)([/](.*))?$'),

		/**
		 * Initialize the sharing plugin.
		 *
		 * Registers the "Share" file action and adds additional
		 * DOM attributes for the sharing file info.
		 *
		 * @param {OCA.Files.FileList} fileList file list to be extended
		 */
		attach: function(fileList) {
			// core sharing is disabled/not loaded
			if (!OC.Share) {
				return
			}
			if (fileList.id === 'trashbin' || fileList.id === 'files.public') {
				return
			}
			var fileActions = fileList.fileActions
			var oldCreateRow = fileList._createRow
			fileList._createRow = function(fileData) {

				var tr = oldCreateRow.apply(this, arguments)
				var sharePermissions = OCA.Sharing.Util.getSharePermissions(fileData)

				if (fileData.permissions === 0) {
					// no permission, disabling sidebar
					delete fileActions.actions.all.Comment
					delete fileActions.actions.all.Details
					delete fileActions.actions.all.Goto
				}
				tr.attr('data-share-permissions', sharePermissions)
				if (fileData.shareOwner) {
					tr.attr('data-share-owner', fileData.shareOwner)
					tr.attr('data-share-owner-id', fileData.shareOwnerId)
					// user should always be able to rename a mount point
					if (fileData.mountType === 'shared-root') {
						tr.attr('data-permissions', fileData.permissions | OC.PERMISSION_UPDATE)
					}
				}
				if (fileData.recipientData && !_.isEmpty(fileData.recipientData)) {
					tr.attr('data-share-recipient-data', JSON.stringify(fileData.recipientData))
				}
				if (fileData.shareTypes) {
					tr.attr('data-share-types', fileData.shareTypes.join(','))
				}
				return tr
			}

			var oldElementToFile = fileList.elementToFile
			fileList.elementToFile = function($el) {
				var fileInfo = oldElementToFile.apply(this, arguments)
				fileInfo.sharePermissions = $el.attr('data-share-permissions') || undefined
				fileInfo.shareOwner = $el.attr('data-share-owner') || undefined
				fileInfo.shareOwnerId = $el.attr('data-share-owner-id') || undefined

				if ($el.attr('data-share-types')) {
					fileInfo.shareTypes = $el.attr('data-share-types').split(',')
				}

				if ($el.attr('data-expiration')) {
					var expirationTimestamp = parseInt($el.attr('data-expiration'))
					fileInfo.shares = []
					fileInfo.shares.push({ expiration: expirationTimestamp })
				}

				return fileInfo
			}

			var oldGetWebdavProperties = fileList._getWebdavProperties
			fileList._getWebdavProperties = function() {
				var props = oldGetWebdavProperties.apply(this, arguments)
				props.push(OC.Files.Client.PROPERTY_OWNER_ID)
				props.push(OC.Files.Client.PROPERTY_OWNER_DISPLAY_NAME)
				props.push(OC.Files.Client.PROPERTY_SHARE_TYPES)
				return props
			}

			fileList.filesClient.addFileInfoParser(function(response) {
				var data = {}
				var props = response.propStat[0].properties
				var permissionsProp = props[OC.Files.Client.PROPERTY_PERMISSIONS]

				if (permissionsProp && permissionsProp.indexOf('S') >= 0) {
					data.shareOwner = props[OC.Files.Client.PROPERTY_OWNER_DISPLAY_NAME]
					data.shareOwnerId = props[OC.Files.Client.PROPERTY_OWNER_ID]
				}

				var shareTypesProp = props[OC.Files.Client.PROPERTY_SHARE_TYPES]
				if (shareTypesProp) {
					data.shareTypes = _.chain(shareTypesProp).filter(function(xmlvalue) {
						return (xmlvalue.namespaceURI === OC.Files.Client.NS_OWNCLOUD && xmlvalue.nodeName.split(':')[1] === 'share-type')
					}).map(function(xmlvalue) {
						return parseInt(xmlvalue.textContent || xmlvalue.text, 10)
					}).value()
				}

				return data
			})

			// use delegate to catch the case with multiple file lists
			fileList.$el.on('fileActionsReady', function(ev) {
				var $files = ev.$files

				_.each($files, function(file) {
					var $tr = $(file)
					var shareTypes = $tr.attr('data-share-types') || ''
					var shareOwner = $tr.attr('data-share-owner')
					if (shareTypes || shareOwner) {
						var hasLink = false
						var hasShares = false
						_.each(shareTypes.split(',') || [], function(shareType) {
							shareType = parseInt(shareType, 10)
							if (shareType === OC.Share.SHARE_TYPE_LINK) {
								hasLink = true
							} else if (shareType === OC.Share.SHARE_TYPE_EMAIL) {
								hasLink = true
							} else if (shareType === OC.Share.SHARE_TYPE_USER) {
								hasShares = true
							} else if (shareType === OC.Share.SHARE_TYPE_GROUP) {
								hasShares = true
							} else if (shareType === OC.Share.SHARE_TYPE_REMOTE) {
								hasShares = true
							} else if (shareType === OC.Share.SHARE_TYPE_REMOTE_GROUP) {
								hasShares = true
							} else if (shareType === OC.Share.SHARE_TYPE_CIRCLE) {
								hasShares = true
							} else if (shareType === OC.Share.SHARE_TYPE_ROOM) {
								hasShares = true
							} else if (shareType === OC.Share.SHARE_TYPE_DECK) {
								hasShares = true
							}
						})
						OCA.Sharing.Util._updateFileActionIcon($tr, hasShares, hasLink)
					}
				})
			})

			fileList.$el.on('changeDirectory', function() {
				OCA.Sharing.sharesLoaded = false
			})

			fileActions.registerAction({
				name: 'Share',
				displayName: function(context) {
					if (context && context.$file) {
						var shareType = parseInt(context.$file.data('share-types'), 10)
						var shareOwner = context.$file.data('share-owner-id')
						if (shareType >= 0 || shareOwner) {
							return t('files_sharing', 'Shared')
						}
					}
					return t('files_sharing', 'Share')
				},
				altText: t('files_sharing', 'Share'),
				mime: 'all',
				order: -150,
				permissions: OC.PERMISSION_ALL,
				iconClass: function(fileName, context) {
					var shareType = parseInt(context.$file.data('share-types'), 10)
					if (shareType === OC.Share.SHARE_TYPE_EMAIL
						|| shareType === OC.Share.SHARE_TYPE_LINK) {
						return 'icon-public'
					}
					return 'icon-shared'
				},
				icon: function(fileName, context) {
					var shareOwner = context.$file.data('share-owner-id')
					if (shareOwner) {
						return OC.generateUrl(`/avatar/${shareOwner}/32`)
					}
				},
				type: OCA.Files.FileActions.TYPE_INLINE,
				actionHandler: function(fileName, context) {
					// details view disabled in some share lists
					if (!fileList._detailsView) {
						return
					}
					// do not open sidebar if permission is set and equal to 0
					var permissions = parseInt(context.$file.data('share-permissions'), 10)
					if (isNaN(permissions) || permissions > 0) {
						fileList.showDetailsView(fileName, 'sharing')
					}
				},
				render: function(actionSpec, isDefault, context) {
					var permissions = parseInt(context.$file.data('permissions'), 10)
					// if no share permissions but share owner exists, still show the link
					if ((permissions & OC.PERMISSION_SHARE) !== 0 || context.$file.attr('data-share-owner')) {
						return fileActions._defaultRenderAction.call(fileActions, actionSpec, isDefault, context)
					}
					// don't render anything
					return null
				}
			})

			// register share breadcrumbs component
			var breadCrumbSharingDetailView = new OCA.Sharing.ShareBreadCrumbView()
			fileList.registerBreadCrumbDetailView(breadCrumbSharingDetailView)
		},

		/**
		 * Update file list data attributes
		 */
		_updateFileListDataAttributes: function(fileList, $tr, shareModel) {
			// files app current cannot show recipients on load, so we don't update the
			// icon when changed for consistency
			if (fileList.id === 'files') {
				return
			}
			var recipients = _.pluck(shareModel.get('shares'), 'share_with_displayname')
			// note: we only update the data attribute because updateIcon()
			if (recipients.length) {
				var recipientData = _.mapObject(shareModel.get('shares'), function(share) {
					return { shareWith: share.share_with, shareWithDisplayName: share.share_with_displayname }
				})
				$tr.attr('data-share-recipient-data', JSON.stringify(recipientData))
			} else {
				$tr.removeAttr('data-share-recipient-data')
			}
		},

		/**
		 * Update the file action share icon for the given file
		 *
		 * @param $tr file element of the file to update
		 * @param {boolean} hasUserShares true if a user share exists
		 * @param {boolean} hasLinkShares true if a link share exists
		 *
		 * @returns {boolean} true if the icon was set, false otherwise
		 */
		_updateFileActionIcon: function($tr, hasUserShares, hasLinkShares) {
			// if the statuses are loaded already, use them for the icon
			// (needed when scrolling to the next page)
			if (hasUserShares || hasLinkShares || $tr.attr('data-share-recipient-data') || $tr.attr('data-share-owner')) {
				OCA.Sharing.Util._markFileAsShared($tr, true, hasLinkShares)
				return true
			}
			return false
		},

		/**
		 * Marks/unmarks a given file as shared by changing its action icon
		 * and folder icon.
		 *
		 * @param $tr file element to mark as shared
		 * @param hasShares whether shares are available
		 * @param hasLink whether link share is available
		 */
		_markFileAsShared: function($tr, hasShares, hasLink) {
			var action = $tr.find('.fileactions .action[data-action="Share"]')
			var type = $tr.data('type')
			var icon = action.find('.icon')
			var message, recipients, avatars
			var ownerId = $tr.attr('data-share-owner-id')
			var owner = $tr.attr('data-share-owner')
			var mountType = $tr.attr('data-mounttype')
			var shareFolderIcon
			var iconClass = 'icon-shared'
			action.removeClass('shared-style')
			// update folder icon
			if (type === 'dir' && (hasShares || hasLink || ownerId)) {
				if (typeof mountType !== 'undefined' && mountType !== 'shared-root' && mountType !== 'shared') {
					shareFolderIcon = OC.MimeType.getIconUrl('dir-' + mountType)
				} else if (hasLink) {
					shareFolderIcon = OC.MimeType.getIconUrl('dir-public')
				} else {
					shareFolderIcon = OC.MimeType.getIconUrl('dir-shared')
				}
				$tr.find('.filename .thumbnail').css('background-image', 'url(' + shareFolderIcon + ')')
				$tr.attr('data-icon', shareFolderIcon)
			} else if (type === 'dir') {
				var isEncrypted = $tr.attr('data-e2eencrypted')
				// FIXME: duplicate of FileList._createRow logic for external folder,
				// need to refactor the icon logic into a single code path eventually
				if (isEncrypted === 'true') {
					shareFolderIcon = OC.MimeType.getIconUrl('dir-encrypted')
					$tr.attr('data-icon', shareFolderIcon)
				} else if (mountType && mountType.indexOf('external') === 0) {
					shareFolderIcon = OC.MimeType.getIconUrl('dir-external')
					$tr.attr('data-icon', shareFolderIcon)
				} else {
					shareFolderIcon = OC.MimeType.getIconUrl('dir')
					// back to default
					$tr.removeAttr('data-icon')
				}
				$tr.find('.filename .thumbnail').css('background-image', 'url(' + shareFolderIcon + ')')
			}
			// update share action text / icon
			if (hasShares || ownerId) {
				recipients = $tr.data('share-recipient-data')
				action.addClass('shared-style')

				avatars = '<span>' + t('files_sharing', 'Shared') + '</span>'
				// even if reshared, only show "Shared by"
				if (ownerId) {
					message = t('files_sharing', 'Shared by')
					avatars = OCA.Sharing.Util._formatRemoteShare(ownerId, owner, message)
				} else if (recipients) {
					avatars = OCA.Sharing.Util._formatShareList(recipients)
				}
				action.html(avatars).prepend(icon)

				if (ownerId || recipients) {
					var avatarElement = action.find('.avatar')
					avatarElement.each(function() {
						$(this).avatar($(this).data('username'), 32)
					})
					action.find('span[title]').tooltip({ placement: 'top' })
				}
			} else {
				action.html('<span class="hidden-visually">' + t('files_sharing', 'Shared') + '</span>').prepend(icon)
			}
			if (hasLink) {
				iconClass = 'icon-public'
			}
			icon.removeClass('icon-shared icon-public').addClass(iconClass)
		},
		/**
		 * Format a remote address
		 *
		 * @param {String} shareWith userid, full remote share, or whatever
		 * @param {String} shareWithDisplayName
		 * @param {String} message
		 * @returns {String} HTML code to display
		 */
		_formatRemoteShare: function(shareWith, shareWithDisplayName, message) {
			var parts = OCA.Sharing.Util._REMOTE_OWNER_REGEXP.exec(shareWith)
			if (!parts || !parts[7]) {
				// display avatar of the user
				var avatar = '<span class="avatar" data-username="' + escapeHTML(shareWith) + '" title="' + message + ' ' + escapeHTML(shareWithDisplayName) + '"></span>'
				var hidden = '<span class="hidden-visually">' + message + ' ' + escapeHTML(shareWithDisplayName) + '</span> '
				return avatar + hidden
			}

			var userName = parts[2]
			var userDomain = parts[4]
			var server = parts[5]
			var protocol = parts[6]
			var serverPath = parts[8] ? parts[7] : ''; // no trailing slash on root

			var tooltip = message + ' ' + userName
			if (userDomain) {
				tooltip += '@' + userDomain
			}
			if (server) {
				tooltip += '@' + server.replace(protocol, '') + serverPath
			}

			var html = '<span class="remoteAddress" title="' + escapeHTML(tooltip) + '">'
			html += '<span class="username">' + escapeHTML(userName) + '</span>'
			if (userDomain) {
				html += '<span class="userDomain">@' + escapeHTML(userDomain) + '</span>'
			}
			html += '</span> '
			return html
		},
		/**
		 * Loop over all recipients in the list and format them using
		 * all kind of fancy magic.
		 *
		* @param {Object} recipients array of all the recipients
		* @returns {String[]} modified list of recipients
		*/
		_formatShareList: function(recipients) {
			var _parent = this
			recipients = _.toArray(recipients)
			recipients.sort(function(a, b) {
				return a.shareWithDisplayName.localeCompare(b.shareWithDisplayName)
			})
			return $.map(recipients, function(recipient) {
				return _parent._formatRemoteShare(recipient.shareWith, recipient.shareWithDisplayName, t('files_sharing', 'Shared with'))
			})
		},

		/**
		 * Marks/unmarks a given file as shared by changing its action icon
		 * and folder icon.
		 *
		* @param $tr file element to mark as shared
		* @param hasShares whether shares are available
		* @param hasLink whether link share is available
		*/
		markFileAsShared: function($tr, hasShares, hasLink) {
			var action = $tr.find('.fileactions .action[data-action="Share"]')
			var type = $tr.data('type')
			var icon = action.find('.icon')
			var message, recipients, avatars
			var ownerId = $tr.attr('data-share-owner-id')
			var owner = $tr.attr('data-share-owner')
			var mountType = $tr.attr('data-mounttype')
			var shareFolderIcon
			var iconClass = 'icon-shared'
			action.removeClass('shared-style')
			// update folder icon
			if (type === 'dir' && (hasShares || hasLink || ownerId)) {
				if (typeof mountType !== 'undefined' && mountType !== 'shared-root' && mountType !== 'shared') {
					shareFolderIcon = OC.MimeType.getIconUrl('dir-' + mountType)
				} else if (hasLink) {
					shareFolderIcon = OC.MimeType.getIconUrl('dir-public')
				} else {
					shareFolderIcon = OC.MimeType.getIconUrl('dir-shared')
				}
				$tr.find('.filename .thumbnail').css('background-image', 'url(' + shareFolderIcon + ')')
				$tr.attr('data-icon', shareFolderIcon)
			} else if (type === 'dir') {
				var isEncrypted = $tr.attr('data-e2eencrypted')
				// FIXME: duplicate of FileList._createRow logic for external folder,
				// need to refactor the icon logic into a single code path eventually
				if (isEncrypted === 'true') {
					shareFolderIcon = OC.MimeType.getIconUrl('dir-encrypted')
					$tr.attr('data-icon', shareFolderIcon)
				} else if (mountType && mountType.indexOf('external') === 0) {
					shareFolderIcon = OC.MimeType.getIconUrl('dir-external')
					$tr.attr('data-icon', shareFolderIcon)
				} else {
					shareFolderIcon = OC.MimeType.getIconUrl('dir')
					// back to default
					$tr.removeAttr('data-icon')
				}
				$tr.find('.filename .thumbnail').css('background-image', 'url(' + shareFolderIcon + ')')
			}
			// update share action text / icon
			if (hasShares || ownerId) {
				recipients = $tr.data('share-recipient-data')
				action.addClass('shared-style')

				avatars = '<span>' + t('files_sharing', 'Shared') + '</span>'
				// even if reshared, only show "Shared by"
				if (ownerId) {
					message = t('files_sharing', 'Shared by')
					avatars = this._formatRemoteShare(ownerId, owner, message)
				} else if (recipients) {
					avatars = this._formatShareList(recipients)
				}
				action.html(avatars).prepend(icon)

				if (ownerId || recipients) {
					var avatarElement = action.find('.avatar')
					avatarElement.each(function() {
						$(this).avatar($(this).data('username'), 32)
					})
					action.find('span[title]').tooltip({ placement: 'top' })
				}
			} else {
				action.html('<span class="hidden-visually">' + t('files_sharing', 'Shared') + '</span>').prepend(icon)
			}
			if (hasLink) {
				iconClass = 'icon-public'
			}
			icon.removeClass('icon-shared icon-public').addClass(iconClass)
		},

		/**
		 * @param {Array} fileData
		 * @returns {String}
		 */
		getSharePermissions: function(fileData) {
			return fileData.sharePermissions
		}
	}
})()

OC.Plugins.register('OCA.Files.FileList', OCA.Sharing.Util)