From 5a16257f6144b9d3e4e177fb9f84c81058ba7975 Mon Sep 17 00:00:00 2001 From: yashash <126703764+yashashkumar@users.noreply.github.com> Date: Fri, 25 Oct 2024 17:59:03 +0530 Subject: [PATCH] #OBS-I289 : added route (#265) --- api-service/package.json | 1 + .../GenerateSignedURL/GenerateSignedURL.ts | 60 ++----------------- api-service/src/routes/Router.ts | 3 +- 3 files changed, 7 insertions(+), 57 deletions(-) diff --git a/api-service/package.json b/api-service/package.json index 2b858f69..782cda64 100644 --- a/api-service/package.json +++ b/api-service/package.json @@ -61,6 +61,7 @@ "@babel/traverse": "7.23.2" }, "devDependencies": { + "@types/busboy": "^1.5.4", "@types/chai": "^4.3.3", "@types/chai-as-promised": "^7.1.5", "@types/chai-spies": "^1.0.3", diff --git a/api-service/src/controllers/GenerateSignedURL/GenerateSignedURL.ts b/api-service/src/controllers/GenerateSignedURL/GenerateSignedURL.ts index 6182f9b3..6454b1fa 100644 --- a/api-service/src/controllers/GenerateSignedURL/GenerateSignedURL.ts +++ b/api-service/src/controllers/GenerateSignedURL/GenerateSignedURL.ts @@ -6,20 +6,20 @@ import logger from "../../logger"; import { ErrorObject } from "../../types/ResponseModel"; import { schemaValidation } from "../../services/ValidationService"; import GenerateURL from "./GenerateSignedURLValidationSchema.json" -import { cloudProvider } from "../../services/CloudServices"; import { config } from "../../configs/Config"; import { URLAccess } from "../../types/SampleURLModel"; -import { v4 as uuidv4 } from "uuid"; -import path from "path"; +import { generatePreSignedUrl } from "./helper"; export const apiId = "api.files.generate-url" export const code = "FILES_GENERATE_URL_FAILURE" const maxFiles = config.presigned_url_configs.maxFiles +let containerType: string; const generateSignedURL = async (req: Request, res: Response) => { const requestBody = req.body const msgid = _.get(req, ["body", "params", "msgid"]); const resmsgid = _.get(res, "resmsgid"); + containerType = _.get(req, ["body", "request", "type"]); try { const isRequestValid: Record = schemaValidation(req.body, GenerateURL) if (!isRequestValid.isValid) { @@ -46,21 +46,7 @@ const generateSignedURL = async (req: Request, res: Response) => { errCode: "BAD_REQUEST" } as ErrorObject, req, res); } - - const { filesList, updatedFileNames } = transformFileNames(files, access) - logger.info(`Updated file names with path:${updatedFileNames}`) - - const urlExpiry: number = getURLExpiry(access) - const preSignedUrls = await Promise.all(cloudProvider.generateSignedURLs(config.cloud_config.container, updatedFileNames, access, urlExpiry)) - const signedUrlList = _.map(preSignedUrls, list => { - const fileNameWithUid = _.keys(list)[0] - return { - filePath: getFilePath(fileNameWithUid), - fileName: filesList.get(fileNameWithUid), - preSignedUrl: _.values(list)[0] - } - }) - + const signedUrlList = await generatePreSignedUrl(access, files, containerType) logger.info({ apiId, requestBody, msgid, resmsgid, response: signedUrlList, message: `Sample urls generated successfully for files:${files}` }) ResponseHandler.successResponse(req, res, { status: httpStatus.OK, data: signedUrlList }) } catch (error: any) { @@ -74,44 +60,6 @@ const generateSignedURL = async (req: Request, res: Response) => { } } -const getFilePath = (file: string) => { - return `${config.cloud_config.container}/${config.presigned_url_configs.service}/user_uploads/${file}` -} - -const transformFileNames = (fileList: Array, access: string): Record => { - if (access === URLAccess.Read) { - return transformReadFiles(fileList) - } - return transformWriteFiles(fileList) -} - -const transformReadFiles = (fileNames: Array) => { - const fileMap = new Map(); - const updatedFileNames = _.map(fileNames, file => { - fileMap.set(file, file) - return getFilePath(file) - }) - return { filesList: fileMap, updatedFileNames } -} - -const transformWriteFiles = (fileNames: Array) => { - const fileMap = new Map(); - const updatedFileNames = _.map(fileNames, file => { - const uuid = uuidv4().replace(/-/g, "").slice(0, 6); - const ext = path.extname(file) - const baseName = path.basename(file, ext) - const updatedFileName = `${baseName}_${uuid}${ext}` - fileMap.set(updatedFileName, file) - return getFilePath(updatedFileName) - }) - return { filesList: fileMap, updatedFileNames } - -} - -const getURLExpiry = (access: string) => { - return access === URLAccess.Read ? config.presigned_url_configs.read_storage_url_expiry : config.presigned_url_configs.write_storage_url_expiry -} - const checkLimitExceed = (files: Array): boolean => { return _.size(files) > maxFiles } diff --git a/api-service/src/routes/Router.ts b/api-service/src/routes/Router.ts index 40f1df02..1e39d942 100644 --- a/api-service/src/routes/Router.ts +++ b/api-service/src/routes/Router.ts @@ -31,6 +31,7 @@ import { OperationType, telemetryAuditStart } from "../services/telemetry"; import telemetryActions from "../telemetry/telemetryActions"; import datasetMetrics from "../controllers/DatasetMetrics/DatasetMetricsController"; import checkRBAC from "../middlewares/RBAC_middleware"; +import connectorRegisterController from "../controllers/ConnectorRegister/ConnectorRegisterController"; export const router = express.Router(); @@ -58,7 +59,7 @@ router.post("/datasets/copy", setDataToRequestObject("api.datasets.copy"), onReq router.post("/connectors/list", setDataToRequestObject("api.connectors.list"), onRequest({ entity: Entity.Management }), telemetryAuditStart({action: telemetryActions.listConnectors, operationType: OperationType.GET}), checkRBAC.handler(), ConnectorsList); router.get("/connectors/read/:id", setDataToRequestObject("api.connectors.read"), onRequest({entity: Entity.Management }), telemetryAuditStart({action: telemetryActions.readConnectors, operationType: OperationType.GET}), checkRBAC.handler(), ConnectorsRead); router.post("/datasets/import", setDataToRequestObject("api.datasets.import"), onRequest({ entity: Entity.Management }), checkRBAC.handler(), DatasetImport); - +router.post("/connector/register", setDataToRequestObject("api.connector.register"), onRequest({ entity: Entity.Management }), connectorRegisterController); //Wrapper Service router.post("/obsrv/data/sql-query", setDataToRequestObject("api.obsrv.data.sql-query"), onRequest({ entity: Entity.Data_out }), checkRBAC.handler(), sqlQuery); router.post("/data/metrics", setDataToRequestObject("api.data.metrics"), onRequest({ entity: Entity.Data_out }), datasetMetrics)