Skip to content

Commit

Permalink
sample changes for the poc
Browse files Browse the repository at this point in the history
  • Loading branch information
midhun-aot committed Nov 9, 2023
1 parent 255c522 commit 7091b8e
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
109 changes: 109 additions & 0 deletions frontend/src/app/features/bcbox/FileUpload.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Needs to be moved to utility class later
import axios from "axios";
import { useState } from "react";


export const FileUpload = () => {


const [bucketId,setBucketId] = useState('');
const [fileToUpload,setFileToUpload] = useState();


const bcBoxBaseURL = "https://coms.api.gov.bc.ca";
const bcBoxBucketURL = "/api/v1/bucket";
const token = "";

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

const setBucketPermission = "https://coms.api.gov.bc.ca/api/v1/permission/bucket/";

const fileUploadEndpoint= "api/v1/object?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=latest2_bin.txt; filename*=UTF-8''latest2_bin.txt'"
}
});

const createBucket = () => {
console.log("create bucket");


axiosInstance.put(bcBoxBucketURL,{
"accessKeyId": "nr-srs-dlv",
"active": true,
"bucket": "vkophq",
"bucketName": "application/06NOV1133",
"endpoint": "https://nrs.objectstore.gov.bc.ca",
"region": "ca-central-1",
"secretAccessKey": "Ht53nIvXzmc7eGFcQwoP+0UsWWiATHeV/dW67gHO",
"key": "application/06NOV1133"
}).then((result)=>{
console.log(result);
if(result.data!=null && result.data.bucketId != '')
setBucketId(result.data.bucketId);
});

}

const findUser = () => {
console.log("find user");
axiosInstance.get(findUserEmail+"[email protected]").then((result)=>{
console.log(result);
result.data.forEach((user:any)=>{
console.log(user)

axiosInstance.put(setBucketPermission + bucketId,[{
"permCode": "CREATE",
"userId": user.userId
}])


})}
);
}

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

const uploadFileSubmit = (event:any) =>{
console.log("upload file", fileToUpload);
//setFileToUpload( event.target.files[0]);

axiosInstanceToFileUpload.put(fileUploadEndpoint+bucketId, window.URL.createObjectURL(fileToUpload)).then((result)=>{
console.log('file upload',result);

});
}

return (
<div style={{ "margin-top": "65px" }}>
<h2>Upload Files To Your Applications</h2>
<input type="file" onChange={(e)=>{uploadFile(e)}}></input>
<div>
<input type="button" value="Create Bucket" onClick={()=>{createBucket()}}/>
<br/>
<input type="button" value="Find User" onClick={()=>{findUser()}} />
<br/>
<input type="button" value="Upload File" onClick={(e)=>{uploadFileSubmit(e)}} />
</div>
</div>
);
};
2 changes: 2 additions & 0 deletions frontend/src/app/routes/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ProtectedRoute from "./ProtectedRoute";
import { SignUp } from "../features/signup/signup";
import { UserProfile } from "../features/users/UserProfile";
import Map from '../features/Map/Map'
import {FileUpload} from "../features/bcbox/FileUpload";

const AppRoutes = () => {
return (
Expand All @@ -18,6 +19,7 @@ const AppRoutes = () => {
<Route path="/profile" element={<ProtectedRoute element={<UserProfile/>}/>}/>
<Route path="/signup" element={<SignUp/>}/>
<Route path="/map" element={<Map/>}/>
<Route path="/fileupload" element={<FileUpload/>}/>
<Route path="/users" element={<UsersList />}></Route>
<Route path="/users/add" element={<AddUserForm />}></Route>
<Route path="*" element={<h1>Page not found</h1>}></Route>
Expand Down

0 comments on commit 7091b8e

Please sign in to comment.