Skip to content

Commit

Permalink
feat(files): add conversion action
Browse files Browse the repository at this point in the history
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Jan 10, 2025
1 parent b2c7491 commit 045a990
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
70 changes: 70 additions & 0 deletions apps/files/src/actions/convertAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { Node, View } from '@nextcloud/files'

import { FileAction, registerFileAction } from '@nextcloud/files'
import { generateUrl } from '@nextcloud/router'
import { getCapabilities } from '@nextcloud/capabilities'
import { t } from '@nextcloud/l10n'

import AutoRenewSvg from '@mdi/svg/svg/autorenew.svg?raw'

import logger from '../logger'

type ConversionsProvider = {
from: string,
to: string[],
}

export const ACTION_CONVERT = 'convert'

export const registerConvertActions = () => {
// Generate sub actions
const convertProviders = getCapabilities()?.core?.conversions as ConversionsProvider[]
const actions = convertProviders.map(provider => {
return provider.to.map(to => new FileAction({
id: `convert-${provider.from}-${to}`,
displayName: () => t('files', 'Save as {to}', { to }),
iconSvgInline: () => generateIconSvg(to),
enabled: (nodes: Node[]) => {
// Check if some of the nodes are not of the right type
return !nodes.some(node => provider.from !== node.mime)
},

async exec(node: Node) {
logger.debug(`Convert to ${provider.from}`, { node })
return null
},

parent: ACTION_CONVERT,
}))
}).flat()

// Register main action
registerFileAction(new FileAction({
id: ACTION_CONVERT,
displayName: () => t('files', 'Save as'),
iconSvgInline: () => AutoRenewSvg,
enabled: (nodes: Node[], view: View) => {
return actions.some(action => action.enabled!(nodes, view))
},
async exec() {
return null
},
order: 25,
}))

// Register sub actions
actions.forEach(registerFileAction)
}

export const generateIconSvg = (mime: string) => {
// Generate icon based on mime type
const url = generateUrl('/core/mimeicon?mime=' + encodeURIComponent(mime))
return `<svg width="32" height="32" viewBox="0 0 32 32"
xmlns="http://www.w3.org/2000/svg">
<image href="${url}" height="32" width="32" />
</svg>`
}
2 changes: 2 additions & 0 deletions apps/files/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ import registerPreviewServiceWorker from './services/ServiceWorker.js'

import { initLivePhotos } from './services/LivePhotos'
import { isPublicShare } from '@nextcloud/sharing/public'
import { registerConvertActions } from './actions/convertAction.ts'

// Register file actions
registerConvertActions()
registerFileAction(deleteAction)
registerFileAction(downloadAction)
registerFileAction(editLocallyAction)
Expand Down

0 comments on commit 045a990

Please sign in to comment.