From 117023817d829a4fdc6a8bb96cdd248f0013034c Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Tue, 16 Apr 2024 22:38:53 +0900 Subject: [PATCH] Eliminate unnecessary AJAX calls to file.getFileList MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 에디터 로딩 직후, 파일 업로드 직후, 파일 삭제 직후 첨부목록 갱신을 위해 file.getFileList를 호출하도록 되어 있는데, 첨부목록을 직전 요청의 응답과 함께 반환하도록 변경하여 불필요한 AJAX 요청이 발생하지 않도록 하고, 로딩 속도를 개선함. --- .../js/plugins/jquery.fileupload/js/main.js | 42 +++++++--- .../editor/skins/ckeditor/file_upload.html | 5 +- modules/file/file.controller.php | 34 ++++++++ modules/file/file.model.php | 80 +++++++++++-------- 4 files changed, 117 insertions(+), 44 deletions(-) diff --git a/common/js/plugins/jquery.fileupload/js/main.js b/common/js/plugins/jquery.fileupload/js/main.js index 99f2d40d0..d504d0284 100644 --- a/common/js/plugins/jquery.fileupload/js/main.js +++ b/common/js/plugins/jquery.fileupload/js/main.js @@ -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)" + "
\n" + res.response().result); return false; } }, fail: function(e, res) { + $container.data('editorStatus', null); lastUploadTime = Date.now(); setTimeout(function() { if (lastUploadTime < Date.now() - 800) { @@ -228,7 +236,7 @@ }, stop: function() { lastUploadTime = Date.now(); - self.loadFilelist($container); + self.loadFilelist($container, true); }, start: function() { lastUploadTime = Date.now(); @@ -261,7 +269,7 @@ $container.data('xefu-instance', this); // 파일 목록 불러오기 - this.loadFilelist($container); + this.loadFilelist($container, true); // 본문 삽입 data.settings.actSelectedInsertContent.on('click', function() { @@ -415,20 +423,25 @@ 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 = {}; @@ -436,7 +449,7 @@ 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; @@ -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(); diff --git a/modules/editor/skins/ckeditor/file_upload.html b/modules/editor/skins/ckeditor/file_upload.html index 589d902d7..e67763992 100644 --- a/modules/editor/skins/ckeditor/file_upload.html +++ b/modules/editor/skins/ckeditor/file_upload.html @@ -2,7 +2,10 @@ -
+
+
diff --git a/modules/file/file.controller.php b/modules/file/file.controller.php index 706adf716..31fcee19c 100644 --- a/modules/file/file.controller.php +++ b/modules/file/file.controller.php @@ -170,6 +170,23 @@ class FileController extends File { $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 + } } /** @@ -608,6 +625,23 @@ class FileController extends File 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 + } } /** diff --git a/modules/file/file.model.php b/modules/file/file.model.php index a0ab5f827..4781038d0 100644 --- a/modules/file/file.model.php +++ b/modules/file/file.model.php @@ -20,42 +20,53 @@ class FileModel extends File * 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)) @@ -96,44 +107,49 @@ class FileModel extends File } } - // 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; } /**