Skip to content

Commit

Permalink
Eliminate unnecessary AJAX calls to file.getFileList
Browse files Browse the repository at this point in the history
에디터 로딩 직후, 파일 업로드 직후, 파일 삭제 직후
첨부목록 갱신을 위해 file.getFileList를 호출하도록 되어 있는데,
첨부목록을 직전 요청의 응답과 함께 반환하도록 변경하여
불필요한 AJAX 요청이 발생하지 않도록 하고, 로딩 속도를 개선함.
  • Loading branch information
kijin committed Apr 16, 2024
1 parent 0dc7dc5 commit 1170238
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 44 deletions.
42 changes: 31 additions & 11 deletions common/js/plugins/jquery.fileupload/js/main.js
Expand Up @@ -205,15 +205,23 @@
}
catch(err) {}
}
if (typeof result.files !== 'undefined') {
$container.data('editorStatus', result);
} else {
$container.data('editorStatus', null);
}
} else if (result.message) {
$container.data('editorStatus', null);
alert(result.message);
return false;
} else {
$container.data('editorStatus', null);
alert(window.xe.msg_file_upload_error + " (Type 6)" + "<br>\n" + res.response().result);
return false;
}
},
fail: function(e, res) {
$container.data('editorStatus', null);
lastUploadTime = Date.now();
setTimeout(function() {
if (lastUploadTime < Date.now() - 800) {
Expand All @@ -228,7 +236,7 @@
},
stop: function() {
lastUploadTime = Date.now();
self.loadFilelist($container);
self.loadFilelist($container, true);
},
start: function() {
lastUploadTime = Date.now();
Expand Down Expand Up @@ -261,7 +269,7 @@
$container.data('xefu-instance', this);

// 파일 목록 불러오기
this.loadFilelist($container);
this.loadFilelist($container, true);

// 본문 삽입
data.settings.actSelectedInsertContent.on('click', function() {
Expand Down Expand Up @@ -415,28 +423,33 @@
file_srls.push(file_srl);
}

exec_json('file.procFileDelete', {'file_srls': file_srls.join(','), 'editor_sequence': data.editorSequence}, function() {
exec_json('file.procFileDelete', {'file_srls': file_srls.join(','), 'editor_sequence': data.editorSequence}, function(result) {
$.each(file_srls, function(idx, srl){
data.settings.fileList.find('ul').find('li[data-file-srl=' + srl + ']').remove();
});
var ckeditor = _getCkeInstance(data.editorSequence);
var regexp = new RegExp('<(img|audio|video) [^>]*data-file-srl="(' + file_srls.join('|') + ')"[^>]*>', 'g');
ckeditor.setData(ckeditor.getData().replace(regexp, ''));
self.loadFilelist($container);
if (result.error == 0 && typeof result.files !== 'undefined') {
$container.data('editorStatus', result);
} else {
$container.data('editorStatus', null);
}
self.loadFilelist($container, true);
});
},
/**
* 파일 목록 갱신
*/
loadFilelist: function($container) {
loadFilelist: function($container, useEditorStatus) {
var self = this;
var data = $container.data();
var obj = {};
obj.mid = window.current_mid;
obj.editor_sequence = data.editorSequence;
obj.allow_chunks = 'Y';

$.exec_json('file.getFileList', obj, function(res){
var refreshList = function(res) {
data.uploadTargetSrl = res.upload_target_srl;
if(editorRelKeys[data.editorSequence]) {
editorRelKeys[data.editorSequence].primary.value = res.upload_target_srl;
Expand Down Expand Up @@ -503,11 +516,18 @@
// 컨트롤, 리스트 표시
data.settings.controll.show()
data.settings.fileList.show();
}, function(data, xhr) {
if (xhr.status != 200) {
return false;
}
});
};

// Get file list from HTML data-editor-status attribute when initializing.
if (useEditorStatus && typeof data.editorStatus !== 'undefined' && data.editorStatus !== null) {
refreshList(data.editorStatus);
} else {
$.exec_json('file.getFileList', obj, refreshList, function(data, xhr) {
if (xhr.status != 200) {
return false;
}
});
}
},
setCover: function($container, selected_el) {
var data = $container.data();
Expand Down
5 changes: 4 additions & 1 deletion modules/editor/skins/ckeditor/file_upload.html
Expand Up @@ -2,7 +2,10 @@
<!--%load_js_plugin("jquery.finderSelect")-->
<!--%load_js_plugin("handlebars")-->
<load target="./lang" />
<div cond="$allow_fileupload" id="xefu-container-{$editor_sequence}" class="xefu-container xe-clearfix" data-editor-sequence="{$editor_sequence}">
<div cond="$allow_fileupload" id="xefu-container-{$editor_sequence}" class="xefu-container xe-clearfix"
data-editor-sequence="{$editor_sequence}"
data-editor-status="{escape(json_encode(FileModel::getInstance()->getFileList($editor_sequence)))}">

<!--// dropzone -->
<div class="xefu-dropzone">
<span class="xefu-btn fileinput-button xefu-act-selectfile">
Expand Down
34 changes: 34 additions & 0 deletions modules/file/file.controller.php
Expand Up @@ -170,6 +170,23 @@ function procFileUpload()
{
$this->add('download_url', FileModel::getDownloadUrl($output->get('file_srl'), $output->get('sid'), 0, $output->get('source_filename')));
}

// Add upload status (getFileList)
try
{
$file_list = FileModel::getInstance()->getFileList($editor_sequence);
foreach ($file_list as $key => $val)
{
if (!isset($this->variables[$key]))
{
$this->add($key, $val);
}
}
}
catch (Exception $e)
{
// pass
}
}

/**
Expand Down Expand Up @@ -608,6 +625,23 @@ function procFileDelete()
if(!FileModel::isDeletable($file_info)) continue;
$output = $this->deleteFile($file_srl);
}

// Add upload status (getFileList)
try
{
$file_list = FileModel::getInstance()->getFileList($editor_sequence);
foreach ($file_list as $key => $val)
{
if (!isset($this->variables[$key]))
{
$this->add($key, $val);
}
}
}
catch (Exception $e)
{
// pass
}
}

/**
Expand Down
80 changes: 48 additions & 32 deletions modules/file/file.model.php
Expand Up @@ -20,42 +20,53 @@ public function init()
* It is used when a file list of the upload_target_srl is requested for creating/updating a document.
* Attempt to replace with sever-side session if upload_target_srl is not yet determined
*
* @return void
* @return object
*/
public function getFileList()
public function getFileList($editor_sequence = null, $upload_target_srl = null, $upload_target_type = null)
{
$file_list = [];
$attached_size = 0;
$editor_sequence = intval(Context::get('editor_sequence'));
$upload_target_srl = $_SESSION['upload_info'][$editor_sequence]->upload_target_srl ?? 0;
$editor_sequence = $editor_sequence ?? intval(Context::get('editor_sequence'));
$upload_target_srl = $upload_target_srl ?? $_SESSION['upload_info'][$editor_sequence]->upload_target_srl ?? 0;

// Get uploaded files
if($upload_target_srl)
{
$oDocument = DocumentModel::getDocument($upload_target_srl);
if (!$upload_target_type || $upload_target_type === 'document')
{
$oDocument = DocumentModel::getDocument($upload_target_srl);
}
else
{
$oDocument = null;
}

// Check permissions of the comment
if(!$oDocument->isExists())
if(!$oDocument || !$oDocument->isExists())
{
$oComment = CommentModel::getComment($upload_target_srl);
if($oComment->isExists())
if (!$upload_target_type || $upload_target_type === 'comment')
{
if(!$oComment->isAccessible())
$oComment = CommentModel::getComment($upload_target_srl);
if($oComment->isExists())
{
throw new Rhymix\Framework\Exceptions\NotPermitted;
if(!$oComment->isAccessible())
{
throw new Rhymix\Framework\Exceptions\NotPermitted;
}
$oDocument = DocumentModel::getDocument($oComment->get('document_srl'));
}
$oDocument = DocumentModel::getDocument($oComment->get('document_srl'));
}
}

// Check permissions of the document
if($oDocument->isExists() && !$oDocument->isAccessible())
if($oDocument && $oDocument->isExists() && !$oDocument->isAccessible())
{
throw new Rhymix\Framework\Exceptions\NotPermitted;
}

// Check permissions of the module
if($module_srl = isset($oComment) ? $oComment->get('module_srl') : $oDocument->get('module_srl'))
$module_srl = isset($oComment) ? $oComment->get('module_srl') : ($oDocument ? $oDocument->get('module_srl') : 0);
if ($module_srl)
{
$module_info = ModuleModel::getModuleInfoByModuleSrl($module_srl);
if(empty($module_info->module_srl))
Expand Down Expand Up @@ -96,44 +107,49 @@ public function getFileList()
}
}

// Set output
$this->add('files', $file_list);
$this->add('attached_size', FileHandler::filesize($attached_size));
$this->add('editor_sequence', $editor_sequence);
$this->add('upload_target_srl', $upload_target_srl);
// Initialize return value
$result = new \stdClass;
$result->files = $file_list;
$result->editor_sequence = $editor_sequence;
$result->upload_target_srl = $upload_target_srl;
$result->upload_status = self::getUploadStatus($attached_size);

// Set upload config
$upload_config = self::getUploadConfig();
$result->attached_size = FileHandler::filesize($attached_size);
$result->left_size = max(0, ($upload_config->allowed_attach_size * 1024 * 1024) - $attached_size);
if($this->user->isAdmin())
{
$this->add('allowed_filesize', sprintf('%s (%s)', lang('common.unlimited'), lang('common.admin')));
$this->add('allowed_attach_size', sprintf('%s (%s)', lang('common.unlimited'), lang('common.admin')));
$this->add('allowed_extensions', []);
$result->allowed_filesize = sprintf('%s (%s)', lang('common.unlimited'), lang('common.admin'));
$result->allowed_attach_size = sprintf('%s (%s)', lang('common.unlimited'), lang('common.admin'));
$result->allowed_extensions = [];
}
else
{
$this->add('allowed_filesize', FileHandler::filesize($upload_config->allowed_filesize * 1024 * 1024));
$this->add('allowed_attach_size', FileHandler::filesize($upload_config->allowed_attach_size * 1024 * 1024));
$this->add('allowed_extensions', $upload_config->allowed_extensions);
$result->allowed_filesize = FileHandler::filesize($upload_config->allowed_filesize * 1024 * 1024);
$result->allowed_attach_size = FileHandler::filesize($upload_config->allowed_attach_size * 1024 * 1024);
$result->allowed_extensions = $upload_config->allowed_extensions;
}

if(!$this->user->isAdmin())
{
if (isset($_SESSION['upload_info'][$editor_sequence]->allowed_filesize))
{
$this->add('allowed_filesize', FileHandler::filesize($_SESSION['upload_info'][$editor_sequence]->allowed_filesize));
$this->add('allowed_attach_size', FileHandler::filesize($_SESSION['upload_info'][$editor_sequence]->allowed_filesize));
$result->allowed_filesize = FileHandler::filesize($_SESSION['upload_info'][$editor_sequence]->allowed_filesize);
$result->allowed_attach_size = FileHandler::filesize($_SESSION['upload_info'][$editor_sequence]->allowed_filesize);
}
if (isset($_SESSION['upload_info'][$editor_sequence]->allowed_extensions))
{
$this->add('allowed_extensions', $_SESSION['upload_info'][$editor_sequence]->allowed_extensions);
$result->allowed_extensions = $_SESSION['upload_info'][$editor_sequence]->allowed_extensions;
}
}

$result->allowed_filetypes = $upload_config->allowed_filetypes ?? null;

// for compatibility
$this->add('allowed_filetypes', $upload_config->allowed_filetypes);
$this->add('upload_status', self::getUploadStatus($attached_size));
$this->add('left_size', $upload_config->allowed_attach_size * 1024 * 1024 - $attached_size);
foreach ($result as $key => $val)
{
$this->add($key, $val);
}
return $result;
}

/**
Expand Down

0 comments on commit 1170238

Please sign in to comment.