getCode() . "|" . $e->getLine() . ": " . $e->getMessage()); } 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 = $this->getRootStoragePath(); $sqlLogin = User::getLogin(); $sqlLabel = DB::getPDO()->quote($sqlLabel, PDO::PARAM_STR); $sqlTblName = $this->getTableName(); $sql = " 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__); DB::getPDO()->exec($sql); $dbLastId = DB::getPDO()->lastInsertId(); DBG::_('DBG', '>1', "dbLastId", $dbLastId, __CLASS__, __FUNCTION__, __LINE__); $filePath = $this->generateFilePathHashFromID($dbLastId); DBG::_('DBG', '>1', "filePath", $filePath, __CLASS__, __FUNCTION__, __LINE__); $absFilePath = "{$rootFileStoragePath}/{$filePath}"; $dirPath = dirname($absFilePath); @mkdir($dirPath, $mode = 0777, $recursive = true); if (!file_exists($dirPath)) throw new Exception("Cannot create path"); @chmod($dirPath, $mode = 0777); $fp = fopen($absFilePath, 'w'); fwrite($fp, $fileContent); fclose($fp); if (!file_exists($dirPath)) throw new Exception("Cannot save file"); } public function uploadAction() { try { $fileContent = Request::getRequestBody(); $sqlLabel = V::get('name', '', $_REQUEST); $this->addFile($fileContent, $sqlLabel); echo 'file uploaded'; } catch (Exception $e) { echo "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage(); } } public function uploadFromBinaryTestAction() { try { $fileContent = Request::getRequestBody(); $filePath = $this->getRootStoragePath() . '/test-upload-data.txt'; $fp = fopen($filePath, 'w'); fwrite($fp, $fileContent); fclose($fp); echo 'file uploaded?'; } catch (Exception $e) { echo "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage(); } } public function downloadTestAction() { try { $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'); header('Content-Disposition: attachment; filename="'.basename($filePath).'"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filePath)); readfile($filePath); exit; } catch (Exception $e) { echo "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage(); } } public function uploadFromFormTestAction() { try { // $fileContent = Request::getRequestBody(); DBG::_(true, true, '_POST', $_POST, __CLASS__, __FUNCTION__, __LINE__); // $filePath = $this->getRootStoragePath() . '/test-upload-data.txt'; // $fp = fopen($filePath, 'w'); // fwrite($fp, $fileContent); // fclose($fp); // echo 'file uploaded?'; } catch (Exception $e) { echo "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage(); } } public function uploadFormTestAction() { UI::gora(); UI::menu(); try { // multiple: // only images: ?>
download

root storage path: getRootStoragePath(); ?>

table name: getTableName(); ?>

getCode() . "|" . $e->getLine() . ": " . $e->getMessage()); } UI::dol(); } public function generateFilePathHashFromID($intId) { // $base36Id = base_convert($intId, 10, 10 + 26);// 0-9 + A-Z $base36Id = base_convert($intId, 10, 10 + 6);// 0-9 + A-F $base36Str = str_pad($base36Id, 10, "0", STR_PAD_LEFT); $base36Str = strrev($base36Str); $pathParts = array(); $pathParts[] = substr($base36Str, 0, 2); $pathParts[] = substr($base36Str, 2, 2); $pathParts[] = substr($base36Str, 4); $hashPath = implode("/", $pathParts); echo "ID($intId) converted to ({$base36Id})\t to ({$base36Str})\t to path ({$hashPath})\n"; return $hashPath; } public function generatePathTestAction() { try { $start = 0; $start = 446071; $limit = $start + 100; echo '
';
			for ($i = $start; $i < $limit; $i++) {
				$this->generateFilePathHashFromID($i);
			}
			echo '
'; } catch (Exception $e) { echo "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage(); } } 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 '', `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; "); {// 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()); } } }