Commit 9f9f90f5 authored by 张辰's avatar 张辰

oss更改目录

parent 5d339557
package com.tanpu.community.api.enums;
public enum OssDirEnum {
Others("others/", "其他"),
Theme_Pic("pic/theme/","主题下的图片"),
;
public String dir;
public String type;
public String getDir() {
return dir;
}
public void setDir(String dir) {
this.dir = dir;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
OssDirEnum(String dir, String type) {
this.dir = dir;
this.type = type;
}
}
...@@ -2,9 +2,12 @@ package com.tanpu.community.controller; ...@@ -2,9 +2,12 @@ package com.tanpu.community.controller;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.tanpu.common.api.CommonResp; import com.tanpu.common.api.CommonResp;
import com.tanpu.common.auth.AuthLogin;
import com.tanpu.common.auth.UserHolder;
import com.tanpu.common.util.JsonUtil; import com.tanpu.common.util.JsonUtil;
import com.tanpu.community.api.beans.qo.ThemeQo; import com.tanpu.community.api.beans.qo.ThemeQo;
import com.tanpu.community.api.beans.resp.FileUploadResp; import com.tanpu.community.api.beans.resp.FileUploadResp;
import com.tanpu.community.api.enums.OssDirEnum;
import com.tanpu.community.cache.RedisCache; import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.manager.FileManager; import com.tanpu.community.manager.FileManager;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -13,6 +16,7 @@ import org.springframework.cache.caffeine.CaffeineCacheManager; ...@@ -13,6 +16,7 @@ import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
...@@ -23,17 +27,24 @@ public class FileController { ...@@ -23,17 +27,24 @@ public class FileController {
@Autowired @Autowired
private FileManager fileManager; private FileManager fileManager;
@Autowired
private RedisCache redisCache;
@Autowired @Resource
private CaffeineCacheManager localCache; private UserHolder userHolder;
@PostMapping("/uploadFile") @PostMapping("/uploadFile")
@ResponseBody @ResponseBody
@AuthLogin
public CommonResp<FileUploadResp> uploadToRemote(@RequestParam(value = "file") MultipartFile file) { public CommonResp<FileUploadResp> uploadToRemote(@RequestParam(value = "file") MultipartFile file) {
return CommonResp.success(fileManager.uploadFile(file)); String userId = userHolder.getUserId();
return CommonResp.success(fileManager.uploadFile(file, OssDirEnum.Others, userId));
}
@PostMapping("/uploadThemePic")
@ResponseBody
@AuthLogin
public CommonResp<FileUploadResp> uploadThemePic(@RequestParam(value = "file") MultipartFile file) {
String userId = userHolder.getUserId();
return CommonResp.success(fileManager.uploadFile(file, OssDirEnum.Theme_Pic, userId));
} }
@GetMapping("/test") @GetMapping("/test")
......
...@@ -3,14 +3,15 @@ package com.tanpu.community.manager; ...@@ -3,14 +3,15 @@ package com.tanpu.community.manager;
import com.tanpu.common.constant.ErrorCodeConstant; import com.tanpu.common.constant.ErrorCodeConstant;
import com.tanpu.common.exception.BizException; import com.tanpu.common.exception.BizException;
import com.tanpu.community.api.beans.resp.FileUploadResp; import com.tanpu.community.api.beans.resp.FileUploadResp;
import com.tanpu.community.api.enums.OssDirEnum;
import com.tanpu.community.dao.entity.community.FileRecordEntity; import com.tanpu.community.dao.entity.community.FileRecordEntity;
import com.tanpu.community.service.OSSFileService; import com.tanpu.community.service.OSSFileService;
import com.tanpu.community.util.ConvertUtil; import com.tanpu.community.util.ConvertUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
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;
...@@ -25,7 +26,7 @@ public class FileManager { ...@@ -25,7 +26,7 @@ public class FileManager {
@Autowired @Autowired
private OSSFileService ossFileService; private OSSFileService ossFileService;
public FileUploadResp uploadFile(MultipartFile file) { public FileUploadResp uploadFile(MultipartFile file, OssDirEnum dirEnum, String userId) {
if (file == null) { if (file == null) {
throw new BizException(ErrorCodeConstant.FILE_UPLOAD_FAIL); throw new BizException(ErrorCodeConstant.FILE_UPLOAD_FAIL);
} }
...@@ -42,7 +43,16 @@ public class FileManager { ...@@ -42,7 +43,16 @@ public class FileManager {
if (data.length == 0 || StringUtils.isEmpty(originalName)) { if (data.length == 0 || StringUtils.isEmpty(originalName)) {
throw new BizException("上传文件为空"); throw new BizException("上传文件为空");
} }
FileRecordEntity fileRecordEntity = ossFileService.uploadFile(data, originalName);
String dirPrefix = "";
if (dirEnum == null) {
dirPrefix = dirPrefix + dirEnum.dir;
}
if (StringUtils.isNotBlank(userId)) {
dirPrefix = dirPrefix + userId + "/";
}
FileRecordEntity fileRecordEntity = ossFileService.uploadFile(data, originalName, dirPrefix);
return ConvertUtil.fileRecordEntity2Resp(fileRecordEntity); return ConvertUtil.fileRecordEntity2Resp(fileRecordEntity);
} }
...@@ -51,9 +61,4 @@ public class FileManager { ...@@ -51,9 +61,4 @@ public class FileManager {
.collect(Collectors.toMap(FileRecordEntity::getFileId, FileRecordEntity::getUrl)); .collect(Collectors.toMap(FileRecordEntity::getFileId, FileRecordEntity::getUrl));
} }
@Cacheable(value = "local", key = "#id")
public String getId(String id) {
System.out.println("cache " + id);
return "haha " + id;
}
} }
...@@ -43,6 +43,7 @@ public class TopicManager { ...@@ -43,6 +43,7 @@ public class TopicManager {
} }
// 话题详情页 // 话题详情页
public TopicRankQo getDetail(String topicId) { public TopicRankQo getDetail(String topicId) {
//TODO 临时埋点,接入新埋点后删除 //TODO 临时埋点,接入新埋点后删除
visitLogService.addPageView(userHolder.getUserId(), topicId, COMM_VISIT_TOPIC_DETAIL); visitLogService.addPageView(userHolder.getUserId(), topicId, COMM_VISIT_TOPIC_DETAIL);
......
...@@ -58,27 +58,19 @@ public class OSSFileService { ...@@ -58,27 +58,19 @@ public class OSSFileService {
@Transactional @Transactional
public FileRecordEntity uploadFile(byte[] data, String fileName) { public FileRecordEntity uploadFile(byte[] data, String fileName, String dirPrefix) {
String fileSuffix = fileName.substring(fileName.lastIndexOf('.') + 1); String fileSuffix = fileName.substring(fileName.lastIndexOf('.') + 1);
return uploadFile(data, fileName, fileSuffix, null, OssRelType.None); return uploadFile(data, fileName, fileSuffix, dirPrefix);
} }
@Transactional @Transactional
public FileRecordEntity uploadFile(byte[] data, String fileName, String relId, OssRelType relType) { public FileRecordEntity uploadFile(byte[] data, String fileName, String fileSuffix, String dirPrefix) {
String fileSuffix = fileName.substring(fileName.lastIndexOf('.') + 1);
return uploadFile(data, fileName, fileSuffix, relId, relType);
}
@Transactional
public FileRecordEntity uploadFile(byte[] data, String fileName, String fileSuffix,
String relId, OssRelType relType) {
// todo uniqueLong
String[] arr = StringUtils.split(fileName, "."); String[] arr = StringUtils.split(fileName, ".");
String suffix = arr[arr.length - 1]; String suffix = arr[arr.length - 1];
if (FileTypeEnum.imageTypeSet.contains(suffix)) { if (FileTypeEnum.imageTypeSet.contains(suffix)) {
//上传 //上传
String id = uuidGenHelper.getUuidStr(); String id = uuidGenHelper.getUuidStr();
String key = CommunityConstant.OSS_PREFIX_FOLDER+id + "." + suffix; String key = CommunityConstant.OSS_PREFIX_FOLDER + dirPrefix + id + "." + suffix;
ossHelper.writeFile(bucketName, key, data, fileSuffix); ossHelper.writeFile(bucketName, key, data, fileSuffix);
//落库 //落库
...@@ -93,7 +85,6 @@ public class OSSFileService { ...@@ -93,7 +85,6 @@ public class OSSFileService {
HashMap<String, Integer> attr = getStringIntegerHashMap(data); HashMap<String, Integer> attr = getStringIntegerHashMap(data);
record.setExtInfo(JsonUtil.toJson(attr)); record.setExtInfo(JsonUtil.toJson(attr));
fileRecordMapper.insert(record); fileRecordMapper.insert(record);
return record; return record;
......
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