预览优化

This commit is contained in:
cjw 2024-06-27 14:48:20 +08:00
parent 61488b5969
commit dc3ad5a99c
7 changed files with 35 additions and 71 deletions

View File

@ -119,7 +119,7 @@ public class SysOssPersonController extends BaseController {
@SaCheckPermission("file:person:preview")
@PostMapping("/preview/{id}")
public void preview(@PathVariable Long id, HttpServletResponse response) throws Exception {
sysOssPersonService.preview(id, response);
public R<String> preview(@NotNull(message = "主键不能为空") @PathVariable Long id) throws Exception {
return R.ok(sysOssPersonService.preview(id));
}
}

View File

@ -72,5 +72,5 @@ public interface ISysOssPersonService {
Boolean moveByBo(SysOssPersonBo bo);
void download(Long id, HttpServletResponse response) throws IOException;
void preview(Long id, HttpServletResponse response) throws Exception;
String preview(Long id) throws Exception;
}

View File

@ -45,6 +45,15 @@ public interface ISysOssService {
*/
SysOssVo getById(Long ossId);
/**
* 根据 ossId 从缓存或数据库中获取Url
*
* @param ossId 文件在数据库中的唯一标识
* @return url 对象链接
*/
String getUrlById(Long ossId);
/**
* 上传 MultipartFile 到对象存储服务并保存文件信息到数据库
*
@ -87,15 +96,6 @@ public interface ISysOssService {
*/
SysOssVo getByMd5(String md5);
/**
* 文件预览
*
* @param ossId
* @param response
* @throws IOException
*/
void preview(Long ossId, HttpServletResponse response) throws Exception;
SysOssVolumeVo getVolumeVoByType(Integer type);
Boolean increase(int type ,Long use);

View File

@ -164,7 +164,7 @@ public class SysOssPersonServiceImpl implements ISysOssPersonService {
if (isValid) {
//TODO 做一些业务上的校验,判断是否需要校验
}
for(Long id :ids){
for (Long id : ids) {
SysOssPersonVo ossPersonVo = baseMapper.selectVoById(id);
SysOssVo ossVo = ossService.getById(ossPersonVo.getOssId());
ossService.decrease(2, ossVo.getSize());
@ -213,8 +213,8 @@ public class SysOssPersonServiceImpl implements ISysOssPersonService {
}
@Override
public void preview(Long id, HttpServletResponse response) throws Exception {
public String preview(Long id) throws Exception {
SysOssPerson ossPerson = baseMapper.selectById(id);
ossService.preview(ossPerson.getOssId(), response);
return ossService.getUrlById(ossPerson.getOssId());
}
}

View File

@ -264,7 +264,7 @@ public class SysOssResourceServiceImpl implements ISysOssResourceService {
@Override
public void preview(Long id, HttpServletResponse response) throws Exception {
SysOssResource ossResource = baseMapper.selectById(id);
ossService.preview(ossResource.getOssId(), response);
//ossService.preview(ossResource.getOssId(), response);
baseMapper.addPreviewNum(id);
}
}

View File

@ -4,9 +4,6 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.unit.DataSizeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@ -42,7 +39,9 @@ import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
@ -169,6 +168,19 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
return baseMapper.selectVoById(ossId);
}
@Override
public String getUrlById(Long ossId) {
SysOssVo vo = SpringUtils.getAopProxy(this).getById(ossId);
if (ObjectUtil.isNotNull(vo)) {
try {
return this.matchingUrl(vo).getUrl();
} catch (Exception ignored) {
// 如果oss异常无法连接则将数据直接返回
return vo.getUrl();
}
}
return null;
}
/**
* 文件下载方法支持一次性下载完整文件
@ -324,56 +336,6 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
return baseMapper.selectVoOne(new LambdaQueryWrapper<SysOss>().eq(SysOss::getFileMd5, md5));
}
@Override
public void preview(Long ossId, HttpServletResponse response) throws Exception {
SysOssVo sysOss = SpringUtils.getAopProxy(this).getById(ossId);
OssClient storage = OssFactory.instance(sysOss.getService());
InputStream is = null;
OutputStream os = null;
try {
is = storage.getObjectContent(sysOss.getFileName());
os = response.getOutputStream();
byte[] byteData = new byte[1024];
// 前端word预览仅支持.docx.doc转换为.docx
if ((".doc").equals(sysOss.getFileSuffix())) {
Document doc = new Document(is);
doc.save(os, com.aspose.words.SaveFormat.DOCX);
}
if ((".xls").equals(sysOss.getFileSuffix())) {
Workbook xls = new Workbook(is);
xls.save(os, com.aspose.cells.SaveFormat.XLSX);
}
if ((".ppt").equals(sysOss.getFileSuffix())) {
Presentation ppt = new Presentation(is);
ppt.save(os, com.aspose.slides.SaveFormat.Pdf);
}
if ((".pptx").equals(sysOss.getFileSuffix())) {
Presentation pptx = new Presentation(is);
pptx.save(os, com.aspose.slides.SaveFormat.Pdf);
}
int len = 0;
while ((len = is.read(byteData)) != -1) {
os.write(byteData, 0, len);
}
os.flush();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
//log.error("IO异常", e);
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
//log.error("IO异常", e);
}
}
}
}
@Override
public SysOssVolumeVo getVolumeVoByType(Integer type) {
SysOssVolumeVo ossVolumeVo = volumeMapper.selectVoOne(new LambdaQueryWrapper<SysOssVolume>().eq(SysOssVolume::getType, type));

View File

@ -275,7 +275,7 @@ public class SysOssTextbookServiceImpl implements ISysOssTextbookService {
@Override
public void preview(Long id, HttpServletResponse response) throws Exception {
SysOssTextbook ossTextbook = baseMapper.selectById(id);
ossService.preview(ossTextbook.getOssId(), response);
//ossService.preview(ossTextbook.getOssId(), response);
baseMapper.addPreviewNum(id);
}
@ -330,6 +330,8 @@ public class SysOssTextbookServiceImpl implements ISysOssTextbookService {
list.add(".avi");
list.add(".mkv");
}
default -> {
}
}
return list;
}