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

文件上传 图片宽高

parent abebd70d
...@@ -9,5 +9,6 @@ public final class CommunityConstant { ...@@ -9,5 +9,6 @@ public final class CommunityConstant {
public static final String PACKAGE_BASE = "com.tanpu.community"; 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_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; ...@@ -2,10 +2,16 @@ package com.tanpu.community.api.beans;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
@Data @Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "讨论多图存储对象,等同于newsFeedRes") @ApiModel(value = "讨论多图存储对象,等同于newsFeedRes")
public class ImagesDTO { public class ImagesDTO {
......
...@@ -69,9 +69,17 @@ public class ThemeController { ...@@ -69,9 +69,17 @@ public class ThemeController {
return CommonResp.success(); 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("收藏/取消收藏主题") @ApiOperation("收藏/取消收藏主题")
@GetMapping(value = "/collect") @PostMapping(value = "/collect")
@ResponseBody @ResponseBody
public CommonResp<Void> bookTheme(@RequestBody CollectThemeReq req) { public CommonResp<Void> bookTheme(@RequestBody CollectThemeReq req) {
String user = "liujm"; String user = "liujm";
...@@ -80,7 +88,7 @@ public class ThemeController { ...@@ -80,7 +88,7 @@ public class ThemeController {
} }
@ApiOperation("举报主题") @ApiOperation("举报主题")
@GetMapping(value = "/report") @PostMapping(value = "/report")
@ResponseBody @ResponseBody
public CommonResp complaintTheme(@RequestBody ReportThemeReq req) { public CommonResp complaintTheme(@RequestBody ReportThemeReq req) {
......
...@@ -7,6 +7,7 @@ import com.tanpu.community.service.OSSFileService; ...@@ -7,6 +7,7 @@ import com.tanpu.community.service.OSSFileService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.IOException;
...@@ -33,7 +34,11 @@ public class FileManager { ...@@ -33,7 +34,11 @@ public class FileManager {
throw new BizException(ErrorCodeConstant.FILE_UPLOAD_FAIL); throw new BizException(ErrorCodeConstant.FILE_UPLOAD_FAIL);
} }
String originalName = file.getOriginalFilename(); String originalName = file.getOriginalFilename();
if (data.length==0 || StringUtils.isEmpty(originalName)){
throw new BizException("上传文件为空");
}
return ossFileService.uploadFile(data, originalName); return ossFileService.uploadFile(data, originalName);
} }
......
...@@ -315,4 +315,8 @@ public class ThemeManager { ...@@ -315,4 +315,8 @@ public class ThemeManager {
} }
public void delete(String themeId) {
themeService.deleteById(themeId);
}
} }
...@@ -3,7 +3,9 @@ package com.tanpu.community.service; ...@@ -3,7 +3,9 @@ package com.tanpu.community.service;
import com.tanpu.common.constant.BizStatus; import com.tanpu.common.constant.BizStatus;
import com.tanpu.common.exception.BizException; import com.tanpu.common.exception.BizException;
import com.tanpu.common.util.AliyunOSSHelper; import com.tanpu.common.util.AliyunOSSHelper;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.common.uuid.UuidGenHelper; 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.FileTypeEnum;
import com.tanpu.community.api.enums.OssRelType; import com.tanpu.community.api.enums.OssRelType;
import com.tanpu.community.dao.entity.community.FileRecordEntity; import com.tanpu.community.dao.entity.community.FileRecordEntity;
...@@ -17,6 +19,10 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -17,6 +19,10 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.Resource; 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; import java.util.List;
@Slf4j @Slf4j
...@@ -71,7 +77,7 @@ public class OSSFileService { ...@@ -71,7 +77,7 @@ public class OSSFileService {
if (FileTypeEnum.imageTypeSet.contains(suffix)) { if (FileTypeEnum.imageTypeSet.contains(suffix)) {
String id = uuidGenHelper.getUuidStr(); String id = uuidGenHelper.getUuidStr();
String key = id + "." + suffix; String key = CommunityConstant.OSS_PREFIX_FOLDER+id + "." + suffix;
ossHelper.writeFile(bucketName, key, data, fileSuffix); ossHelper.writeFile(bucketName, key, data, fileSuffix);
FileRecordEntity record = new FileRecordEntity(); FileRecordEntity record = new FileRecordEntity();
...@@ -81,7 +87,9 @@ public class OSSFileService { ...@@ -81,7 +87,9 @@ public class OSSFileService {
record.setFileName(fileName); record.setFileName(fileName);
record.setFileId(id); record.setFileId(id);
record.setPreviewUrl(ossHelper.getPreSignedUrl(bucketName, key)); 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); fileRecordMapper.insert(record);
...@@ -92,6 +100,20 @@ public class OSSFileService { ...@@ -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) { public FileRecordEntity queryById(String fileId) {
return fileRecordMapper.selectById(fileId); return fileRecordMapper.selectById(fileId);
} }
......
...@@ -4,6 +4,8 @@ import com.google.common.collect.Lists; ...@@ -4,6 +4,8 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.tanpu.common.api.CommonResp; import com.tanpu.common.api.CommonResp;
import com.tanpu.common.enums.fund.ProductTypeEnum; 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.AttachmentDetailVo;
import com.tanpu.community.api.beans.qo.ThemeContentQo; import com.tanpu.community.api.beans.qo.ThemeContentQo;
import com.tanpu.community.api.beans.qo.ThemeQo; import com.tanpu.community.api.beans.qo.ThemeQo;
...@@ -29,6 +31,7 @@ import org.springframework.util.StringUtils; ...@@ -29,6 +31,7 @@ import org.springframework.util.StringUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -85,7 +88,7 @@ public class ProductService { ...@@ -85,7 +88,7 @@ public class ProductService {
// 设置产品信息 // 设置产品信息
Map<String, FundInfoBaseResp> fundMap = Maps.newHashMap(); Map<String, FundInfoBaseResp> fundMap = Maps.newHashMap();
// 图片信息 // 图片信息
Map<String, String> imgMap = Maps.newHashMap(); Map<String, FileRecordEntity> imgMap = Maps.newHashMap();
// 话题标题 // 话题标题
Map<String, String> topicMap = Maps.newHashMap(); Map<String, String> topicMap = Maps.newHashMap();
// 批量查询资源信息 // 批量查询资源信息
...@@ -109,7 +112,7 @@ public class ProductService { ...@@ -109,7 +112,7 @@ public class ProductService {
Map<String, CourseSimpleResp> courseMap, Map<String, CourseSimpleResp> courseMap,
Map<String, ZhiboListResp> zhiboMap, Map<String, ZhiboListResp> zhiboMap,
Map<String, FundInfoBaseResp> fundMap, Map<String, FundInfoBaseResp> fundMap,
Map<String, String> imgMap, Map<String, FileRecordEntity> imgMap,
Map<String, String> topicMap, Map<String, String> topicMap,
List<ThemeQo> themeQos List<ThemeQo> themeQos
) { ) {
...@@ -203,11 +206,11 @@ public class ProductService { ...@@ -203,11 +206,11 @@ public class ProductService {
} }
} }
if (!CollectionUtils.isEmpty(imageIds)) { if (!CollectionUtils.isEmpty(imageIds)) {
// 查询文件 // 查询图片
List<FileRecordEntity> fileRecordEntities = ossFileService.queryByIds(imageIds); List<FileRecordEntity> fileRecordEntities = ossFileService.queryByIds(imageIds);
if (!CollectionUtils.isEmpty(fileRecordEntities)) { if (!CollectionUtils.isEmpty(fileRecordEntities)) {
imgMap.putAll(fileRecordEntities.stream().collect(Collectors imgMap.putAll(fileRecordEntities.stream().collect(Collectors
.toMap(FileRecordEntity::getFileId, FileRecordEntity::getUrl))); .toMap(FileRecordEntity::getFileId, o->o)));
} }
} }
if (!CollectionUtils.isEmpty(topicIds)) { if (!CollectionUtils.isEmpty(topicIds)) {
...@@ -336,7 +339,7 @@ public class ProductService { ...@@ -336,7 +339,7 @@ public class ProductService {
Map<String, CourseSimpleResp> courseMap, Map<String, CourseSimpleResp> courseMap,
Map<String, ZhiboListResp> zhiboMap, Map<String, ZhiboListResp> zhiboMap,
Map<String, FundInfoBaseResp> fundMap, Map<String, FundInfoBaseResp> fundMap,
Map<String, String> imgUrlMap, Map<String, FileRecordEntity> imgUrlMap,
Map<String, String> topicMap) { Map<String, String> topicMap) {
for (ThemeQo themeQo : themeQos) { for (ThemeQo themeQo : themeQos) {
if (!StringUtils.isEmpty(themeQo.getTopicId()) && topicMap.containsKey(themeQo.getTopicId())) { if (!StringUtils.isEmpty(themeQo.getTopicId()) && topicMap.containsKey(themeQo.getTopicId())) {
...@@ -380,7 +383,19 @@ public class ProductService { ...@@ -380,7 +383,19 @@ public class ProductService {
} }
} else if (themeContent.getType().equals(RelTypeEnum.SINGLE_IMG.type)) { } else if (themeContent.getType().equals(RelTypeEnum.SINGLE_IMG.type)) {
if (imgUrlMap.containsKey(themeContent.getValue())) { 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 { ...@@ -115,4 +115,13 @@ public class ThemeService {
.eq(ThemeEntity::getAuthorId, userId) .eq(ThemeEntity::getAuthorId, userId)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED)); .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 { ...@@ -36,12 +36,12 @@ public class ConvertUtil {
ThemeQo themeQO = new ThemeQo(); ThemeQo themeQO = new ThemeQo();
BeanUtils.copyProperties(themeEntity, themeQO); BeanUtils.copyProperties(themeEntity, themeQO);
List<ThemeContentQo> themeContentQos = JsonUtil.toBean(themeEntity.getContent(), new TypeReference<List<ThemeContentQo>>() {}); List<ThemeContentQo> themeContentQos = JsonUtil.toBean(themeEntity.getContent(), new TypeReference<List<ThemeContentQo>>() {});
//首页列表使用,限制附件个数为1(1文本+1附件) //首页列表使用,限制附件个数为1(1文本+1附件),-》已经改为全部返回
if (themeContentQos != null && themeContentQos.size() > 2) { // if (themeContentQos != null && themeContentQos.size() > 2) {
themeQO.setContent(themeContentQos.subList(0, 2)); // themeQO.setContent(themeContentQos.subList(0, 2));
}else{ // }else{
themeQO.setContent(themeContentQos); themeQO.setContent(themeContentQos);
} // }
return themeQO; 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