可以对资源进行增删改
This commit is contained in:
@@ -2,8 +2,12 @@ package com.quinn.service;
|
||||
|
||||
import com.quinn.pojo.Source;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.quinn.vo.SourceUpdateForm;
|
||||
import com.quinn.vo.SourceWriteForm;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
@@ -19,17 +23,38 @@ public interface SourceService extends IService<Source> {
|
||||
|
||||
/**
|
||||
* 下载对应的资源
|
||||
* @param outputStream
|
||||
* @param response
|
||||
* @param source
|
||||
* @throws IOException
|
||||
*/
|
||||
void downloadSource(ServletOutputStream outputStream, Source source) throws IOException;
|
||||
void downloadSource(HttpServletResponse response, Source source) throws IOException;
|
||||
|
||||
void downloadForBaidu(HttpServletResponse response, Source source) throws IOException;
|
||||
|
||||
/**
|
||||
* 点击对应的资源
|
||||
* @throws IOException
|
||||
* @return
|
||||
*/
|
||||
Source hotResource(String sid);
|
||||
Source view(String sid);
|
||||
|
||||
/**
|
||||
* 上传新资源
|
||||
* @param file
|
||||
* @param sourceWriteForm
|
||||
*/
|
||||
void uploadNewSource(MultipartFile file, SourceWriteForm sourceWriteForm) throws IOException;
|
||||
|
||||
/**
|
||||
* 更新资源
|
||||
* @param file
|
||||
* @param sourceUpdateForm
|
||||
*/
|
||||
void updateSource(MultipartFile file, SourceUpdateForm sourceUpdateForm) throws IOException;
|
||||
|
||||
/**
|
||||
* 删除资源
|
||||
* @param sid
|
||||
*/
|
||||
void deleteSource(String sid);
|
||||
}
|
||||
|
||||
@@ -2,20 +2,29 @@ package com.quinn.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.quinn.common.QuinnConstant;
|
||||
import com.quinn.common.SourceType;
|
||||
import com.quinn.mapper.SourceCategoryMapper;
|
||||
import com.quinn.pojo.Source;
|
||||
import com.quinn.mapper.SourceMapper;
|
||||
import com.quinn.pojo.SourceCategory;
|
||||
import com.quinn.service.SourceService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.quinn.utils.OSSClientUtil;
|
||||
import com.quinn.utils.QuinnUtils;
|
||||
import com.quinn.utils.RedisUtils;
|
||||
import com.quinn.vo.SourceUpdateForm;
|
||||
import com.quinn.vo.SourceWriteForm;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -32,14 +41,16 @@ public class SourceServiceImpl extends ServiceImpl<SourceMapper, Source> impleme
|
||||
OSSClientUtil ossClientUtil;
|
||||
@Resource
|
||||
RedisUtils redisUtils;
|
||||
@Resource
|
||||
SourceCategoryMapper sourceCategoryMapper;
|
||||
|
||||
@Override
|
||||
public void downloadSource(ServletOutputStream outputStream, Source source) throws IOException {
|
||||
public void downloadSource(HttpServletResponse response, Source source) throws IOException {
|
||||
String sourceLink = source.getSourceLink();
|
||||
addDownLoadRecord(source);
|
||||
// 读取文件内容。
|
||||
BufferedInputStream in = new BufferedInputStream(ossClientUtil.downloadFile(sourceLink));
|
||||
BufferedOutputStream out = new BufferedOutputStream(outputStream);
|
||||
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
|
||||
byte[] buffer = new byte[1024];
|
||||
int lenght = 0;
|
||||
while ((lenght = in.read(buffer)) != -1) {
|
||||
@@ -55,26 +66,137 @@ public class SourceServiceImpl extends ServiceImpl<SourceMapper, Source> impleme
|
||||
}
|
||||
|
||||
@Override
|
||||
public Source hotResource(String sid) {
|
||||
Source source = getOne(new QueryWrapper<Source>().eq("id", sid));
|
||||
public void downloadForBaidu(HttpServletResponse response, Source source) throws IOException {
|
||||
addDownLoadRecord(source);
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.write(source.getSourceLink());
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Source view(String sid) {
|
||||
Source source = getOne(new QueryWrapper<Source>().eq("sid", sid));
|
||||
addDownLoadRecord(source);
|
||||
return source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void uploadNewSource(MultipartFile file, SourceWriteForm sourceWriteForm) throws IOException {
|
||||
// 先进行文件上传成功再落地
|
||||
String fileType = "";
|
||||
String fileLink = sourceWriteForm.getSourceLink();
|
||||
if (file != null && !file.isEmpty() && SourceType.OSS.name().equals(sourceWriteForm.getSourceType())){
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
fileType = originalFilename.substring(originalFilename.lastIndexOf(QuinnConstant.LINK_SUFFIX)).toLowerCase();
|
||||
fileLink = ossClientUtil.uploadFile(QuinnUtils.getUuid() + QuinnConstant.LINK_DATE_STR
|
||||
+ QuinnUtils.getStrFromDate(new Date()), file.getInputStream());
|
||||
}
|
||||
// 构建问题对象
|
||||
Source source = new Source();
|
||||
source.setSid(QuinnUtils.getUuid());
|
||||
source.setEnName(sourceWriteForm.getEnName());
|
||||
source.setFileType(fileType);
|
||||
source.setSourceName(sourceWriteForm.getTitle());
|
||||
source.setDetail(sourceWriteForm.getSubContent());
|
||||
source.setSourceContent(sourceWriteForm.getContent());
|
||||
String keyWords = sourceWriteForm.getKeyWords();
|
||||
if (!StringUtils.isEmpty(keyWords)){
|
||||
//兼容中英文逗号
|
||||
keyWords = keyWords.replaceAll(",",",");
|
||||
String[] split = keyWords.split(QuinnConstant.LINK_KEY_WORD);
|
||||
source.setKeyWord1(split[0]);
|
||||
if (split.length > 1){
|
||||
source.setKeyWord2(split[1]);
|
||||
if (split.length > 2){
|
||||
source.setKeyWord3(split[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
source.setSourceType(sourceWriteForm.getSourceType());
|
||||
source.setSourceLink(fileLink);
|
||||
|
||||
// 类型
|
||||
source.setCategoryId(sourceWriteForm.getCategoryId());
|
||||
SourceCategory category = sourceCategoryMapper.selectById(sourceWriteForm.getCategoryId());
|
||||
source.setCategoryName(category.getCategory());
|
||||
source.setDownRecord(0);
|
||||
source.setGmtCreate(QuinnUtils.getTime());
|
||||
source.setGmtUpdate(QuinnUtils.getTime());
|
||||
// 存储对象
|
||||
save(source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateSource(MultipartFile file, SourceUpdateForm sourceUpdateForm) throws IOException {
|
||||
Source before = getOne(new QueryWrapper<Source>().eq("sid",sourceUpdateForm.getSid()));
|
||||
String fileType = before.getFileType();
|
||||
// 先进行文件上传成功再落地
|
||||
String fileLink = before.getSourceLink();
|
||||
if (file != null && !file.isEmpty() && SourceType.OSS.name().equals(sourceUpdateForm.getSourceType())){
|
||||
// 上传了新文件,需要删除之前的文件
|
||||
ossClientUtil.deleteFile(before.getSourceLink());
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
fileType = originalFilename.substring(originalFilename.lastIndexOf(QuinnConstant.LINK_SUFFIX)).toLowerCase();
|
||||
fileLink = ossClientUtil.uploadFile(QuinnUtils.getUuid() + QuinnConstant.LINK_DATE_STR
|
||||
+ QuinnUtils.getStrFromDate(new Date()), file.getInputStream());
|
||||
}else if (!SourceType.OSS.name().equals(sourceUpdateForm.getSourceType()) && !StringUtils.isEmpty(sourceUpdateForm.getSourceLink())){
|
||||
fileLink = sourceUpdateForm.getSourceLink();
|
||||
}
|
||||
before.setEnName(sourceUpdateForm.getEnName());
|
||||
before.setFileType(fileType);
|
||||
before.setSourceName(sourceUpdateForm.getSourceName());
|
||||
before.setDetail(sourceUpdateForm.getDetail());
|
||||
before.setSourceContent(sourceUpdateForm.getSourceContent());
|
||||
String keyWords = sourceUpdateForm.getKeyWords();
|
||||
if (!StringUtils.isEmpty(keyWords)){
|
||||
//兼容中英文逗号
|
||||
keyWords = keyWords.replaceAll(",",",");
|
||||
String[] split = keyWords.split(QuinnConstant.LINK_KEY_WORD);
|
||||
before.setKeyWord1(split[0]);
|
||||
if (split.length > 1){
|
||||
before.setKeyWord2(split[1]);
|
||||
if (split.length > 2){
|
||||
before.setKeyWord3(split[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
before.setSourceType(sourceUpdateForm.getSourceType());
|
||||
before.setSourceLink(fileLink);
|
||||
|
||||
// 类型
|
||||
before.setCategoryId(sourceUpdateForm.getCategoryId());
|
||||
SourceCategory category = sourceCategoryMapper.selectById(sourceUpdateForm.getCategoryId());
|
||||
before.setCategoryName(category.getCategory());
|
||||
before.setGmtUpdate(QuinnUtils.getTime());
|
||||
updateById(before);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteSource(String sid) {
|
||||
Source before = getOne(new QueryWrapper<Source>().eq("sid",sid));
|
||||
if (SourceType.OSS.name().equals(before.getSourceType())){
|
||||
// 上传了新文件,需要删除之前的文件
|
||||
ossClientUtil.deleteFile(before.getSourceLink());
|
||||
}
|
||||
removeById(before);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新页码
|
||||
* @param source
|
||||
*/
|
||||
private void addDownLoadRecord(Source source) {
|
||||
String downLoadTime = redisUtils.get(QuinnConstant.SOURCE_KEY + source.getId());
|
||||
String downLoadTime = redisUtils.get(QuinnConstant.SOURCE_KEY + source.getSid());
|
||||
int downTimes = 0;
|
||||
if (StringUtils.isEmpty(downLoadTime)){
|
||||
downTimes = source.getDownRecord() + 1;
|
||||
}else {
|
||||
downTimes = Integer.parseInt(downLoadTime) + 1;
|
||||
}
|
||||
redisUtils.set(QuinnConstant.SOURCE_KEY + source.getId(),downTimes + "");
|
||||
redisUtils.set(QuinnConstant.SOURCE_KEY + source.getSid(),downTimes + "");
|
||||
source.setDownRecord(source.getDownRecord() + 1);
|
||||
updateById(source);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user