3. 對 UEditor 文件上傳的源碼的理解

這里以上傳圖片為例

  1. 先決條件

    • 你已經(jīng)看完了上一篇 : ueditor 之 holleworld,因為我是使用上一篇的開發(fā)環(huán)境
    • 你已經(jīng)知道如何搭建 ueditor 的 helloworld
  2. 問題

    1. ueditor 上傳圖片的過程不受我們控制,編輯過程產(chǎn)生的垃圾(例如:用戶上傳后沒有使用的圖片)不知如何清理?
    2. 上傳圖片只能保存在本服務(wù)器上(繼承上一篇的開發(fā)環(huán)境:也就本 tomcat 上),使得我們無法為本項目配置單獨(dú)一臺圖片服務(wù)器進(jìn)行開發(fā)、生產(chǎn)(服務(wù)器集群)
  3. 尋找后端入口 :

    (com.baidu.ueditor.ActionEnter)

    查看 ueditor.config.js

  4. 下載 ueditor 源碼

    ueditor官網(wǎng)下載鏈接

  5. 刪除 ueditor jar包

  6. 引入源碼

    幾個比較重要的類(突然然發(fā)現(xiàn)這些名字都起的好好,見名知義):

    1. ActionEnter.java
      后臺入口,調(diào)用 CongigManager 加載 jsp/config.json 的配置信息,根據(jù)不同請求執(zhí)行不同的邏輯代碼

      1. 構(gòu)造方法,初始化的時候 利用 CongigManager 加載 jsp/config.json 的數(shù)據(jù)
              public ActionEnter ( HttpServletRequest request, String rootPath ) {
              this.request = request;
              this.rootPath = rootPath;
              this.actionType = request.getParameter( "action" );
              this.contextPath = request.getContextPath();
              this.configManager = ConfigManager.getInstance( this.rootPath, this.contextPath, request.getRequestURI() );
              
          }
      
      1. 根據(jù)不同請求執(zhí)行不同的邏輯代碼
              public String invoke() {
          
          if ( actionType == null || !ActionMap.mapping.containsKey( actionType ) ) {
              return new BaseState( false, AppInfo.INVALID_ACTION ).toJSONString();
          }
          
          if ( this.configManager == null || !this.configManager.valid() ) {
              return new BaseState( false, AppInfo.CONFIG_ERROR ).toJSONString();
          }
          
          State state = null;
          
          int actionCode = ActionMap.getType( this.actionType );
          
          Map<String, Object> conf = null;
          
          switch ( actionCode ) {
          
              case ActionMap.CONFIG:
                  return this.configManager.getAllConfig().toString();
                  
              case ActionMap.UPLOAD_IMAGE:
              case ActionMap.UPLOAD_SCRAWL:
              case ActionMap.UPLOAD_VIDEO:
              case ActionMap.UPLOAD_FILE:
                  conf = this.configManager.getConfig( actionCode );
                  state = new Uploader( request, conf ).doExec();
                  break;
                  
              case ActionMap.CATCH_IMAGE:
                  conf = configManager.getConfig( actionCode );
                  String[] list = this.request.getParameterValues( (String)conf.get( "fieldName" ) );
                  state = new ImageHunter( conf ).capture( list );
                  break;
                  
              case ActionMap.LIST_IMAGE:
              case ActionMap.LIST_FILE:
                  conf = configManager.getConfig( actionCode );
                  int start = this.getStartIndex();
                  state = new FileManager( conf ).listFile( start );
                  break;
                  
          }
          
          return state.toJSONString();
          
      }
      
      
    2. ConfigManager.java

      配置管理器 : 看下面代碼對比一下 jsp/config.json 就知道了

      
          public Map<String, Object> getConfig ( int type ) {
          
          Map<String, Object> conf = new HashMap<String, Object>();
          String savePath = null;
          
          switch ( type ) {
          
              case ActionMap.UPLOAD_FILE:
                  conf.put( "isBase64", "false" );
                  conf.put( "maxSize", this.jsonConfig.getLong( "fileMaxSize" ) );
                  conf.put( "allowFiles", this.getArray( "fileAllowFiles" ) );
                  conf.put( "fieldName", this.jsonConfig.getString( "fileFieldName" ) );
                  savePath = this.jsonConfig.getString( "filePathFormat" );
                  break;
                  
              case ActionMap.UPLOAD_IMAGE:
                  conf.put( "isBase64", "false" );
                  conf.put( "maxSize", this.jsonConfig.getLong( "imageMaxSize" ) );
                  conf.put( "allowFiles", this.getArray( "imageAllowFiles" ) );
                  conf.put( "fieldName", this.jsonConfig.getString( "imageFieldName" ) );
                  savePath = this.jsonConfig.getString( "imagePathFormat" );
                  break;
                  
              case ActionMap.UPLOAD_VIDEO:
                  conf.put( "maxSize", this.jsonConfig.getLong( "videoMaxSize" ) );
                  conf.put( "allowFiles", this.getArray( "videoAllowFiles" ) );
                  conf.put( "fieldName", this.jsonConfig.getString( "videoFieldName" ) );
                  savePath = this.jsonConfig.getString( "videoPathFormat" );
                  break;
                  
              case ActionMap.UPLOAD_SCRAWL:
                  conf.put( "filename", ConfigManager.SCRAWL_FILE_NAME );
                  conf.put( "maxSize", this.jsonConfig.getLong( "scrawlMaxSize" ) );
                  conf.put( "fieldName", this.jsonConfig.getString( "scrawlFieldName" ) );
                  conf.put( "isBase64", "true" );
                  savePath = this.jsonConfig.getString( "scrawlPathFormat" );
                  break;
                  
              case ActionMap.CATCH_IMAGE:
                  conf.put( "filename", ConfigManager.REMOTE_FILE_NAME );
                  conf.put( "filter", this.getArray( "catcherLocalDomain" ) );
                  conf.put( "maxSize", this.jsonConfig.getLong( "catcherMaxSize" ) );
                  conf.put( "allowFiles", this.getArray( "catcherAllowFiles" ) );
                  conf.put( "fieldName", this.jsonConfig.getString( "catcherFieldName" ) + "[]" );
                  savePath = this.jsonConfig.getString( "catcherPathFormat" );
                  break;
                  
              case ActionMap.LIST_IMAGE:
                  conf.put( "allowFiles", this.getArray( "imageManagerAllowFiles" ) );
                  conf.put( "dir", this.jsonConfig.getString( "imageManagerListPath" ) );
                  conf.put( "count", this.jsonConfig.getInt( "imageManagerListSize" ) );
                  break;
                  
              case ActionMap.LIST_FILE:
                  conf.put( "allowFiles", this.getArray( "fileManagerAllowFiles" ) );
                  conf.put( "dir", this.jsonConfig.getString( "fileManagerListPath" ) );
                  conf.put( "count", this.jsonConfig.getInt( "fileManagerListSize" ) );
                  break;
                  
          }
          
          conf.put( "savePath", savePath );
          conf.put( "rootPath", this.rootPath );
          
          return conf;
          
      }
      
      
    
    3. Uploader.java
    
        執(zhí)行下載
    
    4. State.java
        
        處理狀態(tài)接口 ,前后臺溝通的接口。
        
    5. BaseState.java
    
        State 的其中一個實現(xiàn)類里面包含了一個 Map<String ,String > 實例,通過 Map 添加前臺需要的參數(shù)。
        
        1. 前臺請求規(guī)范
            ![](http://upload-images.jianshu.io/upload_images/4139030-0ccf38bfba31759b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
        
        2. 后臺返回規(guī)范(以上傳圖片為例)
           ![](http://upload-images.jianshu.io/upload_images/4139030-666d2791beb13a2d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 
    
    
  7. 具體后臺運(yùn)行過程,自己用 debug 工具,多走幾下就知道了(這里就不演示了)

  8. 運(yùn)行項目

下一篇 : ueditor 實現(xiàn) 自定義上傳

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容