consolidate mount/unmount

This commit is contained in:
FTCHD
2025-02-02 11:33:41 +02:00
parent 474fd13062
commit 2ee05c4fe2
4 changed files with 111 additions and 27 deletions
+29 -12
View File
@@ -1,15 +1,15 @@
import { Menu, MenuItem, PredefinedMenuItem, Submenu } from '@tauri-apps/api/menu' import { Menu, MenuItem, PredefinedMenuItem, Submenu } from '@tauri-apps/api/menu'
import { getCurrentWindow } from '@tauri-apps/api/window' import { getCurrentWindow } from '@tauri-apps/api/window'
import { ask, message, open } from '@tauri-apps/plugin-dialog' import { ask, message, open } from '@tauri-apps/plugin-dialog'
import { exists, remove } from '@tauri-apps/plugin-fs' import { exists, mkdir, remove } from '@tauri-apps/plugin-fs'
import { isPermissionGranted, requestPermission } from '@tauri-apps/plugin-notification' import { isPermissionGranted, requestPermission } from '@tauri-apps/plugin-notification'
import { sendNotification } from '@tauri-apps/plugin-notification' import { sendNotification } from '@tauri-apps/plugin-notification'
import { openPath } from '@tauri-apps/plugin-opener' import { openPath } from '@tauri-apps/plugin-opener'
import { platform } from '@tauri-apps/plugin-os' import { platform } from '@tauri-apps/plugin-os'
import { exit } from '@tauri-apps/plugin-process' import { exit } from '@tauri-apps/plugin-process'
import { isDirectoryEmpty } from './fs' import { isDirectoryEmpty } from './fs'
import { deleteRemote, mountRemote } from './rclone/api' import { deleteRemote, mountRemote, unmountRemote } from './rclone/api'
import { dialogGetMountPlugin, needsMountPlugin, unmountRemote } from './rclone/mount' import { dialogGetMountPlugin, needsMountPlugin } from './rclone/mount'
import { usePersistedStore, useStore } from './store' import { usePersistedStore, useStore } from './store'
import { getLoadingTray, getMainTray, rebuildTrayMenu } from './tray' import { getLoadingTray, getMainTray, rebuildTrayMenu } from './tray'
import { lockWindows, openFullWindow, openTrayWindow, openWindow, unlockWindows } from './window' import { lockWindows, openFullWindow, openTrayWindow, openWindow, unlockWindows } from './window'
@@ -54,7 +54,7 @@ export async function buildMenu() {
console.error(`No mount point found for remote ${remote}`) console.error(`No mount point found for remote ${remote}`)
return return
} }
await unmountRemote(mountPoint) await unmountRemote({ mountPoint })
delete storeState.mountedRemotes[remote] delete storeState.mountedRemotes[remote]
await rebuildTrayMenu() await rebuildTrayMenu()
await message(`Successfully unmounted ${remote} from ${mountPoint}`, { await message(`Successfully unmounted ${remote} from ${mountPoint}`, {
@@ -63,6 +63,7 @@ export async function buildMenu() {
} catch (err) { } catch (err) {
console.error('Unmount operation failed:', err) console.error('Unmount operation failed:', err)
await message(`Failed to unmount ${remote}: ${err}`, { await message(`Failed to unmount ${remote}: ${err}`, {
kind: 'error',
title: 'Unmount Error', title: 'Unmount Error',
}) })
} }
@@ -134,10 +135,18 @@ export async function buildMenu() {
console.log('selectedPath', selectedPath) console.log('selectedPath', selectedPath)
const directoryExists = await exists(selectedPath) let directoryExists: boolean | undefined
try {
directoryExists = await exists(selectedPath)
} catch (err) {
console.error('Error checking if directory exists:', err)
}
console.log('directoryExists', directoryExists)
const isPlatformWindows = platform() === 'windows' const isPlatformWindows = platform() === 'windows'
if (directoryExists || isPlatformWindows) {
if (directoryExists) {
const isEmpty = await isDirectoryEmpty(selectedPath) const isEmpty = await isDirectoryEmpty(selectedPath)
if (!isEmpty) { if (!isEmpty) {
// await resetMainWindow() // await resetMainWindow()
@@ -156,12 +165,20 @@ export async function buildMenu() {
if (isPlatformWindows) { if (isPlatformWindows) {
await remove(selectedPath) await remove(selectedPath)
} }
} else { } else if (!isPlatformWindows) {
await message('The selected directory does not exist.', { try {
title: 'Mount Error', await mkdir(selectedPath)
kind: 'error', } catch (error) {
}) console.error('Error creating directory:', error)
return await message(
'Failed to create mount directory. Try creating it manually first.',
{
title: 'Mount Error',
kind: 'error',
}
)
return
}
} }
// Mount the remote // Mount the remote
+48
View File
@@ -8,6 +8,7 @@ const SUPPORRTED_BACKENDS = ['sftp', 's3', 'b2', 'drive']
function getAuthHeader() { function getAuthHeader() {
return return
// biome-ignore lint/correctness/noUnreachable: <explanation>
if (platform() === 'macos') { if (platform() === 'macos') {
return return
} }
@@ -319,6 +320,53 @@ export async function mountRemote({
return return
} }
export async function unmountRemote({
mountPoint,
}: {
mountPoint: string
}) {
const options = new URLSearchParams()
options.set('mountPoint', mountPoint)
const r = await fetch(`http://localhost:5572/mount/unmount?${options.toString()}`, {
method: 'POST',
headers: getAuthHeader(),
})
.then((res) => res.json())
.catch((e) => {
console.log('error', e)
throw e
})
// console.log('unmountRemote', JSON.stringify(r, null, 2))
if ('error' in r) {
throw new Error(r.error)
}
return
}
export async function unmountAllRemotes() {
const r = await fetch('http://localhost:5572/mount/unmountall', {
method: 'POST',
headers: getAuthHeader(),
})
.then((res) => res.json())
.catch((e) => {
console.log('error', e)
throw e
})
console.log('unmountAllRemotes', r)
if ('error' in r) {
throw new Error(r.error)
}
return
}
export async function startCopy({ export async function startCopy({
source, source,
dest, dest,
+2 -2
View File
@@ -74,7 +74,7 @@ export async function dialogGetMountPlugin() {
} }
} }
export async function unmountRemote(mountPoint: string, force = false) { export async function unmount(mountPoint: string, force = false) {
const command = Command.create('umount', [force ? '-f' : '', mountPoint]) const command = Command.create('umount', [force ? '-f' : '', mountPoint])
const output = await command.execute() const output = await command.execute()
@@ -91,7 +91,7 @@ export async function unmountRemote(mountPoint: string, force = false) {
cancelLabel: 'Cancel', cancelLabel: 'Cancel',
}) })
if (answer) { if (answer) {
return await unmountRemote(mountPoint, true) return await unmount(mountPoint, true)
} }
throw new Error(output.stderr) throw new Error(output.stderr)
} }
+32 -13
View File
@@ -1,7 +1,7 @@
import { Accordion, AccordionItem, Avatar, Button } from '@nextui-org/react' import { Accordion, AccordionItem, Avatar, Button } from '@nextui-org/react'
import { getCurrentWindow } from '@tauri-apps/api/window' import { getCurrentWindow } from '@tauri-apps/api/window'
import { message } from '@tauri-apps/plugin-dialog' import { message } from '@tauri-apps/plugin-dialog'
import { exists, remove } from '@tauri-apps/plugin-fs' import { exists, mkdir, remove } from '@tauri-apps/plugin-fs'
import { revealItemInDir } from '@tauri-apps/plugin-opener' import { revealItemInDir } from '@tauri-apps/plugin-opener'
import { platform } from '@tauri-apps/plugin-os' import { platform } from '@tauri-apps/plugin-os'
import { import {
@@ -94,6 +94,8 @@ export default function Mount() {
}, [mountOptionsJson, vfsOptionsJson]) }, [mountOptionsJson, vfsOptionsJson])
const handleStartMount = useCallback(async () => { const handleStartMount = useCallback(async () => {
if (!dest || !source) return
setIsLoading(true) setIsLoading(true)
try { try {
@@ -111,11 +113,19 @@ export default function Mount() {
_mountOptions.VolumeName = source!.split('/').pop()! _mountOptions.VolumeName = source!.split('/').pop()!
} }
const directoryExists = await exists(dest!) let directoryExists: boolean | undefined
try {
directoryExists = await exists(dest)
} catch (err) {
console.error('Error checking if directory exists:', err)
}
console.log('directoryExists', directoryExists)
const isPlatformWindows = platform() === 'windows' const isPlatformWindows = platform() === 'windows'
if (directoryExists || isPlatformWindows) {
const isEmpty = await isDirectoryEmpty(dest!) if (directoryExists) {
const isEmpty = await isDirectoryEmpty(dest)
if (!isEmpty) { if (!isEmpty) {
// await resetMainWindow() // await resetMainWindow()
@@ -128,18 +138,27 @@ export default function Mount() {
} }
if (isPlatformWindows) { if (isPlatformWindows) {
await remove(dest!) await remove(dest)
}
} else if (!isPlatformWindows) {
try {
await mkdir(dest)
} catch (error) {
console.error('Error creating directory:', error)
await message(
'Failed to create mount directory. Try creating it manually first.',
{
title: 'Mount Error',
kind: 'error',
}
)
return
} }
} else {
await message('The selected directory does not exist.', {
title: 'Mount Error',
kind: 'error',
})
return
} }
await mountRemote({ await mountRemote({
remotePath: source!, remotePath: source,
mountPoint: dest!, mountPoint: dest,
mountOptions: _mountOptions, mountOptions: _mountOptions,
vfsOptions, vfsOptions,
}) })