Skip to content

Commit

Permalink
fix:only create tmp directory when compression is on
Browse files Browse the repository at this point in the history
  • Loading branch information
jfraleigh committed Feb 16, 2024
1 parent 09fc1b5 commit 2b1cfb6
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions lib/file_transfer_agent/file_transfer_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,13 @@ function FileTransferAgent(context) {
*/
async function uploadOneFile(meta) {
meta['realSrcFilePath'] = meta['srcFilePath'];
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tmp'));
meta['tmpDir'] = tmpDir;

try {
if (meta['requireCompress']) {
const result = await SnowflakeFileUtil.compressFileWithGZIP(meta['srcFilePath'], meta['tmpDir']);
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tmp'));
const result = await SnowflakeFileUtil.compressFileWithGZIP(meta['srcFilePath'], tmpDir);

meta['tmpDir'] = tmpDir;
meta['realSrcFilePath'] = result.name;
}
const result = await SnowflakeFileUtil.getDigestAndSizeForFile(meta['realSrcFilePath']);
Expand All @@ -378,25 +380,27 @@ function FileTransferAgent(context) {
meta['errorDetails'] = err.toString();
meta['errorDetails'] += ` file=${meta['srcFileName']}, real file=${meta['realSrcFilePath']}`;
} finally {
// Remove all files inside tmp folder
const matchingFileNames = glob.sync(path.join(meta['tmpDir'], meta['srcFileName'] + '*'));
for (const matchingFileName of matchingFileNames) {
await new Promise((resolve, reject) => {
fs.unlink(matchingFileName, err => {
if (err) {
reject(err);
}
resolve();
// Remove all files inside tmp folder if compression is set
if (meta['requireCompress']) {
const matchingFileNames = glob.sync(path.join(meta['tmpDir'], meta['srcFileName'] + '*'));
for (const matchingFileName of matchingFileNames) {
await new Promise((resolve, reject) => {
fs.unlink(matchingFileName, err => {
if (err) {
reject(err);
}
resolve();
});
});
});
}
// Delete tmp folder
fs.rmdir(meta['tmpDir'], (err) => {
if (err) {
throw (err);
}
// Delete tmp folder
fs.rmdir(meta['tmpDir'], (err) => {
if (err) {
throw (err);
}

});
});
}
}

return meta;
Expand Down Expand Up @@ -562,7 +566,7 @@ function FileTransferAgent(context) {
const quoteIndex = command.substring(startIndex).indexOf('\'');
let endIndex = spaceIndex;
if (quoteIndex !== -1 && quoteIndex < spaceIndex) {
endIndex = quoteIndex;
endIndex = quoteIndex;
}
const filePath = command.substring(startIndex, startIndex + endIndex);
return filePath;
Expand Down Expand Up @@ -757,7 +761,7 @@ function FileTransferAgent(context) {
if (sourceCompression === 'auto_detect') {
autoDetect = true;

} else if (sourceCompression === typeof('undefined')) {
} else if (sourceCompression === typeof ('undefined')) {
autoDetect = false;
} else {
userSpecifiedSourceCompression = fileCompressionType.lookupByMimeSubType(sourceCompression);
Expand Down Expand Up @@ -838,6 +842,6 @@ function FileTransferAgent(context) {
}

//TODO SNOW-992387: Create a function to renew expired client
function renewExpiredClient() {}
function renewExpiredClient() { }

module.exports = FileTransferAgent;

0 comments on commit 2b1cfb6

Please sign in to comment.