Skip to content

Commit

Permalink
#OBS-I289 : added route (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
yashashkumar authored Oct 25, 2024
1 parent c90606f commit 5a16257
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 57 deletions.
1 change: 1 addition & 0 deletions api-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
60 changes: 4 additions & 56 deletions api-service/src/controllers/GenerateSignedURL/GenerateSignedURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any> = schemaValidation(req.body, GenerateURL)
if (!isRequestValid.isValid) {
Expand All @@ -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) {
Expand All @@ -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<string | any>, access: string): Record<string, any> => {
if (access === URLAccess.Read) {
return transformReadFiles(fileList)
}
return transformWriteFiles(fileList)
}

const transformReadFiles = (fileNames: Array<string | any>) => {
const fileMap = new Map();
const updatedFileNames = _.map(fileNames, file => {
fileMap.set(file, file)
return getFilePath(file)
})
return { filesList: fileMap, updatedFileNames }
}

const transformWriteFiles = (fileNames: Array<string | any>) => {
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<string>): boolean => {
return _.size(files) > maxFiles
}
Expand Down
3 changes: 2 additions & 1 deletion api-service/src/routes/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5a16257

Please sign in to comment.