-
Notifications
You must be signed in to change notification settings - Fork 14
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
#3869 - Federal Restrictions - Unzip ZIP files coming from SFTP #4163
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import { | |
getFileNameAsExtendedCurrentTimestamp, | ||
convertToASCII, | ||
FILE_DEFAULT_ENCODING, | ||
readFirstExtractedFile, | ||
} from "@sims/utilities"; | ||
import { | ||
LINE_BREAK_SPLIT_REGEX, | ||
|
@@ -154,10 +155,29 @@ export abstract class SFTPIntegrationBase<DownloadType> { | |
return false; | ||
} | ||
} | ||
// Read all the file content and create a buffer with 'ascii' encoding. | ||
const fileContent = await client.get(remoteFilePath, undefined, { | ||
readStreamOptions: { encoding: FILE_DEFAULT_ENCODING }, | ||
}); | ||
let fileContent: string | NodeJS.WritableStream | Buffer; | ||
const fileExtension = path.parse(remoteFilePath).ext.toLocaleLowerCase(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any particular reason to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was a mistake 😂 intention was There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As an alternative, the below was used in the past. path.extname(file.originalname).toLowerCase() |
||
const isZIPFile = fileExtension === ".zip"; | ||
if (isZIPFile) { | ||
// Read the zip file content with null encoding to avoid data corruption. | ||
const compressedFileContent = (await client.get( | ||
remoteFilePath, | ||
undefined, | ||
{ readStreamOptions: { encoding: null } }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
)) as Buffer; | ||
// Read the first file content with 'ascii' encoding. | ||
const { fileName, data } = readFirstExtractedFile( | ||
compressedFileContent, | ||
{ encoding: FILE_DEFAULT_ENCODING }, | ||
); | ||
this.logger.log(`Extracted the first file ${fileName}.`); | ||
fileContent = data; | ||
} else { | ||
// Read all the file content and create a buffer with 'ascii' encoding. | ||
fileContent = await client.get(remoteFilePath, undefined, { | ||
readStreamOptions: { encoding: FILE_DEFAULT_ENCODING }, | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the compressed get at the line 167 the below can be cast. // Read all the file content and create a buffer with 'ascii' encoding.
fileContent = (await client.get(remoteFilePath, undefined, {
readStreamOptions: { encoding: FILE_DEFAULT_ENCODING },
})) as string; This would allow the fileContent to be declared as string (which really is) instead of |
||
} | ||
// Convert the file content to an array of text lines and remove possible blank lines. | ||
return fileContent | ||
.toString() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as AdmZip from "adm-zip"; | ||
|
||
/** | ||
* Reads the first extracted file from a compressed archive file. | ||
* @param compressedFileBuffer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment. |
||
* @param options options. | ||
* - `encoding`: encoding to read the file. | ||
* @returns first extracted file name and data. | ||
*/ | ||
export function readFirstExtractedFile( | ||
compressedFileBuffer: Buffer, | ||
options?: { encoding: string }, | ||
): { | ||
fileName: string; | ||
data: string; | ||
} { | ||
const zipFile = new AdmZip(compressedFileBuffer); | ||
const [firstExtractedFile] = zipFile.getEntries(); | ||
if (!firstExtractedFile) { | ||
throw new Error("No files found in zip file"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add the period. |
||
} | ||
// Read the first extracted file with the specified encoding. | ||
const data = zipFile.readAsText(firstExtractedFile, options?.encoding); | ||
andrewsignori-aot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return { | ||
fileName: firstExtractedFile.name, | ||
data, | ||
}; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not enforcing the zip extension would allow the process to download the file either way, compressed or not.
I do not see an AC requesting either one or not and I do not see a reason to have it restricted.
Not a blocker.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not enforcing the
.zip
will also lead to not enforcing the end of file name by removing the existing$
. I see the benefit of it working either way, but should e remove the$
. ?Let me know if I am missing something here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we talked, IMO, we do need to enforce the precise ending of the string but I am ok either way.