|
1 | 1 | // SPDX-License-Identifier: MIT |
2 | 2 | pragma solidity ^0.8.0; |
3 | 3 |
|
4 | | -// Import IPFS library |
5 | | -import "./IPFS.sol"; |
| 4 | +contract Hostify { |
| 5 | + // Name |
| 6 | + string public name = "Hostify"; |
6 | 7 |
|
7 | | -contract CDN { |
8 | | - // Initialize IPFS object |
9 | | - IPFS private ipfs; |
| 8 | + // Number of files |
| 9 | + uint256 public fileCount = 0; |
10 | 10 |
|
11 | | - // Set the IPFS object in the constructor |
12 | | - constructor(address ipfsAddress) { |
13 | | - ipfs = IPFS(ipfsAddress); |
14 | | - } |
| 11 | + // Mapping fileId=>Struct |
| 12 | + mapping(uint256 => File) public files; |
15 | 13 |
|
16 | | - // Add file to IPFS and return the file hash |
17 | | - function addFile(bytes memory fileData) public returns (bytes32) { |
18 | | - bytes32 hash = ipfs.add(fileData); |
19 | | - return hash; |
| 14 | + // Struct |
| 15 | + struct File { |
| 16 | + uint256 fileId; |
| 17 | + string fileHash; |
| 18 | + uint256 fileSize; |
| 19 | + string fileType; |
| 20 | + string fileName; |
| 21 | + string fileDescription; |
| 22 | + uint256 uploadTime; |
| 23 | + address payable uploader; |
20 | 24 | } |
21 | 25 |
|
22 | | - // Retrieve file from IPFS by its hash |
23 | | - function getFile(bytes32 hash) public view returns (bytes memory) { |
24 | | - bytes memory fileData = ipfs.get(hash); |
25 | | - return fileData; |
| 26 | + // Event ( Oracle ) |
| 27 | + event FileUploaded( |
| 28 | + uint256 fileId, |
| 29 | + string fileHash, |
| 30 | + uint256 fileSize, |
| 31 | + string fileType, |
| 32 | + string fileName, |
| 33 | + string fileDescription, |
| 34 | + uint256 uploadTime, |
| 35 | + address payable uploader |
| 36 | + ); |
| 37 | + |
| 38 | + constructor() public {} |
| 39 | + |
| 40 | + // Upload File function |
| 41 | + function uploadFile( |
| 42 | + string memory _fileHash, |
| 43 | + uint256 _fileSize, |
| 44 | + string memory _fileType, |
| 45 | + string memory _fileName, |
| 46 | + string memory _fileDescription |
| 47 | + ) public { |
| 48 | + // Make sure the file hash exists |
| 49 | + require(bytes(_fileHash).length > 0); |
| 50 | + |
| 51 | + // Make sure file type exists |
| 52 | + require(bytes(_fileType).length > 0); |
| 53 | + |
| 54 | + // Make sure file description exists |
| 55 | + require(bytes(_fileDescription).length > 0); |
| 56 | + |
| 57 | + // Make sure file fileName exists |
| 58 | + require(bytes(_fileName).length > 0); |
| 59 | + |
| 60 | + // Make sure uploader address exists |
| 61 | + require(msg.sender != address(0)); |
| 62 | + |
| 63 | + // Make sure file size is more than 0 |
| 64 | + require(_fileSize > 0); |
| 65 | + |
| 66 | + // Increment file id |
| 67 | + fileCount++; |
| 68 | + |
| 69 | + // Add File to the contract |
| 70 | + files[fileCount] = File( |
| 71 | + fileCount, |
| 72 | + _fileHash, |
| 73 | + _fileSize, |
| 74 | + _fileType, |
| 75 | + _fileName, |
| 76 | + _fileDescription, |
| 77 | + block.timestamp, |
| 78 | + msg.sender |
| 79 | + ); |
| 80 | + |
| 81 | + // Trigger an event |
| 82 | + emit FileUploaded( |
| 83 | + fileCount, |
| 84 | + _fileHash, |
| 85 | + _fileSize, |
| 86 | + _fileType, |
| 87 | + _fileName, |
| 88 | + _fileDescription, |
| 89 | + now, |
| 90 | + msg.sender |
| 91 | + ); |
26 | 92 | } |
27 | 93 | } |
0 commit comments