Skip to content

Commit

Permalink
feat(chat): add download link to attachments
Browse files Browse the repository at this point in the history
Signed-off-by: Grigorii K. Shartsev <me@shgk.me>
  • Loading branch information
ShGKme authored and backportbot[bot] committed Nov 6, 2024
1 parent 21b2f94 commit f1b9c4d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,21 @@
</template>
{{ t('spreed', 'Mark as unread') }}
</NcActionButton>
<NcActionLink v-if="linkToFile"
:href="linkToFile">
<template #icon>
<File :size="20" />
</template>
{{ t('spreed', 'Go to file') }}
</NcActionLink>
<template v-if="isFileShare">
<NcActionSeparator />
<NcActionLink :href="messageFile.link">
<template #icon>
<File :size="20" />
</template>
{{ t('spreed', 'Go to file') }}
</NcActionLink>
<NcActionLink :href="linkToFileDownload" :download="messageFile.name">
<template #icon>
<IconDownload :size="20" />
</template>
{{ t('spreed', 'Download file') }}
</NcActionLink>
</template>
<NcActionButton v-if="canForwardMessage && !isInNoteToSelf"
close-after-click
@click="forwardToNote">
Expand Down Expand Up @@ -260,6 +268,7 @@ import ClockOutline from 'vue-material-design-icons/ClockOutline.vue'
import CloseCircleOutline from 'vue-material-design-icons/CloseCircleOutline.vue'
import ContentCopy from 'vue-material-design-icons/ContentCopy.vue'
import DeleteIcon from 'vue-material-design-icons/Delete.vue'
import IconDownload from 'vue-material-design-icons/Download.vue'
import EmoticonOutline from 'vue-material-design-icons/EmoticonOutline.vue'
import EyeOffOutline from 'vue-material-design-icons/EyeOffOutline.vue'
import File from 'vue-material-design-icons/File.vue'
Expand All @@ -271,6 +280,7 @@ import Reply from 'vue-material-design-icons/Reply.vue'
import Share from 'vue-material-design-icons/Share.vue'
import Translate from 'vue-material-design-icons/Translate.vue'

import { getCurrentUser } from '@nextcloud/auth'
import { showError, showSuccess } from '@nextcloud/dialogs'
import { t } from '@nextcloud/l10n'
import moment from '@nextcloud/moment'
Expand All @@ -291,6 +301,7 @@ import { hasTalkFeature } from '../../../../../services/CapabilitiesManager.ts'
import { getMessageReminder, removeMessageReminder, setMessageReminder } from '../../../../../services/remindersService.js'
import { useIntegrationsStore } from '../../../../../stores/integrations.js'
import { useReactionsStore } from '../../../../../stores/reactions.js'
import { generatePublicShareDownloadUrl, generateUserFileUrl } from '../../../../../utils/davUtils.ts'
import { copyConversationLinkToClipboard } from '../../../../../utils/handleUrl.ts'
import { parseMentions } from '../../../../../utils/textParse.ts'

Expand All @@ -317,6 +328,7 @@ export default {
ClockEditOutline,
ClockOutline,
ContentCopy,
IconDownload,
DeleteIcon,
EmoticonOutline,
EyeOffOutline,
Expand Down Expand Up @@ -438,13 +450,15 @@ export default {
&& this.$store.getters.isActorUser()
},

linkToFile() {
if (this.isFileShare) {
const firstFileKey = (Object.keys(this.message.messageParameters).find(key => key.startsWith('file')))
return this.message.messageParameters?.[firstFileKey]?.link
} else {
return ''
}
messageFile() {
const firstFileKey = (Object.keys(this.message.messageParameters).find(key => key.startsWith('file')))
return this.message.messageParameters[firstFileKey]
},

linkToFileDownload() {
return getCurrentUser()
? generateUserFileUrl(this.messageFile.path)
: generatePublicShareDownloadUrl(this.messageFile.link)
},

isCurrentGuest() {
Expand Down
29 changes: 29 additions & 0 deletions src/utils/davUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { getCurrentUser } from '@nextcloud/auth'
import { davRemoteURL } from '@nextcloud/files'

/**
* Generate a WebDAV url to a user files
* @param filepath - The path to the user's file, e.g., Talk/20240101-000102.png
* @param userid - The user id, e.g., 'admin'. Defaults to the current user id
* @return {string} The WebDAV URL to the file, e.g., https://nextcloud.ltd/remote.php/dav/files/admin/Talk/20240101-000102.png
*/
export function generateUserFileUrl(filepath: string, userid: string | undefined = getCurrentUser()?.uid) {
if (!userid) {
throw new TypeError('Cannot generate /files/<user>/ URL without a user')
}
return davRemoteURL + '/files/' + encodeURI(userid) + '/' + encodeURI(filepath)
}

/**
* Generate a download link for a public share
* @param shareLink - The public share link, e.g., https://nextcloud.ltd/s/CBeNTiJz5JeT2CH/
* @return {string} The download link, e.g., https://nextcloud.ltd/s/CBeNTiJz5JeT2CH/download
*/
export function generatePublicShareDownloadUrl(shareLink: string) {
return shareLink + '/download'
}

0 comments on commit f1b9c4d

Please sign in to comment.