Implement files changelog

This commit is contained in:
Kijin Sung 2019-09-14 00:22:24 +09:00
parent 2cd2d0528b
commit 941542b991
6 changed files with 107 additions and 17 deletions

View file

@ -16,16 +16,10 @@ class file extends ModuleObject
// Register action forward (to use in administrator mode)
$oModuleController = getController('module');
// Save the default settings for attachments
$config = new stdClass;
$config->allowed_filesize = '2';
$config->allowed_attach_size = '2';
$config->allowed_filetypes = '*.*';
$config->allowed_extensions = array();
$oModuleController->insertModuleConfig('file', $config);
// Generate a directory for the file module
FileHandler::makeDir('./files/attach/images');
FileHandler::makeDir('./files/attach/binaries');
// 2007. 10. 17 Create a trigger to insert, update, delete documents and comments
$oModuleController->insertTrigger('document.insertDocument', 'file', 'controller', 'triggerCheckAttached', 'before');
$oModuleController->insertTrigger('document.insertDocument', 'file', 'controller', 'triggerAttachFiles', 'after');

View file

@ -889,7 +889,6 @@ class fileController extends file
// Check file type
if(!$manual_insert && !$this->user->isAdmin())
{
// Check file type
if(isset($config->allowed_extensions) && count($config->allowed_extensions))
{
@ -1015,9 +1014,33 @@ class fileController extends file
$args->member_srl = $member_srl;
$args->regdate = $file_regdate;
$args->sid = Rhymix\Framework\Security::getRandom(32, 'hex');
$oDB = DB::getInstance();
$oDB->begin();
$output = executeQuery('file.insertFile', $args);
if(!$output->toBool()) return $output;
if(!$output->toBool())
{
$oDB->rollback();
return $output;
}
if($config->save_changelog === 'Y')
{
$clargs = new stdClass;
$clargs->change_type = 'I';
$clargs->file_srl = $args->file_srl;
$clargs->file_size = $args->file_size;
$clargs->uploaded_filename = $args->uploaded_filename;
$clargs->regdate = $args->regdate;
$output = executeQuery('file.insertFileChangelog', $clargs);
if(!$output->toBool())
{
$oDB->rollback();
return $output;
}
}
$oDB->commit();
// Call a trigger (after)
ModuleHandler::triggerCall('file.insertFile', 'after', $args);
@ -1226,6 +1249,10 @@ class fileController extends file
return new BaseObject();
}
$config = getModel('file')->getFileConfig();
$oDB = DB::getInstance();
$oDB->begin();
foreach($file_list as $file)
{
if(!is_object($file))
@ -1248,7 +1275,26 @@ class fileController extends file
// Remove from the DB
$output = executeQuery('file.deleteFile', $file);
if(!$output->toBool()) return $output;
if(!$output->toBool())
{
$oDB->rollback();
return $output;
}
if($config->save_changelog === 'Y')
{
$clargs = new stdClass;
$clargs->change_type = 'D';
$clargs->file_srl = $file->file_srl;
$clargs->file_size = $file->file_size;
$clargs->uploaded_filename = $file->uploaded_filename;
$output = executeQuery('file.insertFileChangelog', $clargs);
if(!$output->toBool())
{
$oDB->rollback();
return $output;
}
}
// If successfully deleted, remove the file
Rhymix\Framework\Storage::delete(FileHandler::getRealPath($file->uploaded_filename));
@ -1260,6 +1306,7 @@ class fileController extends file
Rhymix\Framework\Storage::deleteEmptyDirectory(dirname(FileHandler::getRealPath($file->uploaded_filename)), true);
}
$oDB->commit();
return new BaseObject();
}
@ -1312,7 +1359,7 @@ class fileController extends file
* @param int $source_srl Sequence of target to move
* @param int $target_module_srl New squence of module
* @param int $target_srl New sequence of target
* @return void
* @return object
*/
function moveFile($source_srl, $target_module_srl, $target_srl)
{
@ -1324,11 +1371,14 @@ class fileController extends file
$file_count = count($file_list);
for($i=0;$i<$file_count;$i++)
$config = $oFileModel->getFileConfig($module_srl);
$oDB = DB::getInstance();
$oDB->begin();
foreach($file_list as $i => $file_info)
{
unset($file_info);
$file_info = $file_list[$i];
$old_file = $file_info->uploaded_filename;
// Determine the file path by checking if the file is an image or other kinds
if (Rhymix\Framework\Filters\FilenameFilter::isDirectDownload($file_info->source_filename))
{
@ -1343,22 +1393,46 @@ class fileController extends file
$random_filename = basename($file_info->uploaded_filename) ?: Rhymix\Framework\Security::getRandom(32, 'hex');
$new_file = $path . $random_filename;
}
// Pass if a target document to move is same
if($old_file === $new_file) continue;
// Create a directory
FileHandler::makeDir($path);
// Move the file
FileHandler::rename($old_file, $new_file);
// Delete old path
Rhymix\Framework\Storage::deleteEmptyDirectory(dirname(FileHandler::getRealPath($old_file)), true);
// Update DB information
$args = new stdClass;
$args->file_srl = $file_info->file_srl;
$args->uploaded_filename = $new_file;
$args->module_srl = $file_info->module_srl;
$args->upload_target_srl = $target_srl;
executeQuery('file.updateFile', $args);
$output = executeQuery('file.updateFile', $args);
if($config->save_changelog === 'Y')
{
$clargs = new stdClass;
$clargs->change_type = 'M';
$clargs->file_srl = $file_info->file_srl;
$clargs->file_size = $file_info->file_size;
$clargs->uploaded_filename = $new_file;
$clargs->previous_filename = $old_file;
$output = executeQuery('file.insertFileChangelog', $clargs);
if(!$output->toBool())
{
$oDB->rollback();
return $output;
}
}
}
$oDB->commit();
return new BaseObject();
}
function copyFile($source_file, $module_srl, $upload_target_srl, &$content = null)

View file

@ -229,6 +229,7 @@ class fileModel extends file
if(!$config->image_autorotate) $config->image_autorotate = $file_module_config->image_autorotate;
if(!$config->image_autorotate_quality) $config->image_autorotate_quality = $file_module_config->image_autorotate_quality;
if(!$config->ffmpeg_command) $config->ffmpeg_command = $file_module_config->ffmpeg_command;
if(!$config->save_changelog) $config->save_changelog = $file_module_config->save_changelog;
// Default setting if not exists
if(!$config->allowed_filesize) $config->allowed_filesize = '2';

View file

@ -0,0 +1,13 @@
<query id="insertFileChangelog" action="insert">
<tables>
<table name="files_changelog" />
</tables>
<columns>
<column name="change_type" var="change_type" notnull="notnull" />
<column name="file_srl" var="file_srl" filter="number" notnull="notnull" />
<column name="file_size" var="file_size" filter="number" notnull="notnull" />
<column name="uploaded_filename" var="uploaded_filename" notnull="notnull" />
<column name="previous_filename" var="previous_filename" />
<column name="regdate" var="regdate" default="curdate()" />
</columns>
</query>

View file

@ -0,0 +1,9 @@
<table name="files_changelog">
<column name="id" type="number" primary_key="primary_key" auto_increment="auto_increment" />
<column name="change_type" type="char" size="1" notnull="notnull" index="idx_change_type" />
<column name="file_srl" type="number" notnull="notnull" index="idx_file_srl" />
<column name="file_size" type="number" notnull="notnull" />
<column name="uploaded_filename" type="varchar" size="250" notnull="notnull" />
<column name="previous_filename" type="varchar" size="250" />
<column name="regdate" type="date" notnull="notnull" index="idx_regdate" />
</table>

View file

@ -21,5 +21,4 @@
<button type="submit" class="x_btn x_btn-primary">{$lang->cmd_save}</button>
</div>
</div>
{@ var_dump($config)}
</form>