文章目录 成员变量 成员函数 backUp getFileIdentifier isNeedUpload upLoad RunMoudle
成员变量
private : std:: string _back_dir; dataManager* _data;
成员函数
backUp
backUp ( const std:: string& back_dir, const std:: string& back_file) : _back_dir ( back_dir)
{ _data = new dataManager ( back_file) ; fileUtil fu ( _back_dir) ; if ( fu. exists ( ) == false ) fu. createDirectory ( ) ;
}
getFileIdentifier
std:: string getFileIdentifier ( const std:: string& filePath)
{ fileUtil fu ( filePath) ; if ( fu. exists ( ) == false ) { std:: cout << "getFileIdentifier fail not exists" << std:: endl; return "" ; } std:: stringstream s; s << fu. fileName ( ) << "-" ; s << std:: to_string ( fu. fileSize ( ) ) << "-" ; s << std:: to_string ( fu. lastModifyTime ( ) ) ; return s. str ( ) ;
}
isNeedUpload
bool isNeedUpload ( const std:: string& filePath)
{ std:: string old_id; bool ret = _data-> getUniqueIdentifierByFilePath ( filePath , & old_id) ; if ( ret != false ) { std:: string new_id = getFileIdentifier ( filePath) ; if ( new_id == old_id) return false ; } fileUtil fu ( filePath) ; if ( time ( nullptr ) - fu. lastModifyTime ( ) <= 3 ) { return false ; } return true ;
}
upLoad
bool upLoad ( const std:: string& filePath)
{ std:: string body; fileUtil fu ( filePath) ; fu. getContent ( & body) ; httplib:: Client client ( SERVER_IP, SERVER_PORT) ; httplib:: MultipartFormData item; item. name = "file" ; item. filename = fu. fileName ( ) ; item. content = body; item. content_type = "application/octet-stream" ; httplib:: MultipartFormDataItems items; items. push_back ( item) ; auto ret = client. Post ( "/upload" , items) ; if ( ! ret && ret-> status != 200 ) { return false ; } return true ;
}
RunMoudle
bool RunMoudle ( )
{ while ( true ) { fileUtil fu ( _back_dir) ; std:: vector< std:: string> arry; fu. getDirectory ( & arry) ; for ( auto & e : arry) { if ( isNeedUpload ( e) == false ) { continue ; } std:: string id = getFileIdentifier ( e) ; if ( id. empty ( ) ) continue ; if ( upLoad ( e) == true ) { _data-> insert ( e, id) ; } } Sleep ( 1 ) ; }
}