Skip to content

Commit d662ac6

Browse files
authored
feat(sltt-app): auto-login to samba drive (#45)
What issue(s) is this trying to resolve? * feat(sltt-app): detect when samba drive needs user to login to the router again #44 How does it all work? * in handleProbeConnections, determine if the url has an ipaddress and if so, issue an OS command (e.g. net use on windows) to authorize the connection once. Steps for testing Scenario 1 - newer windows computer 1. connect newer windows (11) computer to network storage samba drive (e.g. via wifi) 2. reboot computer 3. launch latest sltt-app (with these changes). Expect it to detect the local team storage and show an icon that allows an admin to connect and/or turn it on for the project. Scenario 2 - older windows computer 1. repeat on older Windows 10 Scenario 3: repeat for newer macos computer Scenario 4: repeat for older macos computer ticket: #44 commit-convention: https://www.conventionalcommits.org/en/v1.0.0/
1 parent 4f3ff35 commit d662ac6

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

storage/connections.ts

+49-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
11
import { access, appendFile, readFile } from 'fs/promises'
22
import { constants, ensureDir } from 'fs-extra'
3+
import { exec } from 'child_process'
4+
import { promisify } from 'util'
35
import { pathToFileURL, fileURLToPath } from 'url'
46
import { AddStorageProjectArgs, ConnectToUrlArgs, ConnectToUrlResponse, GetStorageProjectsArgs, GetStorageProjectsResponse, ProbeConnectionsArgs, ProbeConnectionsResponse, RemoveStorageProjectArgs } from './connections.d'
57

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+
635
export const handleGetStorageProjects = async (defaultStoragePath: string, { clientId }: GetStorageProjectsArgs): Promise<GetStorageProjectsResponse> => {
736
await ensureDir(defaultStoragePath)
837
console.log(`handleGetStorageProjects by client '${clientId}'`)
@@ -49,21 +78,31 @@ export const handleRemoveStorageProject = async (defaultStoragePath: string, { u
4978
}
5079
}
5180

81+
let lastSambaIP = ''
82+
5283
export const handleProbeConnections = async (defaultStoragePath: string, { urls }: ProbeConnectionsArgs): Promise<ProbeConnectionsResponse> => {
5384

5485
await ensureDir(defaultStoragePath)
5586
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) }
64105
}
65-
return { url, accessible: await canAccess(filePath) }
66-
}
67106
)
68107
)
69108
return connections

0 commit comments

Comments
 (0)