gueditor

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2018 License: MIT Imports: 18 Imported by: 0

README

百度ueditor的go(golang)语言后台服务程序

说明:

百度提供的富文本框插件ueditor,因官方没有提供go版本后台,此项目旨在提供一个go的后台接口

注意:

因个人精力有限,代码中还有一些不足之处,希望使用该库的同学能够发扬开源精神一起完善该库

接口:

主方法在service.go文件中

示例一、:

// 设置umask主要是防止创建文件夹和文件的时候能够正常设置其权限
syscall.Umask(0)
// rootPath为项目的根目录,根据情况自行设定,以image举例,rootPath取值原则是 rootPath + imagePathFormat 为图片保存路径和
rootPath, _    := fileutil.GetCurrentDirectory()
// configFilePath为ueditor的相关配置,库里加载了默认配置,此处配置会与默认配置合并。配置说明参见ueditor官网
configFilePath := filepath.Join(rootPath, "config/ueditor.json") 
// 生成ueditor的服务对象
uedService, _ = gueditor.NewService(nil, nil, rootPath, configFilePath)

示例二、:

基于gin框架的样例(https://github.com/dazhenghu/ginCms 中的admin模块)

package controller

import (
    "github.com/dazhenghu/ginApp/controller"
    "github.com/gin-gonic/gin"
    "github.com/dazhenghu/gueditor"
    "github.com/dazhenghu/util/fileutil"
    "net/http"
    "path/filepath"
    "syscall"
)

type ueditorController struct {
    controller.Controller
}

var ueditorInstance *ueditorController
var uedService *gueditor.Service

func init() {
    ueditorInstance = &ueditorController{}
    ueditorInstance.Init(ueditorInstance)

    syscall.Umask(0)
    rootPath, _    := fileutil.GetCurrentDirectory()
    configFilePath := filepath.Join(rootPath, "config/ueditor.json") // 设置自定义配置文件路径

    rootPath      = filepath.Join(rootPath, "../") // 设置项目根目录
    uedService, _ = gueditor.NewService(nil, nil, rootPath, configFilePath)

    ueditorInstance.PostAndGet("/ueditor", ueditorInstance.index)
}

func (ued *ueditorController) index(context *gin.Context) {
    action := context.Query("action")

    switch action {
    case "config":
        // config接口
        ued.config(context)
    case "uploadimage":
        // 上传图片
        ued.uploadImage(context)
    case "uploadscrawl":
        // 上传涂鸦
        ued.uploadScrawl(context)
    case "uploadvideo":
        // 上传视频
        ued.uploadVideo(context)
    case "uploadfile":
        // 上传附件
        ued.uploadfile(context)
    case "listfile":
        // 查询上传的文件列表
        ued.listFile(context)
    case "listimage":
        // 查询上传的图片列表
        ued.listImage(context)
    }

}

func (ued *ueditorController) config(context *gin.Context) {
    cnf := uedService.Config()
    context.JSON(http.StatusOK, cnf)
}

func (ued *ueditorController) uploadImage(context *gin.Context) {
    res, _ := uedService.Uploadimage(context.Request)
    context.JSON(http.StatusOK, res)
}

func (ued *ueditorController) uploadScrawl(context *gin.Context)  {
    res, _ := uedService.UploadScrawl(context.Request)
    context.JSON(http.StatusOK, res)
}

func (ued *ueditorController) uploadVideo(context *gin.Context)  {
    res, _ := uedService.UploadVideo(context.Request)
    context.JSON(http.StatusOK, res)
}

func (ued *ueditorController) uploadfile(context *gin.Context)  {
    res, _ := uedService.UploadFile(context.Request)
    context.JSON(http.StatusOK, res)
}

func (ued *ueditorController) listFile(context *gin.Context) {
    listFileItem := &gueditor.ListFileItem{}
    uedService.Listfile(listFileItem, 0, 10)
    context.JSON(http.StatusOK, listFileItem)
}

func (ued *ueditorController) listImage(context *gin.Context)  {
    listFileItem := &gueditor.ListFileItem{}
    uedService.ListImage(listFileItem, 0, 10)
    context.JSON(http.StatusOK, listFileItem)
}

Documentation

Index

Constants

View Source
const (
	SUCCESS       string = "SUCCESS" //上传成功标记,在UEditor中内不可改变,否则flash判断会出错
	ERROR         string = "ERROR"
	NO_MATCH_FILE string = "no match file"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	ImageActionName         string   `json:"imageActionName"`
	ImageFieldName          string   `json:"imageFieldName"`
	ImageMaxSize            int      `json:"imageMaxSize"`
	ImageAllowFiles         []string `json:"imageAllowFiles"`
	ImageCompressEnable     bool     `json:"imageCompressEnable"`
	ImageCompressBorder     int      `json:"imageCompressBorder"`
	ImageInsertAlign        string   `json:"imageInsertAlign"`
	ImageURLPrefix          string   `json:"imageUrlPrefix"`
	ImagePathFormat         string   `json:"imagePathFormat"`
	ScrawlActionName        string   `json:"scrawlActionName"`
	ScrawlFieldName         string   `json:"scrawlFieldName"`
	ScrawlPathFormat        string   `json:"scrawlPathFormat"`
	ScrawlMaxSize           int      `json:"scrawlMaxSize"`
	ScrawlURLPrefix         string   `json:"scrawlUrlPrefix"`
	ScrawlInsertAlign       string   `json:"scrawlInsertAlign"`
	SnapscreenActionName    string   `json:"snapscreenActionName"`
	SnapscreenPathFormat    string   `json:"snapscreenPathFormat"`
	SnapscreenURLPrefix     string   `json:"snapscreenUrlPrefix"`
	SnapscreenInsertAlign   string   `json:"snapscreenInsertAlign"`
	CatcherLocalDomain      []string `json:"catcherLocalDomain"`
	CatcherActionName       string   `json:"catcherActionName"`
	CatcherFieldName        string   `json:"catcherFieldName"`
	CatcherPathFormat       string   `json:"catcherPathFormat"`
	CatcherURLPrefix        string   `json:"catcherUrlPrefix"`
	CatcherMaxSize          int      `json:"catcherMaxSize"`
	CatcherAllowFiles       []string `json:"catcherAllowFiles"`
	VideoActionName         string   `json:"videoActionName"`
	VideoFieldName          string   `json:"videoFieldName"`
	VideoPathFormat         string   `json:"videoPathFormat"`
	VideoURLPrefix          string   `json:"videoUrlPrefix"`
	VideoMaxSize            int      `json:"videoMaxSize"`
	VideoAllowFiles         []string `json:"videoAllowFiles"`
	FileActionName          string   `json:"fileActionName"`
	FileFieldName           string   `json:"fileFieldName"`
	FilePathFormat          string   `json:"filePathFormat"`
	FileURLPrefix           string   `json:"fileUrlPrefix"`
	FileMaxSize             int      `json:"fileMaxSize"`
	FileAllowFiles          []string `json:"fileAllowFiles"`
	ImageManagerActionName  string   `json:"imageManagerActionName"`
	ImageManagerListPath    string   `json:"imageManagerListPath"`
	ImageManagerListSize    int      `json:"imageManagerListSize"`
	ImageManagerURLPrefix   string   `json:"imageManagerUrlPrefix"`
	ImageManagerInsertAlign string   `json:"imageManagerInsertAlign"`
	ImageManagerAllowFiles  []string `json:"imageManagerAllowFiles"`
	FileManagerActionName   string   `json:"fileManagerActionName"`
	FileManagerListPath     string   `json:"fileManagerListPath"`
	FileManagerURLPrefix    string   `json:"fileManagerUrlPrefix"`
	FileManagerListSize     int      `json:"fileManagerListSize"`
	FileManagerAllowFiles   []string `json:"fileManagerAllowFiles"`
}
var GloabConfig *Config

type FileItem

type FileItem struct {
	Url   string `json:"url"`   // 文件url
	Mtime int64  `json:"mtime"` // 最后编辑时间
}

type List

type List struct {
	RootPath string      // 项目根目录
	Params   *ListParams // 参数
}

func (*List) GetFileList

func (l *List) GetFileList(start int, size int) (fileList []*FileItem, err error)

* 获取资源列表

func (*List) SetParams

func (l *List) SetParams(params *ListParams) error

func (*List) SetRootPath

func (l *List) SetRootPath(path string) error

type ListFileItem

type ListFileItem struct {
	State string      `json:"state"`
	List  []*FileItem `json:"list"`
	Start int         `json:"start"`
	Total int         `json:"total"`
}

type ListInterface

type ListInterface interface {
	GetFileList(start int, size int) (fileList []*FileItem, err error) // 获取文件夹下的文件列表
	SetParams(params *ListParams) error                                // 设置参数
	SetRootPath(path string) error                                     // 设置根目录
}

type ListParams

type ListParams struct {
	AllowFiles []string // 允许的文件类型
	ListSize   int      // 列表分页大小
	Path       string   // 资源路径
}

type ResFileInfo

type ResFileInfo struct {
	URL      string `json:"url"`
	Title    string `json:"title"`
	Original string `json:"original"`
	Type     string `json:"type"`
	Size     int64  `json:"size"`
}

type ResFileInfoWithState

type ResFileInfoWithState struct {
	State string `json:"state"`
	ResFileInfo
}

type ResFilesInfoWithStates

type ResFilesInfoWithStates struct {
	State string         `json:"state"`
	List  []*ResFileInfo `json:"list"`
}

type Service

type Service struct {
	// contains filtered or unexported fields
}

func NewService

func NewService(uploaderObj UploaderInterface, listObj ListInterface, rootPath string, configFilePath string) (serv *Service, err error)

func NewServiceWithStorageObj

func NewServiceWithStorageObj(storageObj storage.BaseInterface, rootPath string, configFilePath string) (serv *Service, err error)

* 创建service,并制定存储实例

func (*Service) CatchImage

func (serv *Service) CatchImage(r *http.Request) (listRes *ResFilesInfoWithStates, err error)

* 从远程拉取图片

func (*Service) Config

func (serv *Service) Config() (cnf *Config)

* 读取配置信息

func (*Service) ListImage

func (serv *Service) ListImage(listFileItem *ListFileItem, start int, size int)

* 获取图片列表

func (*Service) Listfile

func (serv *Service) Listfile(listFileItem *ListFileItem, start int, size int)

* 获取文件列表

func (*Service) UploadFile

func (serv *Service) UploadFile(r *http.Request) (res *ResFileInfoWithState, err error)

* 上传文件

func (*Service) UploadScrawl

func (serv *Service) UploadScrawl(r *http.Request) (res *ResFileInfoWithState, err error)

* 上传涂鸦

func (*Service) UploadVideo

func (serv *Service) UploadVideo(r *http.Request) (res *ResFileInfoWithState, err error)

* 上传视频

func (*Service) Uploadimage

func (serv *Service) Uploadimage(r *http.Request) (res *ResFileInfoWithState, err error)

* 上传图片

type Uploader

type Uploader struct {
	RootPath string // 项目根目录

	Storage storage.BaseInterface // 文件、图片保存的方法
	// contains filtered or unexported fields
}

func NewUploader

func NewUploader(upParams *UploaderParams) *Uploader

* 新建uploader

func (*Uploader) SaveRemote

func (up *Uploader) SaveRemote(remoteUrl string) (fileInfo *ResFileInfo, err error)

* 拉取远程文件并保存

func (*Uploader) SetParams

func (up *Uploader) SetParams(params *UploaderParams) (err error)

func (*Uploader) SetRootPath

func (up *Uploader) SetRootPath(path string) error

func (*Uploader) SetStorage

func (up *Uploader) SetStorage(storageObj storage.BaseInterface) (err error)

* 设置文件存储对象

func (*Uploader) UpBase64

func (up *Uploader) UpBase64(fileName, base64data string) (fileInfo *ResFileInfo, err error)

* 上传base64数据文件

func (*Uploader) UpFile

func (up *Uploader) UpFile(file multipart.File, fileHeader *multipart.FileHeader) (fileInfo *ResFileInfo, err error)

* 上传文件

type UploaderInterface

type UploaderInterface interface {
	UpFile(file multipart.File, handle *multipart.FileHeader) (fileInfo *ResFileInfo, err error) //上传文件的方法
	UpBase64(fileName, base64data string) (fileInfo *ResFileInfo, err error)                     //处理base64编码的图片上传
	SaveRemote(remoteUrl string) (fileInfo *ResFileInfo, err error)                              // 拉取远程图片
	SetParams(params *UploaderParams) error                                                      // 设置参数信息
	SetRootPath(path string) error                                                               // 设置根目录
	SetStorage(storageObj storage.BaseInterface) error                                           // 设置文件存储的操作实例
}

type UploaderParams

type UploaderParams struct {
	PathFormat string   /* 上传保存路径,可以自定义保存路径和文件名格式 */
	MaxSize    int      /* 上传大小限制,单位B */
	AllowFiles []string /* 上传格式限制 */
	OriName    string   /* 原始文件名 */
}

上传文件的参数

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL