Skip to content

Instantly share code, notes, and snippets.

@KcPele
Created April 12, 2022 06:47
Show Gist options
  • Save KcPele/0d8bf22c6ad9b4bc9fd15be190bf3f96 to your computer and use it in GitHub Desktop.
Save KcPele/0d8bf22c6ad9b4bc9fd15be190bf3f96 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=true&runs=200&gist=
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;
contract Upload {
//task: upload files, retreive, share
//@geting the data format
struct DataFormat {
string name;
string path;
bool isPrivate;
address owner;
}
//modifiers here
modifier isOwnerOfFile(uint _fileIndex) {
//check if the msg.sender is the owner of the indexed
require(msg.sender == fileFormat[_fileIndex].owner, "This file does not belong to u");
_;
}
//keeping track of the users with access
mapping(address => mapping(uint256 => address[])) accessAddress;
DataFormat[] private fileFormat;
//or i clould also create a mapping of address to the struct and remove the owner file
// so that it will be userUpload[msg.sender] it will reaturn all the user data format in an array
// mapping(address => DataFormat[]) ownerFileFormat;
//function that sets the data
function setFileData(string calldata _name, string calldata _path) external returns(bool){
// uint file_id = fileFormat.push(DataFormat(_name, _path, false, msg.sender)) -1;
DataFormat memory data;
data.name = _name;
data.path = _path;
data.isPrivate = false;
data.owner = msg.sender;
fileFormat.push(data);
//getting the returned indexed from pushing data to an array
uint fileIndex = fileFormat.length -1;
setAccessUser(fileIndex, msg.sender);
//emit event here
return true;
}
//toggile the file privacy to rescript access
function toggleFilePrivacy(uint _fileIndex) public isOwnerOfFile(_fileIndex) {
//toggle the files
fileFormat[_fileIndex].isPrivate = !fileFormat[_fileIndex].isPrivate;
}
//seting the access if it is private
function setAccessUser(uint _fileIndex, address _address) public isOwnerOfFile(_fileIndex) {
//check if the fileIndex exist and for only msg..sender
//check if msg.sender has a file on store
accessAddress[msg.sender][_fileIndex].push(_address);
}
//to get all the users that have access to a particuler file
function getAccessUser(uint fileIdex) public view returns(address[] memory){
//check if the fileIndex exist and for only msg..sender
return accessAddress[msg.sender][fileIdex];
}
//return who can view
function whoCanView(uint _fileIndex, address _address) public view returns(DataFormat memory, string memory){
//if file is private
if(fileFormat[_fileIndex].isPrivate){
//check if the user address file list has u(msg.sender) as part of accessUsers
bool foundAccessAddress;
//loop through accessAddress to see if msg.snder is part of them
//set foundAccessAddress true if its in else false
for(uint i=0; i < accessAddress[_address][_fileIndex].length; i++){
address accAddress = accessAddress[_address][_fileIndex][i];
if(accAddress != msg.sender){
foundAccessAddress = false;
} else {
foundAccessAddress = true;
}
}
require(foundAccessAddress, "Not allowed to view this file");
//emit event to notify the owner who viewed his file private file
return (fileFormat[_fileIndex], "private");
} else {
return (fileFormat[_fileIndex], "public");
}
}
//data to be collected
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment