Commit 618b0d9d authored by 刘基明's avatar 刘基明

文件上传 图片宽高

parent abebd70d
......@@ -9,5 +9,6 @@ public final class CommunityConstant {
public static final String PACKAGE_BASE = "com.tanpu.community";
public static final String OSS_PREFIX_URL ="https://tamp-sit.oss-cn-shanghai.aliyuncs.com";
public static final String OSS_PREFIX_FOLDER ="community/";
}
......@@ -2,10 +2,16 @@ package com.tanpu.community.api.beans;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "讨论多图存储对象,等同于newsFeedRes")
public class ImagesDTO {
......
......@@ -69,9 +69,17 @@ public class ThemeController {
return CommonResp.success();
}
@ApiOperation("用户删除自有主题")
@GetMapping(value = "/delete")
@ResponseBody
public CommonResp<Void> delete(@RequestParam String themeId) {
String userId = "liujm";
themeManager.delete(themeId);
return CommonResp.success();
}
@ApiOperation("收藏/取消收藏主题")
@GetMapping(value = "/collect")
@PostMapping(value = "/collect")
@ResponseBody
public CommonResp<Void> bookTheme(@RequestBody CollectThemeReq req) {
String user = "liujm";
......@@ -80,7 +88,7 @@ public class ThemeController {
}
@ApiOperation("举报主题")
@GetMapping(value = "/report")
@PostMapping(value = "/report")
@ResponseBody
public CommonResp complaintTheme(@RequestBody ReportThemeReq req) {
......
......@@ -7,6 +7,7 @@ import com.tanpu.community.service.OSSFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
......@@ -33,7 +34,11 @@ public class FileManager {
throw new BizException(ErrorCodeConstant.FILE_UPLOAD_FAIL);
}
String originalName = file.getOriginalFilename();
if (data.length==0 || StringUtils.isEmpty(originalName)){
throw new BizException("上传文件为空");
}
return ossFileService.uploadFile(data, originalName);
}
......
......@@ -315,4 +315,8 @@ public class ThemeManager {
}
public void delete(String themeId) {
themeService.deleteById(themeId);
}
}
......@@ -3,7 +3,9 @@ package com.tanpu.community.service;
import com.tanpu.common.constant.BizStatus;
import com.tanpu.common.exception.BizException;
import com.tanpu.common.util.AliyunOSSHelper;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.common.uuid.UuidGenHelper;
import com.tanpu.community.api.CommunityConstant;
import com.tanpu.community.api.enums.FileTypeEnum;
import com.tanpu.community.api.enums.OssRelType;
import com.tanpu.community.dao.entity.community.FileRecordEntity;
......@@ -17,6 +19,10 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.List;
@Slf4j
......@@ -71,7 +77,7 @@ public class OSSFileService {
if (FileTypeEnum.imageTypeSet.contains(suffix)) {
String id = uuidGenHelper.getUuidStr();
String key = id + "." + suffix;
String key = CommunityConstant.OSS_PREFIX_FOLDER+id + "." + suffix;
ossHelper.writeFile(bucketName, key, data, fileSuffix);
FileRecordEntity record = new FileRecordEntity();
......@@ -81,7 +87,9 @@ public class OSSFileService {
record.setFileName(fileName);
record.setFileId(id);
record.setPreviewUrl(ossHelper.getPreSignedUrl(bucketName, key));
record.setFileType(relType.type);
record.setFileType(FileTypeEnum.IMAGE.getCode());
HashMap<String, Integer> attr = getStringIntegerHashMap(data);
record.setExtInfo(JsonUtil.toJson(attr));
fileRecordMapper.insert(record);
......@@ -92,6 +100,20 @@ public class OSSFileService {
}
private HashMap<String, Integer> getStringIntegerHashMap(byte[] data) {
ByteArrayInputStream bins = new ByteArrayInputStream(data);
BufferedImage bi = null;
try {
bi = ImageIO.read(bins);
} catch (Exception e) {
e.printStackTrace();
}
HashMap<String, Integer> attr = new HashMap<>();
attr.put("width",bi.getWidth());
attr.put("height",bi.getHeight());
return attr;
}
public FileRecordEntity queryById(String fileId) {
return fileRecordMapper.selectById(fileId);
}
......
......@@ -4,6 +4,8 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tanpu.common.api.CommonResp;
import com.tanpu.common.enums.fund.ProductTypeEnum;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.community.api.beans.ImagesDTO;
import com.tanpu.community.api.beans.qo.AttachmentDetailVo;
import com.tanpu.community.api.beans.qo.ThemeContentQo;
import com.tanpu.community.api.beans.qo.ThemeQo;
......@@ -29,6 +31,7 @@ import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
......@@ -85,7 +88,7 @@ public class ProductService {
// 设置产品信息
Map<String, FundInfoBaseResp> fundMap = Maps.newHashMap();
// 图片信息
Map<String, String> imgMap = Maps.newHashMap();
Map<String, FileRecordEntity> imgMap = Maps.newHashMap();
// 话题标题
Map<String, String> topicMap = Maps.newHashMap();
// 批量查询资源信息
......@@ -109,7 +112,7 @@ public class ProductService {
Map<String, CourseSimpleResp> courseMap,
Map<String, ZhiboListResp> zhiboMap,
Map<String, FundInfoBaseResp> fundMap,
Map<String, String> imgMap,
Map<String, FileRecordEntity> imgMap,
Map<String, String> topicMap,
List<ThemeQo> themeQos
) {
......@@ -203,11 +206,11 @@ public class ProductService {
}
}
if (!CollectionUtils.isEmpty(imageIds)) {
// 查询文件
// 查询图片
List<FileRecordEntity> fileRecordEntities = ossFileService.queryByIds(imageIds);
if (!CollectionUtils.isEmpty(fileRecordEntities)) {
imgMap.putAll(fileRecordEntities.stream().collect(Collectors
.toMap(FileRecordEntity::getFileId, FileRecordEntity::getUrl)));
.toMap(FileRecordEntity::getFileId, o->o)));
}
}
if (!CollectionUtils.isEmpty(topicIds)) {
......@@ -336,7 +339,7 @@ public class ProductService {
Map<String, CourseSimpleResp> courseMap,
Map<String, ZhiboListResp> zhiboMap,
Map<String, FundInfoBaseResp> fundMap,
Map<String, String> imgUrlMap,
Map<String, FileRecordEntity> imgUrlMap,
Map<String, String> topicMap) {
for (ThemeQo themeQo : themeQos) {
if (!StringUtils.isEmpty(themeQo.getTopicId()) && topicMap.containsKey(themeQo.getTopicId())) {
......@@ -380,7 +383,19 @@ public class ProductService {
}
} else if (themeContent.getType().equals(RelTypeEnum.SINGLE_IMG.type)) {
if (imgUrlMap.containsKey(themeContent.getValue())) {
themeContent.setValue(imgUrlMap.get(themeContent.getValue()));
FileRecordEntity imgEntity = imgUrlMap.get(themeContent.getValue());
themeContent.setValue(imgEntity.getUrl());
String extInfo = imgEntity.getExtInfo();
if (!StringUtils.isEmpty(extInfo)){
Map<String, Object> extMap = JsonUtil.toMap(extInfo);
ImagesDTO imagesDTO = ImagesDTO.builder().imgHeight((Integer) extMap.get("height"))
.imgWidth((Integer) extMap.get("width"))
.build();
themeContent.setImgList(Collections.singletonList(imagesDTO));
}
}
}
}
......
......@@ -115,4 +115,13 @@ public class ThemeService {
.eq(ThemeEntity::getAuthorId, userId)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
}
public void deleteById(String themeId) {
ThemeEntity themeEntity = themeMapper.selectById(themeId);
if (themeEntity==null){
throw new BizException("主题未找到,id:"+themeId);
}
themeEntity.setDeleteTag(DeleteTagEnum.DELETED.getCode());
themeMapper.updateById(themeEntity);
}
}
......@@ -36,12 +36,12 @@ public class ConvertUtil {
ThemeQo themeQO = new ThemeQo();
BeanUtils.copyProperties(themeEntity, themeQO);
List<ThemeContentQo> themeContentQos = JsonUtil.toBean(themeEntity.getContent(), new TypeReference<List<ThemeContentQo>>() {});
//首页列表使用,限制附件个数为1(1文本+1附件)
if (themeContentQos != null && themeContentQos.size() > 2) {
themeQO.setContent(themeContentQos.subList(0, 2));
}else{
//首页列表使用,限制附件个数为1(1文本+1附件),-》已经改为全部返回
// if (themeContentQos != null && themeContentQos.size() > 2) {
// themeQO.setContent(themeContentQos.subList(0, 2));
// }else{
themeQO.setContent(themeContentQos);
}
// }
return themeQO;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment