|
|
@@ -20,7 +20,8 @@ Lib::loadClass('Http');
|
|
|
- [x] use streams for upload - better memory usage
|
|
|
- [ ] use streams for upload progress in form - http://www.webiny.com/blog/2012/05/07/webiny-file-upload-with-html5-and-ajax-using-php-streams/
|
|
|
- [x] add IP to table struct
|
|
|
-- [ ] WebDav - use only files from this table
|
|
|
+- [ ] WebDav - use only files from this table @see WebDav on Mac OS X https://www.qnap.com/pl-pl/tutorial/con_show.php?op=showone&cid=75
|
|
|
+- [ ] Update file - add {tblName}__#VERSIONS table (work like _HIST, but only on update file content)
|
|
|
*/
|
|
|
class FileStorage {
|
|
|
|
|
|
@@ -39,7 +40,7 @@ class FileStorage {
|
|
|
return (self::isProduction()) ? 'CRM_FILES' : 'CRM_FILES__#DEV';
|
|
|
}
|
|
|
|
|
|
- public static function getFileById($idFile) {
|
|
|
+ public static function getFileById($idFile, $forceVersion = null) {
|
|
|
$fileObject = array();// @returns array
|
|
|
$fileObject['_raw'] = null;// raw object from database
|
|
|
$fileObject['id'] = (int)$idFile;
|
|
|
@@ -49,11 +50,13 @@ class FileStorage {
|
|
|
$fileObject['exists'] = null;
|
|
|
$fileObject['size'] = 0;
|
|
|
$fileObject['mimeType'] = null;
|
|
|
- $rowFile = self::_getRowFile($idFile);
|
|
|
+ $fileObject['version'] = 0;
|
|
|
+ $rowFile = self::_getRowFile($idFile, $forceVersion);
|
|
|
$fileObject['_raw'] = $rowFile;
|
|
|
// TODO: check perms
|
|
|
$fileObject['name'] = V::get('FILE_LABEL', $id, $rowFile);
|
|
|
- $fileObject['relativePath'] = self::generateFilePathHashFromId($rowFile['ID']);
|
|
|
+ $fileObject['version'] = ($forceVersion) ? $forceVersion : V::get('FILE_VERSION', 0, $rowFile);
|
|
|
+ $fileObject['relativePath'] = self::generateFilePathHashFromId($rowFile['ID'], $version);
|
|
|
$fileObject['absolutePath'] = self::getRootStoragePath() . "/" . $fileObject['relativePath'];
|
|
|
$fileObject['exists'] = file_exists($fileObject['absolutePath']);
|
|
|
if ($fileObject['exists']) {
|
|
|
@@ -118,6 +121,18 @@ class FileStorage {
|
|
|
return $idFile;
|
|
|
}
|
|
|
|
|
|
+ public static function updateFileStream($idFile, $inputHandler, $name = '') {
|
|
|
+ throw new Exception("TODO: insert into `\{\$tblName\}__#VERSIONS` for file `{$idFile}`");
|
|
|
+ $fileObject = self::getFileById($idFile);
|
|
|
+ if (!$fileObject['exists']) throw new Exception("Oryginalny plik nie istnieje");
|
|
|
+ // TODO: check perms
|
|
|
+ $fileVersion = self::_createUpdateRowFile($idFile, $name);// insert into `{$tblName}__#VERSIONS` ( `FILE_VERSION` = 1 + (select MAX(`FILE_VERSION`) from #VER) )
|
|
|
+ {// upload by stream
|
|
|
+
|
|
|
+ }
|
|
|
+ self::_updateRowFileFromVersion($fileVersion);// update `{$tblName}` values from `{$tblName}__#VERSIONS`
|
|
|
+ }
|
|
|
+
|
|
|
public static function _createRowFile($name = '') {
|
|
|
$sqlLogin = User::getLogin();
|
|
|
$sqlLabel = DB::getPDO()->quote($name, PDO::PARAM_STR);
|
|
|
@@ -132,7 +147,7 @@ class FileStorage {
|
|
|
return DB::getPDO()->lastInsertId();
|
|
|
}
|
|
|
|
|
|
- public static function _getRowFile($idFile) {
|
|
|
+ public static function _getRowFile($idFile, $version = null) {
|
|
|
$idFile = intval($idFile);
|
|
|
if ($idFile <= 0) throw new HttpException("#L" . __LINE__ . ": Wrong file id", 500);
|
|
|
$sqlId = DB::getPDO()->quote($idFile, PDO::PARAM_INT);
|
|
|
@@ -144,7 +159,9 @@ class FileStorage {
|
|
|
");
|
|
|
DBG::_('DBG', '>2', "rows", $rows, __CLASS__, __FUNCTION__, __LINE__);
|
|
|
if (empty($rows)) throw new HttpException("Plik nie istnieje w bazie", 404);
|
|
|
- return $rows[0];
|
|
|
+ $rowFile = $rows[0];
|
|
|
+ // TODO: fetch info by $version
|
|
|
+ return $rowFile;
|
|
|
}
|
|
|
|
|
|
public static function _uploadUpdateMimeType($idFile, $absPath) {
|
|
|
@@ -198,7 +215,7 @@ class FileStorage {
|
|
|
");
|
|
|
}
|
|
|
|
|
|
- public static function generateFilePathHashFromId($intId) {
|
|
|
+ public static function generateFilePathHashFromId($intId, $version = null) {
|
|
|
// $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);
|
|
|
@@ -209,6 +226,7 @@ class FileStorage {
|
|
|
$pathParts[] = substr($base36Str, 4);
|
|
|
$hashPath = implode("/", $pathParts);
|
|
|
// echo "ID($intId) converted to ({$base36Id})\t to ({$base36Str})\t to path ({$hashPath})\n";
|
|
|
+ // TODO: add $version to file name: .= "-v{$version}"
|
|
|
return $hashPath;
|
|
|
}
|
|
|
|
|
|
@@ -233,7 +251,7 @@ CREATE TABLE IF NOT EXISTS `CRM_FILES` (
|
|
|
`A_CLASSIFIED` varchar(100) NOT NULL DEFAULT '',
|
|
|
`A_USER_IP` bigint NOT NULL DEFAULT '0',
|
|
|
PRIMARY KEY (`ID`),
|
|
|
- KEY `FILE_TYPE` (`FILE_MIME_TYPE`)
|
|
|
+ KEY `FILE_TYPE` (`FILE_TYPE`)
|
|
|
) ENGINE=MyISAM DEFAULT CHARSET=latin2;
|
|
|
");
|
|
|
DB::getPDO()->exec("
|
|
|
@@ -255,7 +273,39 @@ CREATE TABLE IF NOT EXISTS `CRM_FILES__#DEV` (
|
|
|
`A_CLASSIFIED` varchar(100) NOT NULL DEFAULT '',
|
|
|
`A_USER_IP` bigint NOT NULL DEFAULT '0',
|
|
|
PRIMARY KEY (`ID`),
|
|
|
- KEY `FILE_TYPE` (`FILE_MIME_TYPE`)
|
|
|
+ KEY `FILE_TYPE` (`FILE_TYPE`)
|
|
|
+) ENGINE=MyISAM DEFAULT CHARSET=latin2;
|
|
|
+ ");
|
|
|
+ DB::getPDO()->exec("
|
|
|
+CREATE TABLE IF NOT EXISTS `CRM_FILES__#VERSIONS` (
|
|
|
+ `ID_FILE` int(11) NOT NULL,
|
|
|
+ `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_MIME_TYPE` varchar(64) NOT NULL DEFAULT '',
|
|
|
+ `FILE_MTIME` datetime NOT NULL,
|
|
|
+ `FILE_SIZE` bigint NOT NULL DEFAULT 0,
|
|
|
+ `FILE_VERSION` int(11) NOT NULL DEFAULT 0, -- used for update
|
|
|
+ `A_RECORD_CREATE_DATE` datetime DEFAULT NULL,
|
|
|
+ `A_RECORD_CREATE_AUTHOR` varchar(20) NOT NULL DEFAULT '',
|
|
|
+ `A_USER_IP` bigint NOT NULL DEFAULT '0',
|
|
|
+ KEY `ID_FILE` (`ID_FILE`)
|
|
|
+) ENGINE=MyISAM DEFAULT CHARSET=latin2;
|
|
|
+ ");
|
|
|
+ DB::getPDO()->exec("
|
|
|
+CREATE TABLE IF NOT EXISTS `CRM_FILES__#DEV__#VERSIONS` (
|
|
|
+ `ID_FILE` int(11) NOT NULL,
|
|
|
+ `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_MIME_TYPE` varchar(64) NOT NULL DEFAULT '',
|
|
|
+ `FILE_MTIME` datetime NOT NULL,
|
|
|
+ `FILE_SIZE` bigint NOT NULL DEFAULT 0,
|
|
|
+ `FILE_VERSION` int(11) NOT NULL DEFAULT 0, -- used for update
|
|
|
+ `A_RECORD_CREATE_DATE` datetime DEFAULT NULL,
|
|
|
+ `A_RECORD_CREATE_AUTHOR` varchar(20) NOT NULL DEFAULT '',
|
|
|
+ `A_USER_IP` bigint NOT NULL DEFAULT '0',
|
|
|
+ KEY `ID_FILE` (`ID_FILE`)
|
|
|
) ENGINE=MyISAM DEFAULT CHARSET=latin2;
|
|
|
");
|
|
|
|