-
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
Merged
dheepak-aot
merged 4 commits into
main
from
enhancement/#3869-unzip-sftp-file-federal-restrictions
Dec 21, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
const fileExtension = path.extname(remoteFilePath).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 }, | ||
})) as string; | ||
} | ||
// Convert the file content to an array of text lines and remove possible blank lines. | ||
return fileContent | ||
.toString() | ||
|
28 changes: 28 additions & 0 deletions
28
sources/packages/backend/libs/utilities/src/compressed-file-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 compressed file buffer. | ||
* @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."); | ||
} | ||
// 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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.