第二次课程提交
This commit is contained in:
parent
f5a0a1b00d
commit
dac4d3653f
|
@ -43,5 +43,10 @@
|
||||||
<artifactId>hutool-all</artifactId>
|
<artifactId>hutool-all</artifactId>
|
||||||
<version>5.7.22</version>
|
<version>5.7.22</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>transmittable-thread-local</artifactId>
|
||||||
|
<version>2.14.4</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -1,18 +1,22 @@
|
||||||
package com.atguigu.tingshu.common.util;
|
package com.atguigu.tingshu.common.util;
|
||||||
|
|
||||||
|
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前用户信息帮助类
|
* 获取当前用户信息帮助类
|
||||||
*/
|
*/
|
||||||
public class AuthContextHolder {
|
public class AuthContextHolder {
|
||||||
|
|
||||||
private static ThreadLocal<Long> userId = new ThreadLocal<Long>();
|
private static ThreadLocal<Long> userId = new TransmittableThreadLocal<Long>();
|
||||||
|
|
||||||
public static void setUserId(Long _userId) {
|
public static void setUserId(Long _userId) {
|
||||||
userId.set(_userId);
|
userId.set(_userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Long getUserId() {
|
public static Long getUserId() {
|
||||||
return userId.get();
|
//return userId.get();
|
||||||
|
//TODO 暂时硬编码为1,后续做完登录验证后再修改
|
||||||
|
return 1L;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void removeUserId() {
|
public static void removeUserId() {
|
||||||
|
|
Binary file not shown.
|
@ -1,8 +1,15 @@
|
||||||
package com.atguigu.tingshu.album.api;
|
package com.atguigu.tingshu.album.api;
|
||||||
|
|
||||||
import com.atguigu.tingshu.album.service.AlbumInfoService;
|
import com.atguigu.tingshu.album.service.AlbumInfoService;
|
||||||
|
import com.atguigu.tingshu.common.result.Result;
|
||||||
|
import com.atguigu.tingshu.common.util.AuthContextHolder;
|
||||||
|
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@ -16,5 +23,19 @@ public class AlbumInfoApiController {
|
||||||
private AlbumInfoService albumInfoService;
|
private AlbumInfoService albumInfoService;
|
||||||
|
|
||||||
|
|
||||||
|
@Operation(summary = "新建专辑信息")
|
||||||
|
@PostMapping("/albumInfo/saveAlbumInfo")
|
||||||
|
public Result saveAlbumInfo(@Validated @RequestBody AlbumInfoVo albumInfoVo) {
|
||||||
|
Long userId = AuthContextHolder.getUserId();
|
||||||
|
albumInfoService.saveAlbumInfo(albumInfoVo, userId);
|
||||||
|
return Result.ok();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,31 @@
|
||||||
package com.atguigu.tingshu.album.api;
|
package com.atguigu.tingshu.album.api;
|
||||||
|
|
||||||
|
|
||||||
|
import com.atguigu.tingshu.album.service.FileUploadService;
|
||||||
|
import com.atguigu.tingshu.common.result.Result;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
@Tag(name = "上传管理接口")
|
@Tag(name = "上传管理接口")
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("api/album")
|
@RequestMapping("api/album")
|
||||||
public class FileUploadApiController {
|
public class FileUploadApiController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private FileUploadService fileUploadService;
|
||||||
|
@Operation(summary = "上传文件")
|
||||||
|
@PostMapping("/fileUpload")
|
||||||
|
public Result<String> fileUpload(@RequestParam("file") MultipartFile file) throws IOException {
|
||||||
|
String url = fileUploadService.fileUpload(file);
|
||||||
|
|
||||||
|
return Result.ok(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package com.atguigu.tingshu.album.config;
|
package com.atguigu.tingshu.album.config;
|
||||||
|
|
||||||
|
import io.minio.MinioClient;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@ -13,4 +15,13 @@ public class MinioConstantProperties {
|
||||||
private String accessKey;
|
private String accessKey;
|
||||||
private String secreKey;
|
private String secreKey;
|
||||||
private String bucketName;
|
private String bucketName;
|
||||||
|
@Bean
|
||||||
|
public MinioClient minioClient() {
|
||||||
|
return
|
||||||
|
MinioClient.builder()
|
||||||
|
.endpoint(endpointUrl)
|
||||||
|
.credentials(accessKey, secreKey)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.atguigu.tingshu.album.service;
|
||||||
|
|
||||||
|
import com.atguigu.tingshu.model.album.AlbumAttributeValue;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
|
||||||
|
public interface AlbumAttributeValueService extends IService<AlbumAttributeValue> {
|
||||||
|
}
|
|
@ -1,9 +1,14 @@
|
||||||
package com.atguigu.tingshu.album.service;
|
package com.atguigu.tingshu.album.service;
|
||||||
|
|
||||||
import com.atguigu.tingshu.model.album.AlbumInfo;
|
import com.atguigu.tingshu.model.album.AlbumInfo;
|
||||||
|
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
|
||||||
public interface AlbumInfoService extends IService<AlbumInfo> {
|
public interface AlbumInfoService extends IService<AlbumInfo> {
|
||||||
|
|
||||||
|
|
||||||
|
//新建专辑信息
|
||||||
|
void saveAlbumInfo(AlbumInfoVo albumInfoVo, Long userId);
|
||||||
|
//保存专辑的状态
|
||||||
|
void saveAlbumInfoStat(Long albumId, String statType, int statNum);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.atguigu.tingshu.album.service;
|
||||||
|
|
||||||
|
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public interface FileUploadService {
|
||||||
|
String fileUpload(MultipartFile file) throws IOException;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.atguigu.tingshu.album.service.impl;
|
||||||
|
|
||||||
|
import com.atguigu.tingshu.album.mapper.AlbumAttributeValueMapper;
|
||||||
|
import com.atguigu.tingshu.album.service.AlbumAttributeValueService;
|
||||||
|
import com.atguigu.tingshu.model.album.AlbumAttributeValue;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AlbumAttributeValueServiceImpl extends ServiceImpl<AlbumAttributeValueMapper, AlbumAttributeValue> implements AlbumAttributeValueService {
|
||||||
|
}
|
|
@ -1,18 +1,88 @@
|
||||||
package com.atguigu.tingshu.album.service.impl;
|
package com.atguigu.tingshu.album.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
|
import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
|
||||||
|
import com.atguigu.tingshu.album.mapper.AlbumStatMapper;
|
||||||
|
import com.atguigu.tingshu.album.service.AlbumAttributeValueService;
|
||||||
import com.atguigu.tingshu.album.service.AlbumInfoService;
|
import com.atguigu.tingshu.album.service.AlbumInfoService;
|
||||||
|
import com.atguigu.tingshu.common.constant.SystemConstant;
|
||||||
|
import com.atguigu.tingshu.model.album.AlbumAttributeValue;
|
||||||
import com.atguigu.tingshu.model.album.AlbumInfo;
|
import com.atguigu.tingshu.model.album.AlbumInfo;
|
||||||
|
import com.atguigu.tingshu.model.album.AlbumStat;
|
||||||
|
import com.atguigu.tingshu.vo.album.AlbumAttributeValueVo;
|
||||||
|
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
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.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@SuppressWarnings({"all"})
|
|
||||||
public class AlbumInfoServiceImpl extends ServiceImpl<AlbumInfoMapper, AlbumInfo> implements AlbumInfoService {
|
public class AlbumInfoServiceImpl extends ServiceImpl<AlbumInfoMapper, AlbumInfo> implements AlbumInfoService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AlbumInfoMapper albumInfoMapper;
|
private AlbumInfoMapper albumInfoMapper;
|
||||||
|
@Autowired
|
||||||
|
private AlbumAttributeValueService albumAttributeValueService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AlbumStatMapper albumStatMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void saveAlbumInfo(AlbumInfoVo albumInfoVo, Long userId) {
|
||||||
|
//1.保存专辑信息
|
||||||
|
//1.1 将专辑VO(视图对象)对象转为专辑PO(持久化对象)对象
|
||||||
|
//将我们上传专辑的那个表中的数据转换成为albumInfo这个持久化对象数据里
|
||||||
|
//相当于把传上来的表变成一个你存大数据的表 albumInfo这表中有大量的数据 比如说专辑的状态 属性等
|
||||||
|
//不是这一个albumInfoVo表能装下的
|
||||||
|
|
||||||
|
//用这个工具要保证两个字段是相同的
|
||||||
|
AlbumInfo albumInfo= BeanUtil.copyProperties(albumInfoVo, AlbumInfo.class);
|
||||||
|
//1.2 封装PO对象中用户ID、免费试听集数、专辑状态
|
||||||
|
//这两张表只有这三个状态是不一样的
|
||||||
|
albumInfo.setUserId(userId);
|
||||||
|
albumInfo.setTracksForFree(5);
|
||||||
|
albumInfo.setStatus(SystemConstant.ALBUM_STATUS_NO_PASS);
|
||||||
|
//1.3 保存专辑,得到专辑ID
|
||||||
|
albumInfoMapper.insert(albumInfo);
|
||||||
|
Long albumId = albumInfo.getId();
|
||||||
|
//2.保存专辑标签信息
|
||||||
|
List<AlbumAttributeValueVo> albumAttributeValueVoList = albumInfoVo.getAlbumAttributeValueVoList();
|
||||||
|
|
||||||
|
if (CollUtil.isNotEmpty(albumAttributeValueVoList)) {
|
||||||
|
//2.1 将集合泛型从AlbumAttributeValueVo转为AlbumAttributeValue
|
||||||
|
//最一开始是vo类型 你通过stream将泛型
|
||||||
|
List<AlbumAttributeValue> attributeValueList = albumAttributeValueVoList
|
||||||
|
.stream()
|
||||||
|
.map(vo -> {
|
||||||
|
AlbumAttributeValue albumAttributeValue = BeanUtil.copyProperties(vo, AlbumAttributeValue.class);
|
||||||
|
//2.2 为每个专辑标签关系 关联 专辑ID
|
||||||
|
albumAttributeValue.setAlbumId(albumId);
|
||||||
|
return albumAttributeValue;
|
||||||
|
}).collect(Collectors.toList());
|
||||||
|
//2.3 批量保存
|
||||||
|
albumAttributeValueService.saveBatch(attributeValueList);
|
||||||
|
}
|
||||||
|
this.saveAlbumInfoStat(albumId, SystemConstant.ALBUM_STAT_PLAY, 0);
|
||||||
|
this.saveAlbumInfoStat(albumId, SystemConstant.ALBUM_STAT_SUBSCRIBE, 0);
|
||||||
|
this.saveAlbumInfoStat(albumId, SystemConstant.ALBUM_STAT_BUY, 0);
|
||||||
|
this.saveAlbumInfoStat(albumId, SystemConstant.ALBUM_STAT_COMMENT, 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void saveAlbumInfoStat(Long albumId, String statType, int statNum) {
|
||||||
|
AlbumStat albumStat = new AlbumStat();
|
||||||
|
albumStat.setAlbumId(albumId);
|
||||||
|
albumStat.setStatType(statType);
|
||||||
|
albumStat.setStatNum(statNum);
|
||||||
|
albumStatMapper.insert(albumStat);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
package com.atguigu.tingshu.album.service.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import com.atguigu.tingshu.album.config.MinioConstantProperties;
|
||||||
|
import com.atguigu.tingshu.album.service.FileUploadService;
|
||||||
|
import com.atguigu.tingshu.common.execption.GuiguException;
|
||||||
|
import io.minio.MinioClient;
|
||||||
|
import io.minio.PutObjectArgs;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.UUID;
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class FileUploadServiceImpl implements FileUploadService {
|
||||||
|
@Autowired
|
||||||
|
private MinioClient minioClient;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MinioConstantProperties minioConstantProperties;
|
||||||
|
@Override
|
||||||
|
public String fileUpload(MultipartFile file) throws IOException {
|
||||||
|
|
||||||
|
|
||||||
|
//1.业务校验 检验图片是否合法
|
||||||
|
BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
|
||||||
|
if (bufferedImage == null){
|
||||||
|
throw new GuiguException(500,"上传文件类型错误");
|
||||||
|
}
|
||||||
|
//2.检查图片上传的大小是否合理
|
||||||
|
int width = bufferedImage.getWidth();
|
||||||
|
int height = bufferedImage.getHeight();
|
||||||
|
if(width>900||height>900){
|
||||||
|
throw new GuiguException(500,"上传图片过大");
|
||||||
|
}
|
||||||
|
//3.将图片文件上传MINIO 规范:日期/文件唯一命名.后缀
|
||||||
|
//3.1 生成日期作为文件夹名称
|
||||||
|
String folderName = DateUtil.today();
|
||||||
|
//3.2 生成文件唯一名称
|
||||||
|
String extName = FileUtil.extName(file.getOriginalFilename());
|
||||||
|
String objectName = "/" + folderName + "/" + UUID.randomUUID().toString() + "." + extName;
|
||||||
|
//3.上传文件
|
||||||
|
try {
|
||||||
|
minioClient.putObject(
|
||||||
|
PutObjectArgs.builder().bucket(minioConstantProperties.getBucketName()).object(objectName).stream(
|
||||||
|
file.getInputStream(), file.getSize(), -1)
|
||||||
|
.contentType(file.getContentType())
|
||||||
|
.build());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("上传文件到MINIO失败:{}", e.getMessage());
|
||||||
|
throw new GuiguException(500, "上传文件到MINIO失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//4.拼接MINIO文件在线地址
|
||||||
|
return minioConstantProperties.getEndpointUrl() + "/" + minioConstantProperties.getBucketName() + objectName;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue