Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue [Edge Case] Handle the Empty file Upload #111

Open
wants to merge 5 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 36 additions & 19 deletions src/pages/Dashboard/dashbaord.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@ const DashBoard = (_props) => {
}, []);
// Function to get the file buffer from the drag and drop
const captureFile = (files, event) => {
setLoader(true);
setMessage("Encrypting your File");
event.stopPropagation();
event.preventDefault();
const file = files[0];
setfilename(file.name);
let reader = new window.FileReader();
reader.readAsArrayBuffer(file);
reader.onloadend = () => convertToBuffer(reader);
setLoader(false);
setMessage("Encryption Successful. Click Upload");
// check for no file added or an empty file

setLoader(true);
setMessage("Encrypting your File");
event.stopPropagation();
event.preventDefault();
const file = files[0];
setfilename(file.name);
let reader = new window.FileReader();
reader.readAsArrayBuffer(file);
reader.onloadend = () => convertToBuffer(reader);
setLoader(false);
setMessage("Encryption Successful. Click Upload");
};

// Function to convert the file object to the Array Buffer and setting it to state
Expand All @@ -76,6 +78,13 @@ const DashBoard = (_props) => {
event.preventDefault();
setLoader(true);
setMessage("Preparing to Upload to Blockchain");
// check for no file added or an empty file
if (uploadFiles.length > 0 && uploadFiles[0].size === 0) {
setLoader(false);
return window.alert(
"The file you tried to upload is empty. Please check the file and try again."
);
} else {
event.stopPropagation();
//save document to IPFS,return its hash#, and set hash# to state
const publicHASH = await Validator("publicHash");
Expand All @@ -92,7 +101,7 @@ const DashBoard = (_props) => {
setHash(hash);
const username = Validator("username");
try{
// Add the file name and dile hash retrived from the ipfs will be sent to the smart contract
// Add the file name and dile hash retrived from the ipfs will be sent to the smart contract
setMessage("Upload Request Sent to Blockchain");
const result = await AddFile(contract, username, hash, filename);
// If the file is successfully uploaded to blockchain, notify it to user
Expand All @@ -112,7 +121,7 @@ const DashBoard = (_props) => {
setLoader(false);

}

}
}
catch(error){
setLoader(false);
Expand All @@ -121,7 +130,7 @@ const DashBoard = (_props) => {
);
return;
}

};
// get ipfs publicHash, if already logged in
const token = Validator("publicHash");
Expand Down Expand Up @@ -164,13 +173,21 @@ const DashBoard = (_props) => {
event.currentTarget.style.border = "";
}}
onDrop={(files, event) => {
event.currentTarget.style.background = "yellowgreen";
event.currentTarget.style.color = "black";
event.currentTarget.style.opacity = "";
event.currentTarget.style.border = "";
event.currentTarget.style.padding = "25px";
captureFile(files, event);
setUploadFiles(files);
// having trouble with this line. uploadFiles.length always === 0 at this point
if (uploadFiles.length > 0 && uploadFiles[0].size === 0) {
return window.alert(
"The file you tried to upload is empty. Please check the file and try again." + uploadFiles[0].size
);
} else {

event.currentTarget.style.background = "yellowgreen";
event.currentTarget.style.color = "black";
event.currentTarget.style.opacity = "";
event.currentTarget.style.border = "";
event.currentTarget.style.padding = "25px";
}
}}
>
Drop a file here!
Expand Down
6 changes: 6 additions & 0 deletions src/pages/Login/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ const Login = () => {
// fucntion to read the private file
function readkeyFile(file) {
var reader = new FileReader();

reader.readAsText(file, "UTF-8");

reader.onload = (evt) => {
if(reader.result.length === 0) {
window.alert("The file you uploaded is empty");
return;
}
setPrivateKey(evt.target.result);
};
reader.onerror = () => console.log("error");
Expand Down