Skip to content

Commit 84d5014

Browse files
committed
upload files to pre assigned URl
1 parent 36896d9 commit 84d5014

5 files changed

Lines changed: 300 additions & 143 deletions

File tree

src/connect_repo/s3_uploader.ts

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import { generateSettings } from "../settings";
1111
import { uuidv4 } from '../utils/setup_utils';
1212
import { removeFile } from '../utils/file_utils';
1313
import { pathUtils } from '../utils/path_utils';
14-
import { uploadFileTos3 } from '../utils/upload_utils';
14+
import { uploadFileTos3, uploadFileToGCS } from '../utils/upload_utils';
1515
import { CodeSyncLogger } from '../logger';
1616
import { CODESYNC_STATES, CodeSyncState } from '../utils/state_utils';
1717
import { IS3UploaderFile, IS3UploaderPreProcess } from '../interface';
1818
import { S3_UPLOADR_RETRY_AFTER } from '../constants';
19+
import { UserUtils } from "../utils/user_utils";
1920

2021

2122
export class s3UploaderUtils {
@@ -272,26 +273,46 @@ class s3Uploader extends s3UploaderUtils {
272273
return await this.processTasks(0);
273274
}
274275

275-
createTasks = async (content: IS3UploaderFile) => {
276-
// Proceess the given file and create parallelTasks
277-
const pathUtils_ = new pathUtils(this.repoPath, content.branch);
278-
this.originalsRepoBranchPath = pathUtils_.getOriginalsRepoBranchPath();
279-
const fileRelPaths = Object.keys(content.file_path_and_urls);
280-
if (!fileRelPaths || isEmpty(content.file_path_and_urls)) return;
281-
this.filePathAndURLs = <any>{};
282-
// Skip files which don't exist in .originals or don't have URL
283-
fileRelPaths.sort().forEach(fileRelPath => {
284-
const originalsFilePath = path.join(this.originalsRepoBranchPath, fileRelPath);
285-
if (!fs.existsSync(originalsFilePath)) return;
286-
const presignedURL = content.file_path_and_urls[fileRelPath];
287-
if (!presignedURL) return removeFile(originalsFilePath, "s3Uploader.deleting-originals-file");
288-
this.filePathAndURLs[fileRelPath] = presignedURL;
289-
this.tasks.push(async function (callback: any) {
290-
const json = <any>await uploadFileTos3(originalsFilePath, presignedURL);
291-
callback(json.error, originalsFilePath);
292-
});
293-
});
294-
}
276+
createTasks = async (content: IS3UploaderFile) => {
277+
// Proceess the given file and create parallelTasks
278+
const pathUtils_ = new pathUtils(this.repoPath, content.branch);
279+
this.originalsRepoBranchPath = pathUtils_.getOriginalsRepoBranchPath();
280+
const fileRelPaths = Object.keys(content.file_path_and_urls);
281+
if (!fileRelPaths || isEmpty(content.file_path_and_urls)) return;
282+
this.filePathAndURLs = <any>{};
283+
// Skip files which don't exist in .originals or don't have URL
284+
fileRelPaths.sort().forEach((fileRelPath) => {
285+
const originalsFilePath = path.join(
286+
this.originalsRepoBranchPath,
287+
fileRelPath
288+
);
289+
if (!fs.existsSync(originalsFilePath)) return;
290+
const presignedURL = content.file_path_and_urls[fileRelPath];
291+
if (!presignedURL)
292+
return removeFile(
293+
originalsFilePath,
294+
"s3Uploader.deleting-originals-file"
295+
);
296+
this.filePathAndURLs[fileRelPath] = presignedURL;
297+
this.tasks.push(async function (callback: any) {
298+
const userUtils = new UserUtils();
299+
const activeUser: any = userUtils.getActiveUser();
300+
let users = <any>{};
301+
users = readYML(generateSettings().USER_PATH) || {};
302+
let json: any = null;
303+
let key: any= null;
304+
if (activeUser && activeUser?.email in users) {
305+
key = users[activeUser.email].gcp_private_key;
306+
}
307+
if (key) {
308+
json = <any>await uploadFileToGCS(originalsFilePath, presignedURL);
309+
} else {
310+
json = <any>await uploadFileTos3(originalsFilePath, presignedURL);
311+
}
312+
callback(json.error, originalsFilePath);
313+
});
314+
});
315+
};
295316

296317
processTasks = async (startIndex: number) => {
297318
if (!await this.shouldProceed()) return;

0 commit comments

Comments
 (0)