RestApiController.java 4.1 KB
Newer Older
Y
yadong.zhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**
 * MIT License
 * Copyright (c) 2018 yadong.zhang
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.zyd.blog.controller;

智布道's avatar
智布道 已提交
22
import com.zyd.blog.business.entity.Config;
Y
yadong.zhang 已提交
23 24
import com.zyd.blog.business.enums.QiniuUploadType;
import com.zyd.blog.business.service.BizArticleService;
智布道's avatar
智布道 已提交
25 26 27
import com.zyd.blog.business.service.SysConfigService;
import com.zyd.blog.core.websocket.server.ZydWebsocketServer;
import com.zyd.blog.core.websocket.util.WebSocketUtil;
Y
yadong.zhang 已提交
28 29 30
import com.zyd.blog.framework.object.ResponseVO;
import com.zyd.blog.util.FileUtil;
import com.zyd.blog.util.ResultUtil;
智布道's avatar
智布道 已提交
31
import org.apache.shiro.authz.annotation.RequiresPermissions;
Y
yadong.zhang 已提交
32 33 34 35 36 37 38
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

智布道's avatar
智布道 已提交
39 40 41 42
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

Y
yadong.zhang 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/**
 * 其他api性质的接口
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @website https://www.zhyd.me
 * @date 2018/4/24 14:37
 * @since 1.0
 */
@RestController
@RequestMapping("/api")
public class RestApiController {

    @Autowired
    private BizArticleService articleService;
智布道's avatar
智布道 已提交
58 59 60 61
    @Autowired
    private SysConfigService configService;
    @Autowired
    private ZydWebsocketServer websocketServer;
Y
yadong.zhang 已提交
62 63 64 65 66 67 68

    /**
     * 上传文件到七牛云
     *
     * @param file
     * @return
     */
智布道's avatar
智布道 已提交
69
    @RequiresPermissions("article:publish")
Y
yadong.zhang 已提交
70 71 72 73 74 75
    @PostMapping("/upload2Qiniu")
    public ResponseVO upload2Qiniu(@RequestParam("file") MultipartFile file) {
        String filePath = FileUtil.uploadToQiniu(file, QiniuUploadType.SIMPLE, false);
        return ResultUtil.success("图片上传成功", filePath);
    }

智布道's avatar
智布道 已提交
76 77 78 79 80 81 82 83
    @RequiresPermissions("article:publish")
    @PostMapping("/upload2QiniuForMd")
    public Object upload2QiniuForMd(@RequestParam("file") MultipartFile file) {
        String filePath = FileUtil.uploadToQiniu(file, QiniuUploadType.SIMPLE, false);
        Config config = configService.get();
        Map<String, Object> resultMap = new HashMap<>(3);
        resultMap.put("success", 1);
        resultMap.put("message", "上传成功");
84
        resultMap.put("filename", config.getQiuniuBasePath() + filePath);
智布道's avatar
智布道 已提交
85 86 87
        return resultMap;
    }

Y
yadong.zhang 已提交
88 89 90 91 92
    /**
     * 发布文章选择图片时获取素材库
     *
     * @return
     */
智布道's avatar
智布道 已提交
93
    @RequiresPermissions("article:publish")
Y
yadong.zhang 已提交
94 95 96 97
    @PostMapping("/material")
    public ResponseVO material() {
        return ResultUtil.success("", articleService.listMaterial());
    }
智布道's avatar
智布道 已提交
98 99 100 101 102 103 104 105 106 107 108 109

    /**
     * 发送消息通知
     *
     * @return
     */
    @RequiresPermissions("notice")
    @PostMapping("/notice")
    public ResponseVO notice(String msg) throws UnsupportedEncodingException {
        WebSocketUtil.sendNotificationMsg(msg, websocketServer.getOnlineUsers());
        return ResultUtil.success("消息发送成功", articleService.listMaterial());
    }
Y
yadong.zhang 已提交
110
}