inleft
2022-02-22 31e475bd52f8b4decb9614028a0046f67d224f5c
commit | author | age
0613f2 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.modular.blogarticle.controller;
26
bd3bc1 27 import cn.hutool.core.bean.BeanUtil;
31e475 28 import cn.hutool.core.date.DateUtil;
0613f2 29 import cn.hutool.core.util.PageUtil;
I 30 import cn.hutool.core.util.StrUtil;
bd3bc1 31 import cn.hutool.crypto.SecureUtil;
0613f2 32 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
552004 33 import org.springframework.validation.annotation.Validated;
I 34 import org.springframework.web.bind.annotation.*;
0613f2 35 import vip.xiaonuo.core.annotion.BusinessLog;
bd3bc1 36 import vip.xiaonuo.core.consts.MyConstant;
cfde48 37 import vip.xiaonuo.core.context.constant.ConstantContextHolder;
0613f2 38 import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
bd3bc1 39 import vip.xiaonuo.core.exception.BlogException;
I 40 import vip.xiaonuo.core.exception.enums.BlogExceptionEnum;
0613f2 41 import vip.xiaonuo.core.pojo.response.ResponseData;
I 42 import vip.xiaonuo.core.pojo.response.SuccessResponseData;
bd3bc1 43 import vip.xiaonuo.modular.blogarticle.entity.BlogArticle;
552004 44 import vip.xiaonuo.modular.blogarticle.param.BlogArticleAddDto;
0613f2 45 import vip.xiaonuo.modular.blogarticle.param.BlogArticleQueryDto;
I 46 import vip.xiaonuo.modular.blogarticle.param.BlogArticleVo;
47 import vip.xiaonuo.modular.blogarticle.service.BlogArticleService;
552004 48 import vip.xiaonuo.modular.blogarticletype.entity.BlogArticleType;
I 49 import vip.xiaonuo.modular.blogarticletype.service.BlogArticleTypeService;
cfde48 50 import vip.xiaonuo.sys.modular.file.entity.SysFileInfo;
I 51 import vip.xiaonuo.sys.modular.file.service.SysFileInfoService;
0613f2 52
I 53 import javax.annotation.Resource;
552004 54 import java.util.Date;
0613f2 55 import java.util.HashMap;
I 56 import java.util.List;
57 import java.util.Map;
58 import java.util.stream.Collectors;
59
60 /**
61  * blog文章控制器 (提供给外部blog系统查询)
62  *
63  * @author inleft
64  * @date 2022-02-09 18:20:22
65  */
66 @RestController
67 @RequestMapping("/outside")
68 public class BlogArticleOutsideController {
69
70     @Resource
71     private BlogArticleService blogArticleService;
72
cfde48 73
I 74     @Resource
552004 75     private BlogArticleTypeService blogArticleTypeService;
I 76
77     @Resource
cfde48 78     private SysFileInfoService fileInfoService;
552004 79
I 80     @PostMapping("/blogArticle/add")
81     @BusinessLog(title = "外部blog系统_blog文章_增加", opType = LogAnnotionOpTypeEnum.ADD)
82     public ResponseData add(@RequestBody @Validated(BlogArticleAddDto.add.class) BlogArticleAddDto addDto) {
31e475 83         if (!SecureUtil.md5(addDto.getSecret()).equals("b49e4cc48616cfb8d5ed3e5b983165c8")) {
552004 84             throw new BlogException("口令错误..");
I 85         }
86
87         if (MyConstant.Yes.equals(addDto.getIsTop()) && addDto.getTopValue() == null) {
88             throw new BlogException("选择了置顶需要指定置顶值..");
89         }
90
91         if (addDto.getIsLock().equals(MyConstant.Yes)) {
92             if (addDto.getAuthStatus().equals(MyConstant.AuthStatus.authCode) && StrUtil.isEmpty(addDto.getAuthPassword())) {
93                 throw new BlogException("选择了密码授权,密码不能为空");
94             }
95         } else {
96             addDto.setAuthPassword(null);
97         }
98
99         if (MyConstant.Yes.equals(addDto.getIsOnline())) {
100             if (addDto.getArticleFileId() == null || fileInfoService.getById(addDto.getArticleFileId()) == null) {
101                 throw new BlogException("已选择在线模式..文件id非法");
102             }
103         } else {
104             if (StrUtil.isEmpty(addDto.getContent())) {
105                 throw new BlogException("已选择在线模式..文件内容非法");
106             }
107         }
108
109         if (addDto.getCoverFileId() != null && fileInfoService.getById(addDto.getCoverFileId()) == null) {
110             throw new BlogException("封面文件id非法");
111         }
112
113         if (blogArticleTypeService.lambdaQuery()
114                 .eq(BlogArticleType::getIsEnable, MyConstant.Yes)
115                 .eq(BlogArticleType::getId, addDto.getArticleTypeId())
116                 .one() == null) {
117             throw new BlogException("分类id非法");
118         }
119
120         if (addDto.getPublishDate() == null) {
121             addDto.setPublishDate(new Date());
122         }
123
124         BlogArticle article = new BlogArticle();
125         BeanUtil.copyProperties(addDto, article);
31e475 126         article.setSeparateYear(DateUtil.year(article.getPublishDate()));
I 127         article.setSeparateMonth(DateUtil.month(article.getPublishDate()));
128         article.setSeparateDay(DateUtil.dayOfMonth(article.getPublishDate()));
129
130         article.setIsEnable(MyConstant.Yes);
131         blogArticleService.save(article);
552004 132         System.out.println(article.toString());
31e475 133
552004 134
I 135 //        blogArticleService.add(blogArticleParam);
136         return new SuccessResponseData();
137     }
cfde48 138
0613f2 139     /**
I 140      * 查询blog文章
141      *
142      * @author inleft
143      * @date 2022-02-09 18:20:22
144      */
145     @GetMapping("/blogArticle/queryBlogArticleList")
146     @BusinessLog(title = "外部blog系统_blog文章_查询", opType = LogAnnotionOpTypeEnum.QUERY)
147     public ResponseData queryBlogArticleList(BlogArticleQueryDto queryDto) {
148
149
150         Map<String, Object> param = new HashMap<>(16);
151         param.put("pageNo", PageUtil.getStart(queryDto.getPageNo() - 1, queryDto.getPageSize()));
152         param.put("pageSize", queryDto.getPageSize());
a9c4c9 153         param.put("typeId", queryDto.getTypeId());
0613f2 154
cfde48 155         String blogSourcePrefix = ConstantContextHolder.getBlogSourcePrefix();
a9c4c9 156         List<BlogArticleVo> resList = blogArticleService.searchList(param).stream().map(e -> {
504927 157             if (StrUtil.isNotEmpty(e.getCoverFileURL())) {
I 158                 if (!e.getCoverFileURL().startsWith("http")) {
159                     //补上访问参数
cfde48 160                     e.setCoverFileURL(blogSourcePrefix + e.getCoverFileURL());
504927 161                 }
0613f2 162             }
I 163             return e;
164         }).collect(Collectors.toList());
165
a9c4c9 166         long count = blogArticleService.searchListCount(param);
0613f2 167
I 168         Page<BlogArticleVo> queryPage = new Page<>(queryDto.getPageNo(), queryDto.getPageSize());
169         queryPage.setRecords(resList);
170         queryPage.setTotal(count);
171
172         return new SuccessResponseData(queryPage);
173     }
174
bd3bc1 175     @GetMapping("/blogArticle/queryBlogArticleDetail")
I 176     @BusinessLog(title = "外部blog系统_blog文章详情_查询", opType = LogAnnotionOpTypeEnum.QUERY)
177     public ResponseData queryBlogArticleDetail(BlogArticleQueryDto queryDto) {
178         BlogArticle find = blogArticleService.lambdaQuery()
179                 .eq(BlogArticle::getIsEnable, MyConstant.Yes)
180                 .eq(BlogArticle::getEditorStatus, MyConstant.Yes)
181                 .eq(BlogArticle::getId, queryDto.getId())
182                 .one();
183
184         if (find == null) {
185             throw new BlogException(BlogExceptionEnum.article_not_found);
186         }
187
188         //加密文章
189         if (find.getAuthStatus().equals(MyConstant.AuthStatus.authCode)) {
190             //授权码缺失
191             if (StrUtil.isEmpty(queryDto.getAuthWord())) {
192                 throw new BlogException(BlogExceptionEnum.article_auth_error);
193             }
194             //授权码比对
195             if (!SecureUtil.md5(find.getAuthPassword()).equals(queryDto.getAuthWord())) {
196                 throw new BlogException(BlogExceptionEnum.article_auth_pass_error);
197             }
cfde48 198         } else if (find.getAuthStatus().equals(MyConstant.AuthStatus.privateCode)) {
bd3bc1 199             throw new BlogException(BlogExceptionEnum.article_auth_private_error);
I 200         }
201
202         BlogArticleVo vo = new BlogArticleVo();
203
204         BeanUtil.copyProperties(find, vo);
205
206         if (queryDto.getId() % 2 == 0) {
cfde48 207             String tempURL = this.getBlogSourceURL(find.getArticleFileId());
31e475 208
I 209             if (StrUtil.isEmpty(tempURL))
210                 throw new BlogException(BlogExceptionEnum.article_file_lose);
cfde48 211
I 212             vo.setArticleFileURL(tempURL);
bd3bc1 213         } else {
I 214             vo.setArticleFileURL("http://t.inleft.com/share/book/blog/es-search.md");
215         }
216
217         return new SuccessResponseData(vo);
218     }
219
cfde48 220     private String getBlogSourceURL(Long fileId) {
I 221         SysFileInfo sysFileInfo = fileInfoService.getById(fileId);
222         if (sysFileInfo == null) {
223             return null;
224         } else {
225             String blogSourcePrefix = ConstantContextHolder.getBlogSourcePrefix();
31e475 226             return blogSourcePrefix + sysFileInfo.getFileBucket() +"/"+ sysFileInfo.getFileObjectName();
cfde48 227         }
I 228     }
0613f2 229
I 230 }