Skip to content

Commit

Permalink
Redirecting To bcbox On Clicking Uploadlink
Browse files Browse the repository at this point in the history
  • Loading branch information
midhun-aot committed Dec 22, 2023
1 parent c9dc2d6 commit 33895b4
Showing 1 changed file with 18 additions and 134 deletions.
152 changes: 18 additions & 134 deletions frontend/src/app/features/bcbox/FileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ import axios from "axios";
import { useState } from "react";
import { getUser } from "../../helpers/utility";
import { useEffect } from "react";
import { builtinModules } from "module";
// import { builtinModules } from "module";
import { useAuth } from "react-oidc-context";

export const FileUpload = () => {
const auth = useAuth();
const [bucketId, setBucketId] = useState("");
const [fileToUpload, setFileToUpload] = useState();
const [appId, setAppId] = useState("");
const [fileName, setFileName] = useState("");
const [isDisabled, setIsDisabled] = useState(false);

useEffect(() => {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
Expand All @@ -27,16 +22,17 @@ export const FileUpload = () => {
auth.user.profile.identity_provider !== "bceid"
) {
alert("You are not allowed to use this functionality");
setIsDisabled(true);
window.close();
//return;
} else {
setIsDisabled(false);
}
}, []);

console.log(auth);

const bcBoxAPPUI =
process.env.REACT_APP_BCBOX_APP_URL ||
((window as any)._env_ && (window as any)._env_.REACT_APP_BCBOX_APP_URL);

const bcBoxBaseURL =
process.env.REACT_APP_BCBOX_BASE_URL ||
((window as any)._env_ && (window as any)._env_.REACT_APP_BCBOX_BASE_URL);
Expand All @@ -45,28 +41,6 @@ export const FileUpload = () => {
((window as any)._env_ && (window as any)._env_.REACT_APP_BCBOX_BUCKET_URL);
const token = getUser()?.access_token;

const findUserEmail =
process.env.REACT_APP_BCBOX_FIND_USER_ENDPOINT ||
((window as any)._env_ &&
(window as any)._env_.REACT_APP_BCBOX_FIND_USER_ENDPOINT);
const setBucketPermission =
process.env.REACT_APP_BCBOX_SET_USER_PERMISSION ||
((window as any)._env_ &&
(window as any)._env_.REACT_APP_BCBOX_SET_USER_PERMISSION);
const fileUploadEndpoint =
process.env.REACT_APP_BCBOX_FILE_UPLOAD_ENDPOINT ||
((window as any)._env_ &&
(window as any)._env_.REACT_APP_BCBOX_FILE_UPLOAD_ENDPOINT);

// const bcBoxBaseURL = "http://localhost:4050";
// const bcBoxBucketURL = "/api/v1/bucket";

// const findUserEmail = "api/v1/user?email=";

// const setBucketPermission = "/api/v1/permission/bucket/";

// const fileUploadEndpoint = "api/v1/object?bucketId=";

const accessKeyId =
process.env.REACT_APP_COMS_ACCESS_KEY_ID ||
((window as any)._env_ &&
Expand All @@ -85,6 +59,15 @@ export const FileUpload = () => {
((window as any)._env_ &&
(window as any)._env_.REACT_APP_COMS_ACCESS_REGION);

const axiosInstance = axios.create({
baseURL: bcBoxBaseURL,
timeout: 1000,
headers: {
Authorization: "Bearer " + token,
"Content-Type": "application/json",
},
});

useEffect(() => {
if (appId !== "") {
axiosInstance
Expand All @@ -102,115 +85,16 @@ export const FileUpload = () => {
(result) => {
console.log(result);
if (result.data != null && result.data.bucketId != "")
setBucketId(result.data.bucketId);
window.location.assign(
bcBoxAPPUI + "/list/objects?bucketId=" + result.data.bucketId
);
},
(error) => {
alert("Unable to Proceed. Please try again.");
setIsDisabled(true);
}
);
}
}, [appId]);

useEffect(() => {
if (bucketId !== "" && bucketId !== null) {
axiosInstance.get(findUserEmail + auth?.user?.profile.email).then(
(result) => {
console.log(result);
result.data.forEach((user: any) => {
console.log(user);

axiosInstance.put(bcBoxBaseURL + setBucketPermission + bucketId, [
{
permCode: "CREATE",
userId: user.userId,
},
]);
});
},
(error) => {
alert("Unable to find user in BCBOX cannot proceed.");
setIsDisabled(true);
}
);
}
}, [bucketId]);

const axiosInstance = axios.create({
baseURL: bcBoxBaseURL,
timeout: 1000,
headers: {
Authorization: "Bearer " + token,
"Content-Type": "application/json",
},
});

const axiosInstanceToFileUpload = axios.create({
baseURL: bcBoxBaseURL,
timeout: 1000,
headers: {
Authorization: "Bearer " + token,
"Content-Type": "text/plain",
"Content-Disposition": 'attachment; filename="' + fileName + '"',
},
});

const uploadFile = (event: any) => {
console.log("upload file", event.target.files);
setFileToUpload(event.target.files[0]);
setFileName(event.target.files[0].name);
};

const uploadFileSubmit = (event: any) => {
if (bucketId !== null && bucketId !== "")
axiosInstanceToFileUpload
.put(
fileUploadEndpoint + bucketId,
window.URL.createObjectURL(fileToUpload)
)
.then(
(result) => {
console.log("file upload", result);
alert("File successfully upload.");
window.close();
},
(error) => {
console.log("file upload", error);
if (error.response.status === 409) {
alert("File already exists.");
} else {
alert("Failed to upload.");
}
}
);
else alert("Unable to find application upload location");
//setFileToUpload( event.target.files[0]);
};

return (
<div style={{ marginTop: "65px" }}>
<div hidden={!isDisabled}>
{" "}
<b> You are not allowed to use this functionality </b>
</div>
<input
disabled={isDisabled}
type="file"
onChange={(e) => {
uploadFile(e);
}}
></input>
<div>
<input
disabled={isDisabled}
className="btn btn-success"
type="button"
value="Upload File"
onClick={(e) => {
uploadFileSubmit(e);
}}
/>
</div>
</div>
);
return <div>Redirecting to BC BOX please wait</div>;
};

0 comments on commit 33895b4

Please sign in to comment.