Sync server v2

This commit is contained in:
2019-10-07 18:01:03 +02:00
parent 76346ecce8
commit b40d3f7783
2 changed files with 76 additions and 37 deletions

View File

@@ -20,7 +20,7 @@ class CATC extends Main
array('name'=>'doc', 'project'=>true),
array('name'=>'definition', 'project'=>true)
);
parent::__construct($oClassManagement, $sProcessPage, $asClasses);
parent::__construct($oClassManagement, $sProcessPage, $asClasses, true , __FILE__);
//if($this->oDb->sDbState == Db::DB_PEACHY) $this->oAuth = new Auth($this->oDb, Settings::API_KEY);
$this->oAuth = new Auth($this->oDb, Settings::API_KEY);
@@ -199,38 +199,69 @@ class CATC extends Main
if($sBackup === false) $sDesc = 'Error executing mysqldump';
else {
//Store backup as a zip file
$sBackupPath = Doc::DOC_FOLDER.'db/'.uniqid('backup_').'.sql.gz';
$sBackupFile = uniqid('backup_').'.sql.gz';
$sBackupPath = Doc::DOC_FOLDER.'db/'.$sBackupFile;
file_put_contents($sBackupPath, gzencode($sBackup, self::GZ_LVL));
//Send backup
$sResult = self::sendFileToServer('server_update', $sBackupPath);
//Parse response from server
$asResult = json_decode($sResult, true);
$bSuccess = ($asResult['result'] == self::SUCCESS);
$sDesc = $asResult['desc'];
$asResult = self::sendFilesToServer('server_update', array('filepath'=>$sBackupPath, 'filename'=>$sBackupFile));
unlink($sBackupPath);
if($asResult['result']) {
$asData = $asResult['content'];
$bSuccess = ($asData['result'] == self::SUCCESS);
$sDesc = $asData['desc'];
//Send missing files
$asFiles = $asResult['data']['files'];
foreach($asFiles as $asFile) {
$this->sendFileToServer('file_update', $asFile['filepath'], $asFile['filename']);
if($bSuccess && isset($asData['data']['files']) && !empty($asData['data']['files'])) {
$asResult = $this->sendFilesToServer('file_update', $asData['data']['files']);
if($asResult['result']) {
$asData = $asResult['content'];
$bSuccess = ($asData['result'] == self::SUCCESS);
$sDesc = $asData['desc'];
}
else $sDesc = $asResult['desc'];
}
else $this->addNotice('No missing files identified');
}
else $sDesc = $asResult['desc'];
}
}
return self::getJsonResult($bSuccess, $sDesc);
}
private static function sendFilesToServer($sAction, $asFiles) {
if(is_string($asFiles)) $asFiles = array(array('filepath'=>$asFiles, 'filename'=>'file'));
elseif(array_key_exists('filepath', $asFiles)) $asFiles = array($asFiles);
$asPostData = array('a'=>$sAction, 'api'=>Settings::SERVER_KEY);
$asMoves = array();
foreach($asFiles as $iIndex=>$asFile) {
$asPathInfo = pathinfo($asFile['filepath']);
$asMove = array('origin'=>$asFile['filepath'], 'safe'=>$asPathInfo['dirname'].'/'.uniqid('file_').'.'.$asPathInfo['extension']);
rename($asMove['origin'], $asMove['safe']);
$asPostData['files['.$iIndex.']'] = new CURLFile(realpath($asMove['safe']), mime_content_type($asMove['safe']), $asFile['filename']);
$asMoves[] = $asMove;
}
$asResult = ToolBox::curl(Settings::SERVER_URL, false, $asPostData, 'json');
foreach($asMoves as $asMove) rename($asMove['safe'], $asMove['origin']);
return $asResult;
}
public function updateServer() {
$bSuccess = false;
$sDesc = '';
$asMissingFiles = array();
//Replace DB
$sBackupPath = Doc::DOC_FOLDER.'db/'.uniqid('backup_').'.sql';
$sCompressedPath = $sBackupPath.'.gz';
move_uploaded_file($_FILES['file']['tmp_name'], $sCompressedPath);
$asFilePaths = $this->updateFiles(Doc::DOC_FOLDER.'db/', true);
if(empty($asFilePaths)) $sDesc = 'Missing db file on remote server';
else {
$sCompressedPath = array_shift($asFilePaths);
$sBackupPath = str_replace('.gz', '', $sCompressedPath);
file_put_contents($sBackupPath, gzdecode(file_get_contents($sCompressedPath)));
unlink($sCompressedPath);
$sDesc = $this->oDb->restoreBackup($sBackupPath);
@@ -244,21 +275,29 @@ class CATC extends Main
//Check for missing files
$asMissingFiles = (new Doc($this->oDb))->getMissingFiles();
}
}
//Send list of missing files back
return self::getJsonResult($bSuccess, $sDesc, array('files'=>$asMissingFiles));
}
public function updateFile($sPath='') {
if($sPath=='') $sPath = Doc::DOC_FOLDER.$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $sPath);
public function updateFiles($sFolder=Doc::DOC_FOLDER, $bInternal=false) {
$bSuccess = false;
$sDesc = '';
$asFilePaths = array();
if(!empty($_FILES)) {
$asFiles = $_FILES['files'];
for($iIndex=0 ; $iIndex < count($asFiles['name']) ; $iIndex++) {
$sFilePath = $sFolder.$asFiles['name'][$iIndex];
move_uploaded_file($asFiles['tmp_name'][$iIndex], $sFilePath);
$asFilePaths[] = $sFilePath;
//TODO Check data integrity
}
$bSuccess = true;
}
else $sDesc = 'No file to update';
private static function sendFileToServer($sAction, $sFilePath, $sFileName='file') {
$asPostData = array('a'=>$sAction, 'api'=>Settings::SERVER_KEY);
$asPostData['file'] = new CURLFile(realpath($sFilePath), mime_content_type($sFilePath), $sFileName);
return ToolBox::curl(Settings::SERVER_URL, false, $asPostData);
return $bInternal?$asFilePaths:self::getJsonResult($bSuccess, $sDesc);
}
}

View File

@@ -88,7 +88,7 @@ elseif($sAction!='' && !$bLoggedIn)
$sResult = $oCATC->updateServer();
break;
case 'file_update':
$sResult = $oCATC->updateFile();
$sResult = $oCATC->updateFiles();
break;
default:
$sResult = CATC::getJsonResult(false, CATC::NOT_FOUND);