aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_trashbin/src/filelist.js
blob: e113b0f0ffa380892efbe25f8514f75b2e6f2ed8 (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
/**
 * Copyright (c) 2014
 *
 * @author Azul <azul@riseup.net>
 * @author Gary Kim <gary@garykim.dev>
 * @author Jan C. Borchardt <hey@jancborchardt.net>
 * @author John Molakvoæ <skjnldsv@protonmail.com>
 * @author Robin Appelman <robin@icewind.nl>
 * @author Vincent Petry <vincent@nextcloud.com>
 *
 * @license AGPL-3.0-or-later
 *
 * 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-next-line import/no-unresolved, node/no-missing-import
import PQueue from 'p-queue'

/* eslint-disable */
(function() {
	var DELETED_REGEXP = new RegExp(/^(.+)\.d[0-9]+$/)
	var FILENAME_PROP = '{http://nextcloud.org/ns}trashbin-filename'
	var DELETION_TIME_PROP = '{http://nextcloud.org/ns}trashbin-deletion-time'
	var TRASHBIN_ORIGINAL_LOCATION = '{http://nextcloud.org/ns}trashbin-original-location'
	var TRASHBIN_TITLE = '{http://nextcloud.org/ns}trashbin-title'

	/**
	 * Convert a file name in the format filename.d12345 to the real file name.
	 * This will use basename.
	 * The name will not be changed if it has no ".d12345" suffix.
	 * @param {String} name file name
	 * @returns {String} converted file name
	 */
	function getDeletedFileName(name) {
		name = OC.basename(name)
		var match = DELETED_REGEXP.exec(name)
		if (match && match.length > 1) {
			name = match[1]
		}
		return name
	}

	/**
	 * @class OCA.Trashbin.FileList
	 * @augments OCA.Files.FileList
	 * @classdesc List of deleted files
	 *
	 * @param $el container element with existing markup for the .files-controls
	 * and a table
	 * @param [options] map of options
	 */
	var FileList = function($el, options) {
		this.client = options.client
		this.initialize($el, options)
		this.deleteOperationQueue = new PQueue({ concurrency: 4 })
	}
	FileList.prototype = _.extend({}, OCA.Files.FileList.prototype,
		/** @lends OCA.Trashbin.FileList.prototype */ {
		id: 'trashbin',
		appName: t('files_trashbin', 'Deleted files'),
		/** @type {OC.Files.Client} */
		client: null,

		/**
		 * @private
		 */
		initialize: function() {
			this.client.addFileInfoParser(function(response, data) {
				var props = response.propStat[0].properties
				var path = props[TRASHBIN_ORIGINAL_LOCATION]
				var title = props[TRASHBIN_TITLE]
				return {
					displayName: props[FILENAME_PROP],
					mtime: parseInt(props[DELETION_TIME_PROP], 10) * 1000,
					hasPreview: true,
					path: path,
					extraData: title
				}
			})

			var result = OCA.Files.FileList.prototype.initialize.apply(this, arguments)
			this.$el.find('.undelete').click('click', _.bind(this._onClickRestoreSelected, this))

			// Sort by most recently deleted first
			this.setSort('mtime', 'desc')

			/**
			 * Override crumb making to add "Deleted Files" entry
			 * and convert files with ".d" extensions to a more
			 * user friendly name.
			 */
				this.breadcrumb._makeCrumbs = function() {
					var parts = OCA.Files.BreadCrumb.prototype._makeCrumbs.apply(this, [...arguments, 'icon-delete no-hover'])
					for (var i = 1; i < parts.length; i++) {
						parts[i].name = getDeletedFileName(parts[i].name)
					}
					return parts
				}

				OC.Plugins.attach('OCA.Trashbin.FileList', this)
				return result
			},

			/**
		 * Override to only return read permissions
		 */
			getDirectoryPermissions: function() {
				return OC.PERMISSION_READ | OC.PERMISSION_DELETE
			},

			_setCurrentDir: function(targetDir) {
				OCA.Files.FileList.prototype._setCurrentDir.apply(this, arguments)

				var baseDir = OC.basename(targetDir)
				if (baseDir !== '') {
					this.setPageTitle(getDeletedFileName(baseDir))
				}
			},

			_createRow: function() {
			// FIXME: MEGAHACK until we find a better solution
				var tr = OCA.Files.FileList.prototype._createRow.apply(this, arguments)
				tr.find('td.filesize').remove()
				return tr
			},

			getAjaxUrl: function(action, params) {
				var q = ''
				if (params) {
					q = '?' + OC.buildQueryString(params)
				}
				return OC.filePath('files_trashbin', 'ajax', action + '.php') + q
			},

			setupUploadEvents: function() {
			// override and do nothing
			},

			linkTo: function(dir) {
				return OC.linkTo('files', 'index.php') + '?view=trashbin&dir=' + encodeURIComponent(dir).replace(/%2F/g, '/')
			},

			elementToFile: function($el) {
				var fileInfo = OCA.Files.FileList.prototype.elementToFile($el)
				if (this.getCurrentDirectory() === '/') {
					fileInfo.displayName = getDeletedFileName(fileInfo.name)
				}
				// no size available
				delete fileInfo.size
				return fileInfo
			},

			updateEmptyContent: function() {
				var exists = this.$fileList.find('tr:first').exists()
				this.$el.find('.emptyfilelist.emptycontent').toggleClass('hidden', exists)
				this.$el.find('.files-filestable th').toggleClass('hidden', !exists)
			},

			_removeCallback: function(files) {
				var $el
				for (var i = 0; i < files.length; i++) {
					$el = this.remove(OC.basename(files[i]), { updateSummary: false })
					this.fileSummary.remove({ type: $el.attr('data-type'), size: $el.attr('data-size') })
				}
				this.fileSummary.update()
				this.updateEmptyContent()
			},

			_onClickRestoreSelected: function(event) {
				event.preventDefault()
				var self = this
				var files = _.pluck(this.getSelectedFiles(), 'name')
				for (var i = 0; i < files.length; i++) {
					var tr = this.findFileEl(files[i])
					this.showFileBusyState(tr, true)
				}

				this.fileMultiSelectMenu.toggleLoading('restore', true)
				var restorePromises = files.map(function(file) {
					return self.deleteOperationQueue.add(async () => {
						self.client.move(OC.joinPaths('trash', self.getCurrentDirectory(), file), OC.joinPaths('restore', file), true)
						self._removeCallback([file])
					})
				})
				return Promise.all(restorePromises).then(
					function() {
						self.fileMultiSelectMenu.toggleLoading('restore', false)
					},
					function() {
						OC.Notification.show(t('files_trashbin', 'Error while restoring files from trash bin'))
					}
				)
			},

			_onClickDeleteSelected: function(event) {
				event.preventDefault()
				var self = this
				var allFiles = this.$el.find('.select-all').is(':checked')
				var files = _.pluck(this.getSelectedFiles(), 'name')
				for (var i = 0; i < files.length; i++) {
					var tr = this.findFileEl(files[i])
					this.showFileBusyState(tr, true)
				}

				if (allFiles) {
					return this.client.remove(OC.joinPaths('trash', this.getCurrentDirectory()))
						.then(
							function() {
								self.hideMask()
								self.setFiles([])
							},
							function() {
								OC.Notification.show(t('files_trashbin', 'Error while emptying trash bin'))
							}
						)
				} else {
					this.fileMultiSelectMenu.toggleLoading('delete', true)
					var deletePromises = files.map(function(file) {
						return self.deleteOperationQueue.add(async () => {
							await self.client.remove(OC.joinPaths('trash', self.getCurrentDirectory(), file))
							self._removeCallback([file])
						})
					})
					return Promise.all(deletePromises).then(
						function() {
							self.fileMultiSelectMenu.toggleLoading('delete', false)
						},
						function() {
							OC.Notification.show(t('files_trashbin', 'Error while removing files from trash bin'))
						}
					)
				}
			},

			_onClickFile: function(event) {
				var mime = $(this).parent().parent().data('mime')
				if (mime !== 'httpd/unix-directory') {
					event.preventDefault()
				}
				return OCA.Files.FileList.prototype._onClickFile.apply(this, arguments)
			},

			generatePreviewUrl: function(urlSpec) {
				return OC.generateUrl('/apps/files_trashbin/preview?') + $.param(urlSpec)
			},

			getDownloadUrl: function() {
			// no downloads
				return '#'
			},

			getDefaultActionUrl: function() {
			// no default action
				return '#'
			},

			updateStorageStatistics: function() {
			// no op because the trashbin doesn't have
			// storage info like free space / used space
			},

			isSelectedDeletable: function() {
				return true
			},

			/**
		 * Returns list of webdav properties to request
		 */
			_getWebdavProperties: function() {
				return [FILENAME_PROP, DELETION_TIME_PROP, TRASHBIN_ORIGINAL_LOCATION, TRASHBIN_TITLE].concat(this.filesClient.getPropfindProperties())
			},

			/**
		 * Reloads the file list using ajax call
		 *
		 * @returns ajax call object
		 */
			reload: function() {
				this._selectedFiles = {}
				this._selectionSummary.clear()
				this.$el.find('.select-all').prop('checked', false)
				this.showMask()
				if (this._reloadCall?.abort) {
					this._reloadCall.abort()
				}
				this._reloadCall = this.client.getFolderContents(
					'trash/' + this.getCurrentDirectory(), {
						includeParent: false,
						properties: this._getWebdavProperties()
					}
				)
				var callBack = this.reloadCallback.bind(this)
				return this._reloadCall.then(callBack, callBack)
			},
			reloadCallback: function(status, result) {
				delete this._reloadCall
				this.hideMask()

				if (status === 401) {
					return false
				}

				// Firewall Blocked request?
				if (status === 403) {
				// Go home
					this.changeDirectory('/')
					OC.Notification.show(t('files', 'This operation is forbidden'))
					return false
				}

				// Did share service die or something else fail?
				if (status === 500) {
				// Go home
					this.changeDirectory('/')
					OC.Notification.show(t('files', 'This directory is unavailable, please check the logs or contact the administrator'))
					return false
				}

				if (status === 404) {
				// go back home
					this.changeDirectory('/')
					return false
				}
				// aborted ?
				if (status === 0) {
					return true
				}

				this.setFiles(result)
				return true
			}

		})

	OCA.Trashbin.FileList = FileList
})()