-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a method to get file contents #50
Comments
I tried to create a method to make it work, but I noticed that surround single quotes arround remote and local paths make the command in the OP not working (tested on What is the reason for quoting the paths? Spaces and other special characters? Update It works only without quotes around paths or using double quotes. It does not work with single quotes. // These commands work
smbclient -U server -c "get ./file.txt /dev/fd/1" "//1.2.3.4/file" --password p4ss
smbclient -U server -c "get \"./file.txt\" \"/dev/fd/1\"" "//1.2.3.4/file" --password p4ss
smbclient -U server -c 'get "./file.txt" "/dev/fd/1"' "//1.2.3.4/file" --password p4ss
// This command does not work
smbclient -U server -c "get './file.txt' '/dev/fd/1'" "//1.2.3.4/file" --password p4ss Update 2 I’ve just found out that in order make it work, we need to add However, now I have no idea why can’t I save the file contents to variable (it is always output to |
As I could not find a way to define a variable with the contents of the remote file from It is not ideal (one could argue it could be done outside of /**
* Read file contents of a remote file
*
* @param {string} file File name with path relative to remote share root folder
* @param {string} encoding Character encoding
* @param {string} [tempPath='/dev/shm'] Temporary folder path
* @param {string} workingDir Working directory
* @return {Promise} Remote file contents
*/
const {dirname, basename} = require('path')
const {access, constants, readFile, unlink} = require('fs-extra')
async readFileContents({file, encoding, tempPath = '/dev/shm', workingDir}) {
const tempFileBase = `${tempPath}/${basename(file)}`
let tempFile = tempFileBase
let i = 0
let created = false
try {
await access(tempPath, constants.R_OK | constants.W_OK)
} catch (e) {
throw e
}
while (!created) {
try {
await access(tempFile, constants.F_OK)
i++
tempFile = `${tempFileBase}_${i}`
} catch {
try {
await this.getFile(file, tempFile, workingDir)
created = true
} catch {
i++
tempFile = `${tempFileBase}_${i}`
}
}
}
const content = (await readFile(tempFile, encoding)).toString()
await unlink(tempFile)
return content
} Note that I could not make running // const getCleanedSmbClientArgs = (args) => args.map((arg) => `'${arg.replace(/\//g, '\\')}'`).join(' ')
const getCleanedSmbClientArgs = (args) => args.map((arg) => `"${arg}"`).join(' ') |
I’d like to get contents of a file. I have stumbled upon this SO answer, which works perfectly:
Could we add a method like
readFile()
? Subsequently, could we also and an method towriteFile()
(different fromsendFile()
that the contents would be in memory only, not a file)?The text was updated successfully, but these errors were encountered: