本系列文章是針對 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方法中如果是作者本人再向視頻資源中添加視頻文件,那麼默認是直接允許訪問的,如果是他人向我的視頻資源中添加視頻文件,默認是不可以訪問的,我需要去核審視頻並再開放權限,允許這個資源訪問。