summaryrefslogtreecommitdiffstats
path: root/apps/files_versions/src/views/VersionTab.vue
blob: 90664491941339f34ff100a63bb6ad32ed74fd35 (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
<!--
 - @copyright 2022 Carl Schwan <carl@carlschwan.eu>
 - @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/>.
 -->
<template>
	<div>
		<ul>
			<NcListItem v-for="version in versions"
				:key="version.dateTime.unix()"
				class="version"
				:title="version.title"
				:href="version.url">
				<template #icon>
					<img lazy="true"
						:src="version.preview"
						alt=""
						height="256"
						width="256"
						class="version-image">
				</template>
				<template #subtitle>
					<div class="version-info">
						<a v-tooltip="version.dateTime" :href="version.url">{{ version.relativeTime }}</a>
						<span class="version-info-size">•</span>
						<span class="version-info-size">
							{{ version.size }}
						</span>
					</div>
				</template>
				<template #actions>
					<NcActionLink :href="version.url">
						<template #icon>
							<Download :size="22" />
						</template>
						{{ t('files_versions', 'Download version') }}
					</NcActionLink>
					<NcActionButton v-if="!version.isCurrent" @click="restoreVersion(version)">
						<template #icon>
							<BackupRestore :size="22" />
						</template>
						{{ t('files_versions', 'Restore version') }}
					</NcActionButton>
				</template>
			</NcListItem>
			<NcEmptyContent v-if="!loading && versions.length === 1"
				:title="t('files_version', 'No versions yet')">
				<!-- length === 1, since we don't want to show versions if there is only the current file -->
				<template #icon>
					<BackupRestore />
				</template>
			</NcEmptyContent>
		</ul>
	</div>
</template>

<script>
import { createClient, getPatcher } from 'webdav'
import axios from '@nextcloud/axios'
import { generateRemoteUrl, generateUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import BackupRestore from 'vue-material-design-icons/BackupRestore.vue'
import Download from 'vue-material-design-icons/Download.vue'
import NcActionButton from '@nextcloud/vue/dist/Components/NcActionButton.js'
import NcActionLink from '@nextcloud/vue/dist/Components/NcActionLink.js'
import NcListItem from '@nextcloud/vue/dist/Components/NcListItem.js'
import NcEmptyContent from '@nextcloud/vue/dist/Components/NcEmptyContent.js'
import { showError, showSuccess } from '@nextcloud/dialogs'
import moment from '@nextcloud/moment'
import { basename, joinPaths } from '@nextcloud/paths'
import { getLoggerBuilder } from '@nextcloud/logger'
import { translate } from '@nextcloud/l10n'

const logger = getLoggerBuilder()
	.setApp('files_version')
	.detectUser()
	.build()

/**
 * Get WebDAV request body for version list
 */
function getDavRequest() {
	return `<?xml version="1.0"?>
			<d:propfind xmlns:d="DAV:"
				xmlns:oc="http://owncloud.org/ns"
				xmlns:nc="http://nextcloud.org/ns"
				xmlns:ocs="http://open-collaboration-services.org/ns">
				<d:prop>
					<d:getcontentlength />
					<d:getcontenttype />
					<d:getlastmodified />
				</d:prop>
			</d:propfind>`
}

/**
 * Format version
 *
 * @param version
 * @param fileInfo
 */
function formatVersion(version, fileInfo) {
	const fileVersion = basename(version.filename)
	const isCurrent = version.mime === ''

	const preview = isCurrent
		? generateUrl('/core/preview?fileId={fileId}&c={fileEtag}&x=250&y=250&forceIcon=0&a=0', {
			fileId: fileInfo.id,
			fileEtag: fileInfo.etag,
		})
		: generateUrl('/apps/files_versions/preview?file={file}&version={fileVersion}', {
			file: joinPaths(fileInfo.path, fileInfo.name),
			fileVersion,
		})

	return {
		displayVersionName: fileVersion,
		title: isCurrent ? translate('files_versions', 'Current version') : '',
		fileName: version.filename,
		mimeType: version.mime,
		size: OC.Util.humanFileSize(isCurrent ? fileInfo.size : version.size),
		type: version.type,
		dateTime: moment(isCurrent ? fileInfo.mtime : version.lastmod),
		relativeTime: moment(isCurrent ? fileInfo.mtime : version.lastmod).fromNow(),
		preview,
		url: isCurrent ? joinPaths('/remote.php/dav', version.filename) : joinPaths('/remote.php/dav', fileInfo.path, fileInfo.name),
		fileVersion,
		isCurrent,
	}
}

const rootPath = 'dav'

// force our axios
const patcher = getPatcher()
patcher.patch('request', axios)

// init webdav client on default dav endpoint
const remote = generateRemoteUrl(rootPath)
const client = createClient(remote)

export default {
	name: 'VersionTab',
	components: {
		NcEmptyContent,
		NcActionLink,
		NcActionButton,
		NcListItem,
		BackupRestore,
		Download,
	},
	data() {

		return {
			fileInfo: null,
			versions: [],
			loading: true,
		}
	},
	methods: {
		/**
		 * Update current fileInfo and fetch new data
		 *
		 * @param {object} fileInfo the current file FileInfo
		 */
		async update(fileInfo) {
			this.fileInfo = fileInfo
			this.resetState()
			this.fetchVersions()
		},

		/**
		 * Get the existing versions infos
		 */
		async fetchVersions() {
			const path = `/versions/${getCurrentUser().uid}/versions/${this.fileInfo.id}`

			try {
				const response = await client.getDirectoryContents(path, {
					data: getDavRequest(),
				})
				this.versions = response.map(version => formatVersion(version, this.fileInfo))
				this.loading = false
			} catch (exception) {
				logger.error('Could not fetch version', { exception })
				this.loading = false
			}
		},

		/**
		 * Restore the given version
		 *
		 * @param version
		 */
		async restoreVersion(version) {
			try {
				logger.debug('restoring version', version.url)
				await client.moveFile(
					`/versions/${getCurrentUser().uid}/versions/${this.fileInfo.id}/${version.fileVersion}`,
					`/versions/${getCurrentUser().uid}/restore/target`
				)
				showSuccess(t('files_versions', 'Version restored'))
				await this.fetchVersions()
			} catch (exception) {
				logger.error('Could not restore version', { exception })
				showError(t('files_versions', 'Could not restore version'))
			}
		},

		/**
		 * Reset the current view to its default state
		 */
		resetState() {
			this.versions = []
		},
	},
}
</script>

<style scopped lang="scss">
.version {
	display: flex;
	flex-direction: row;
	&-info {
		display: flex;
		flex-direction: row;
		align-items: center;
		gap: 0.5rem;
		&-size {
			color: var(--color-text-lighter);
		}
	}
	&-image {
		width: 3rem;
		height: 3rem;
		border: 1px solid var(--color-border);
		margin-right: 1rem;
		border-radius: var(--border-radius-large);
	}
}
</style>