本系列文章是针对 https://blog.csdn.net/weixin_43668031/article/details/83962959 内容的实现所编写的。开发经历包括思考过程、重构和推翻重来。
在上一节中Video表(结构体)中含有 uint gratuitySum;字段,此字段冗余,可以通过实时计算获得,可以在结构体中删除。Video 设计如下
struct Video {
string title;
string cover;
string videoinfo;
string info;
uint duration;
uint timestamp;
address payable author;
uint commentsNum;
uint gratuityNum;
uint lableNum;
uint fileNum;
uint8 permission;
mapping (uint => Videofile) videofiles;
mapping (uint => Lable) lables;
mapping (uint => Gratuity) gratuitys;
mapping (uint => Comment) comments;
}
uint public videoNum;
mapping(uint => Video) videos;
0x3.智能合约
区块链的智能合约的操作方法更像是mysql dba编写的过程和函数,但是这是远远不够的,我们连传统数据库的insert、delete、update、select都还没有提供,就连最基本的crud也需要自己去实现。这里只要考虑像C语言那样去操作结构体即可,不需要考虑数据同步、分布式等技术,那是以太坊已经给我做好的铺垫。也因为如此,会在我的代码上看到大量set、get方法。
1.用户表
function setMyInfo (string memory _nickname, string memory _profile, string memory _avatar) public {
users[msg.sender] = User(_nickname, _profile, _avatar, 0);
}
function setMyNickname (string memory _nickname) public {
users[msg.sender].nickname = _nickname;
}
function setMyProfile (string memory _profile) public {
users[msg.sender].profile = _profile;
}
function setMyAvatar (string memory _avatar) public {
users[msg.sender].avatar = _avatar;
}
function getUserInfo (address _userAdd) view public returns (string memory nickname, string memory profile, string memory avatar, uint videoNums) {
return (users[_userAdd].nickname, users[_userAdd].profile, users[_userAdd].avatar, users[_userAdd].videoNums);
}
2.视频存储方法
function publish (string memory _title, string memory _cover, string memory _videoinfo, string memory _info, uint _duration, string memory _filename, string memory _fileinfo, uint _size, uint32 _width, uint32 _height, uint32 _fps) public returns (uint articleId) {
videos[videoNum++] = Video(_title, _cover, _videoinfo, _info, _duration, now, msg.sender, 0, 0, 0, 0, 0);
videos[videoNum].videofiles[videos[videoNum].fileNum++] = Videofile(_filename, _fileinfo, _size, _width, _height, _fps, 0);
return videoNum-1;
}
function setVideoPermission(uint _videoId, uint8 _value) public {
if( msg.sender != videos[_videoId].author) return;
videos[_videoId].permission = _value;
}
function setVideo(uint _videoId, string memory _cover, string memory _videoinfo) public {
if( msg.sender != videos[_videoId].author) return;
videos[_videoId].cover = _cover;
videos[_videoId].videoinfo = _videoinfo;
}
function setVideoInfo(uint _videoId, string memory _title, string memory _info) public {
if( msg.sender != videos[_videoId].author) return;
videos[_videoId].title = _title;
videos[_videoId].info = _info;
}
function addVideofile(uint _videoId, string memory _filename, string memory _fileinfo, uint _size, uint32 _width, uint32 _height, uint32 _fps) public {
if( msg.sender != videos[_videoId].author){
videos[_videoId].videofiles[videos[_videoId].fileNum++] = Videofile(_filename, _fileinfo, _size, _width, _height, _fps, 1);
}else{
videos[_videoId].videofiles[videos[_videoId].fileNum++] = Videofile(_filename, _fileinfo, _size, _width, _height, _fps, 0);
}
}
function setFilePermission(uint _videoId, uint _fileId, uint8 _value) public {
if( msg.sender == videos[_videoId].author ){
videos[_videoId].videofiles[_fileId].filePermission = _value;
}
}
function getVideo (uint _videoId) view public returns (string memory title, string memory cover, string memory videoinfo,string memory info, uint timestamp, address author, uint commentsNum, uint videofiles, uint gratuitySum) {
if( videos[_videoId].permission != 0 && msg.sender != videos[_videoId].author ){
Video storage _video = videos[_videoId];
for (uint i = 0; i < _video.gratuityNum; i++)
gratuitySum += _video.gratuitys[i].gratuity;
return (_video.title, _video.cover,_video.videoinfo,_video.info, _video.timestamp, _video.author,_video.commentsNum, _video.fileNum, gratuitySum);
}
}
function getVideoFile (uint _videoId, uint _fileId) view public returns (string memory filename, string memory fileinfo,uint size, uint32 width, uint32 height, uint32 fps, uint fileNum){
if(videos[_videoId].permission != 0 && msg.sender == videos[_videoId].author || videos[_videoId].permission == 0 && videos[_videoId].videofiles[_fileId].filePermission ==0){
Video storage _video = videos[_videoId];
Videofile storage _videofile = _video.videofiles[_fileId];
return (_videofile.filename, _videofile.fileinfo, _videofile.size, _videofile.width, _videofile.height, _videofile.fps, _video.fileNum);
}else{
return ("", "", 0, 0, 0, 0, videos[_videoId].fileNum);
}
}
这里会详细说明一下:
getVideoFile 方法中的判断,如果多视频资源有访问权限,并视频的文件也有访问权限,那就是可以访问的,如果视频无访问权限,但是是作者自己上传的视频,也可以访问,还要用于核审呢!
addVideofile方法中如果是作者本人再向视频资源中添加视频文件,那么默认是直接允许访问的,如果是他人向我的视频资源中添加视频文件,默认是不可以访问的,我需要去核审视频并再开放权限,允许这个资源访问。