|
1 | 1 | import { access, appendFile, readFile } from 'fs/promises'
|
2 | 2 | import { constants, ensureDir } from 'fs-extra'
|
| 3 | +import { exec } from 'child_process' |
| 4 | +import { promisify } from 'util' |
3 | 5 | import { pathToFileURL, fileURLToPath } from 'url'
|
4 | 6 | import { AddStorageProjectArgs, ConnectToUrlArgs, ConnectToUrlResponse, GetStorageProjectsArgs, GetStorageProjectsResponse, ProbeConnectionsArgs, ProbeConnectionsResponse, RemoveStorageProjectArgs } from './connections.d'
|
5 | 7 |
|
| 8 | +const execPromise = promisify(exec) |
| 9 | + |
| 10 | +async function connectToSambaWithCommand(command: string): Promise<boolean> { |
| 11 | + try { |
| 12 | + const { stdout, stderr } = await execPromise(command) |
| 13 | + console.log(`Successfully connected to Samba drive (${command})`) |
| 14 | + console.log(stdout) |
| 15 | + return true |
| 16 | + } catch (error) { |
| 17 | + console.error(`Error connecting to Samba drive (${command}): ${error.message}`) |
| 18 | + return false |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +async function connectToSamba(sambaIP: string): Promise<boolean> { |
| 23 | + if (process.platform === 'win32') { |
| 24 | + const command = `net use \\\\${sambaIP}\\sltt-app /user:guest ""` |
| 25 | + return await connectToSambaWithCommand(command) |
| 26 | + } else if (process.platform === 'darwin') { |
| 27 | + const command = `mount_smbfs //guest:@${sambaIP}/sltt-app /Volumes/sltt-app` |
| 28 | + return await connectToSambaWithCommand(command) |
| 29 | + } else { |
| 30 | + console.error('Unsupported platform') |
| 31 | + return false |
| 32 | + } |
| 33 | +} |
| 34 | + |
6 | 35 | export const handleGetStorageProjects = async (defaultStoragePath: string, { clientId }: GetStorageProjectsArgs): Promise<GetStorageProjectsResponse> => {
|
7 | 36 | await ensureDir(defaultStoragePath)
|
8 | 37 | console.log(`handleGetStorageProjects by client '${clientId}'`)
|
@@ -49,21 +78,31 @@ export const handleRemoveStorageProject = async (defaultStoragePath: string, { u
|
49 | 78 | }
|
50 | 79 | }
|
51 | 80 |
|
| 81 | +let lastSambaIP = '' |
| 82 | + |
52 | 83 | export const handleProbeConnections = async (defaultStoragePath: string, { urls }: ProbeConnectionsArgs): Promise<ProbeConnectionsResponse> => {
|
53 | 84 |
|
54 | 85 | await ensureDir(defaultStoragePath)
|
55 | 86 | const connections = await Promise.all(
|
56 |
| - [pathToFileURL(defaultStoragePath).href, ...(urls || [])].map( |
57 |
| - async (url) => { |
58 |
| - let filePath = '' |
59 |
| - try { |
60 |
| - filePath = fileURLToPath(url) |
61 |
| - } catch (e) { |
62 |
| - console.error(`fileURLToPath(${url}) error`, e) |
63 |
| - return { url, accessible: false, error: e.message } |
| 87 | + [pathToFileURL(defaultStoragePath).href, ...(urls || [])] |
| 88 | + .map( |
| 89 | + async (url) => { |
| 90 | + let filePath = '' |
| 91 | + try { |
| 92 | + const urlObj = new URL(url) |
| 93 | + const ipAddress = urlObj.hostname |
| 94 | + if (urlObj.protocol === 'file:' |
| 95 | + && ipAddress && ipAddress !== lastSambaIP) { |
| 96 | + lastSambaIP = ipAddress |
| 97 | + await connectToSamba(ipAddress) |
| 98 | + } |
| 99 | + filePath = fileURLToPath(url) |
| 100 | + } catch (e) { |
| 101 | + console.error(`fileURLToPath(${url}) error`, e) |
| 102 | + return { url, accessible: false, error: e.message } |
| 103 | + } |
| 104 | + return { url, accessible: await canAccess(filePath) } |
64 | 105 | }
|
65 |
| - return { url, accessible: await canAccess(filePath) } |
66 |
| - } |
67 | 106 | )
|
68 | 107 | )
|
69 | 108 | return connections
|
|
0 commit comments