Просмотр исходного кода

added reinstall to FileStorage and bash_install_check

Piotr Labudda 9 лет назад
Родитель
Сommit
9eaddc828e
2 измененных файлов с 83 добавлено и 10 удалено
  1. 6 0
      SE/bash_install_check.php
  2. 77 10
      SE/se-lib/Route/FileStorage.php

+ 6 - 0
SE/bash_install_check.php

@@ -67,6 +67,12 @@ function runUrlTask($url) {
 $string = runUrlTask("{$baseUrl}/index.php?_route=Cron&_key={$keyToken}&_token={$token}&_task={$cronTaskName}}");
 echo "DBG: string -----------------------\n{$string}\nEOF string---------------------------------\n";
 
+{
+	echo "Router::getRoute('FileStorage')->reinstall() ...\n";
+	Router::getRoute('FileStorage')->reinstall();
+	echo "\nRouter::getRoute('FileStorage')->reinstall() DONE\n";
+}
+
 die(".EOF - OK\n");
 
 ?>

+ 77 - 10
SE/se-lib/Route/FileStorage.php

@@ -39,13 +39,29 @@ class Route_FileStorage extends RouteBase {
 		UI::dol();
 	}
 
+	public function isProduction() {// TODO: mv to Config
+		// return V::get('P5_ENV', '/Library/Server/Web/Data/p5-file-storage', $_SERVER);
+		// $env = getenv('P5_ENV');
+		// if (empty($env)) $env = 'production';
+		return ('production' == V::get('P5_ENV', 'production', $_SERVER));
+	}
+
+	public function getRootStoragePath() {
+		return ($this->isProduction()) ? '/Library/Server/Web/Data/p5-file-storage' : '/tmp/test-upload-file-storage';
+	}
+
+	public function getTableName() {
+		return ($this->isProduction()) ? 'CRM_FILES' : 'CRM_FILES__#DEV';
+	}
+
 	public function addFile($content, $name = '') {
-		$rootFileStoragePath = '/tmp/test-upload-file-storage';// TODO: from config!
+		$rootFileStoragePath = $this->getRootStoragePath();
 
 		$sqlLogin = User::getLogin();
 		$sqlLabel = DB::getPDO()->quote($sqlLabel, PDO::PARAM_STR);
+		$sqlTblName = $this->getTableName();
 		$sql = "
-			insert into CRM_FILES (`A_RECORD_CREATE_AUTHOR`,`A_RECORD_CREATE_DATE`,`FILE_LABEL`)
+			insert into {$sqlTblName} (`A_RECORD_CREATE_AUTHOR`,`A_RECORD_CREATE_DATE`,`FILE_LABEL`)
 			values ('{$sqlLogin}', NOW(), {$sqlLabel})
 		";
 		DBG::_('DBG', '>2', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
@@ -82,7 +98,7 @@ class Route_FileStorage extends RouteBase {
 	public function uploadFromBinaryTestAction() {
 		try {
 			$fileContent = Request::getRequestBody();
-			$filePath = '/tmp/test-upload-data.txt';
+			$filePath = $this->getRootStoragePath() . '/test-upload-data.txt';
 			$fp = fopen($filePath, 'w');
 			fwrite($fp, $fileContent);
 			fclose($fp);
@@ -95,7 +111,7 @@ class Route_FileStorage extends RouteBase {
 
 	public function downloadTestAction() {
 		try {
-			$filePath = '/tmp/test-upload-data.txt';
+			$filePath = $this->getRootStoragePath() . '/test-upload-data.txt';
 			if (!file_exists($filePath)) throw new Exception("file not exists!");
 	    header('Content-Description: File Transfer');
 	    header('Content-Type: application/octet-stream');
@@ -116,7 +132,7 @@ class Route_FileStorage extends RouteBase {
 			// $fileContent = Request::getRequestBody();
 			DBG::_(true, true, '_POST', $_POST, __CLASS__, __FUNCTION__, __LINE__);
 
-			// $filePath = '/tmp/test-upload-data.txt';
+			// $filePath = $this->getRootStoragePath() . '/test-upload-data.txt';
 			// $fp = fopen($filePath, 'w');
 			// fwrite($fp, $fileContent);
 			// fclose($fp);
@@ -141,6 +157,10 @@ class Route_FileStorage extends RouteBase {
 	<button class="btn btn-primary" id="upload_file_as_binary_btn">upload as binary</button>
 	<button class="btn btn-default" id="upload_file_as_form_btn">upload as form</button>
 	<a class="btn btn-default" href="index.php?_route=FileStorage&_task=downloadTest" target="_blank">download</a>
+	<blockquote>
+		<p>root storage path: <code><?php echo $this->getRootStoragePath(); ?></code></p>
+		<p>table name: <code><?php echo $this->getTableName(); ?></code></p>
+	</blockquote>
 </div>
 <script>
 document.getElementById('file_input').addEventListener('change', function() {
@@ -259,16 +279,42 @@ jQuery('#upload_file_as_form_btn').on('click', function() {
 		}
 	}
 
-}
-
-/*
+	public function reinstallAction() {
+		UI::gora();
+		UI::menu();
+		$this->reinstall();
+		UI::dol();
+	}
 
+	public function reinstall() {
+		try {
+			DB::getPDO()->exec("
 CREATE TABLE IF NOT EXISTS `CRM_FILES` (
   `ID` int(11) NOT NULL AUTO_INCREMENT,
   `FILE_HASH` varchar(255) NOT NULL, -- generated from ID by hash function - only for cache
   `FILE_LABEL` varchar(255) NOT NULL, -- original file name or system name
   `FILE_TYPE` varchar(32) NOT NULL DEFAULT '', -- $TRG_FILE -> config/.cnf--folders...
   `FILE_MTIME` datetime NOT NULL,
+  `FILE_VERSION` int(11) NOT NULL DEFAULT 0, -- used for update
+  `A_STATUS` enum('WAITING','NORMAL','MONITOR','OFF_HARD','OFF_SOFT','DELETED') NOT NULL DEFAULT 'WAITING',
+  `A_RECORD_CREATE_DATE` datetime DEFAULT NULL,
+  `A_RECORD_CREATE_AUTHOR` varchar(40) NOT NULL DEFAULT '',
+  `A_RECORD_UPDATE_DATE` datetime DEFAULT NULL,
+  `A_RECORD_UPDATE_AUTHOR` varchar(40) NOT NULL DEFAULT '',
+	`A_ADM_COMPANY` varchar(100) NOT NULL DEFAULT '',
+	`A_CLASSIFIED` varchar(100) NOT NULL DEFAULT '',
+  PRIMARY KEY (`ID`),
+  KEY `FILE_TYPE` (`FILE_TYPE`)
+) ENGINE=MyISAM  DEFAULT CHARSET=latin2;
+			");
+			DB::getPDO()->exec("
+CREATE TABLE IF NOT EXISTS `CRM_FILES__#DEV` (
+  `ID` int(11) NOT NULL AUTO_INCREMENT,
+  `FILE_HASH` varchar(255) NOT NULL, -- generated from ID by hash function - only for cache
+  `FILE_LABEL` varchar(255) NOT NULL, -- original file name or system name
+  `FILE_TYPE` varchar(32) NOT NULL DEFAULT '', -- $TRG_FILE -> config/.cnf--folders...
+  `FILE_MTIME` datetime NOT NULL,
+  `FILE_VERSION` int(11) NOT NULL DEFAULT 0, -- used for update
   `A_STATUS` enum('WAITING','NORMAL','MONITOR','OFF_HARD','OFF_SOFT','DELETED') NOT NULL DEFAULT 'WAITING',
   `A_RECORD_CREATE_DATE` datetime DEFAULT NULL,
   `A_RECORD_CREATE_AUTHOR` varchar(40) NOT NULL DEFAULT '',
@@ -279,7 +325,28 @@ CREATE TABLE IF NOT EXISTS `CRM_FILES` (
   PRIMARY KEY (`ID`),
   KEY `FILE_TYPE` (`FILE_TYPE`)
 ) ENGINE=MyISAM  DEFAULT CHARSET=latin2;
+			");
 
-2016-07-20: 446071 files in /Library/Server/Web/Data/Sites/Default/PLIKI
+			{// TODO: only in cli mode - require root perms
+				$devRootPath = '/tmp/test-upload-file-storage';
+				if (!file_exists($devRootPath)) {
+					@mkdir($devRootPath, $mode = 0777, $recursive = true);
+					if (!file_exists($devRootPath)) throw new Exception("Cannot create FileStorage root path for dev");
+					@chmod($devRootPath, $mode = 0777);
+					@chown($devRootPath, $user = '_www');
+				}
 
-*/
+				$productionRootPath = '/Library/Server/Web/Data/p5-file-storage';
+				if (!file_exists($productionRootPath)) {
+					@mkdir($productionRootPath, $mode = 0775, $recursive = true);
+					if (!file_exists($productionRootPath)) throw new Exception("Cannot create FileStorage root path");
+					@chmod($productionRootPath, $mode = 0775);
+					@chown($productionRootPath, $user = '_www');
+				}
+			}
+		} catch (Exception $e) {
+			UI::alert('danger', $e->getMessage());
+		}
+	}
+
+}