Move background to database.

hikari
PeterAlbus 12 months ago
parent a652927c9b
commit c11ec55cbf

@ -0,0 +1,75 @@
package com.peteralbus.controller;
import com.peteralbus.domain.BackgroundImage;
import com.peteralbus.domain.Result;
import com.peteralbus.service.BackgroundImageService;
import com.peteralbus.service.PhotoService;
import com.peteralbus.service.impl.PhotoServiceImpl;
import com.peteralbus.util.ResultUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RestController
@CrossOrigin
@RequestMapping("/background")
public class BackgroundImageController {
private final BackgroundImageService backgroundImageService;
private final PhotoService photoService;
@RequestMapping("/queryAll")
public Result<List<BackgroundImage>> queryAll() {
return ResultUtil.success(backgroundImageService.queryAll());
}
@RequestMapping("/queryById")
public Result<BackgroundImage> queryById(Long backgroundId) {
return ResultUtil.success(backgroundImageService.queryById(backgroundId));
}
@PostMapping("/add")
public Result<?> add(@RequestParam("file") MultipartFile file, String description) {
BackgroundImage backgroundImage = new BackgroundImage();
if(file.getSize() > 1024 * 1024 * 512) {
return ResultUtil.error(500,"上传失败文件大小超过512MB");
}
Map<String,String> savedUrl = photoService.savePhoto(file, "blog/static/background", false);
backgroundImage.setBackgroundUrl(savedUrl.get("url"));
backgroundImage.setBackgroundPath(savedUrl.get("url").replace(PhotoServiceImpl.BASE_URL,PhotoServiceImpl.BASE_PATH));
int result = backgroundImageService.add(backgroundImage);
if (result == 0) {
return ResultUtil.error(500, "添加失败");
}
return ResultUtil.success(null);
}
@PostMapping("/update")
public Result<?> update(Long backgroundId, String description) {
BackgroundImage backgroundImage = backgroundImageService.queryById(backgroundId);
backgroundImage.setBackgroundDescription(description);
int result = backgroundImageService.update(backgroundImage);
if (result == 0) {
return ResultUtil.error(500, "更新失败");
}
return ResultUtil.success(null);
}
@PostMapping("/delete")
public Result<?> delete(Long backgroundId) {
BackgroundImage backgroundImage = backgroundImageService.queryById(backgroundId);
Result<?> result = photoService.deletePhotoByUrl(backgroundImage.getBackgroundPath());
if (result.getCode() != 200) {
return result;
}
int infoResult = backgroundImageService.delete(backgroundId);
if (infoResult == 0) {
return ResultUtil.error(500, "数据库信息删除失败");
}
return ResultUtil.success(null);
}
}

@ -49,7 +49,7 @@ public class PhotoController {
@PostMapping("/uploadOriginImg")
public Result<?> uploadOriginImg(@RequestParam("file") MultipartFile file, String path) {
/*pathExample:blog/imgs/photo/*/
// 文件大于1GB不允许上传
// 文件大于0.5GB不允许上传
if(file.getSize() > 1024 * 1024 * 512) {
return ResultUtil.error(500,"上传失败文件大小超过512MB");
}

@ -0,0 +1,42 @@
package com.peteralbus.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
/**
* The type Background image.
* Store background image information of blog.
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class BackgroundImage {
/**
* The Background id.
*/
@TableId(type= IdType.ASSIGN_ID)
@JsonFormat(shape = JsonFormat.Shape.STRING)
Long backgroundId;
/**
* The Background url.
*/
String backgroundUrl;
/**
* The Background path.
*/
String backgroundPath;
/**
* The Background description.
*/
String backgroundDescription;
/**
* Whether put it into random list.
*/
Boolean isShow;
}

@ -0,0 +1,12 @@
package com.peteralbus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.peteralbus.domain.BackgroundImage;
import org.apache.ibatis.annotations.Mapper;
/**
* The interface Background image mapper.
*/
@Mapper
public interface BackgroundImageMapper extends BaseMapper<BackgroundImage> {
}

@ -0,0 +1,49 @@
package com.peteralbus.service;
import com.peteralbus.domain.BackgroundImage;
import java.util.List;
/**
* The interface Background image service.
*/
public interface BackgroundImageService {
/**
* Query all list.
*
* @return the list
*/
List<BackgroundImage> queryAll();
/**
* Add int.
*
* @param backgroundImage the background image
* @return the int
*/
int add(BackgroundImage backgroundImage);
/**
* Update int.
*
* @param backgroundImage the background image
* @return the int
*/
int update(BackgroundImage backgroundImage);
/**
* Delete int.
*
* @param backgroundId the background id
* @return the int
*/
int delete(Long backgroundId);
/**
* Query by id background image.
*
* @param backgroundId the background id
* @return the background image
*/
BackgroundImage queryById(Long backgroundId);
}

@ -0,0 +1,41 @@
package com.peteralbus.service.impl;
import com.peteralbus.domain.BackgroundImage;
import com.peteralbus.mapper.BackgroundImageMapper;
import com.peteralbus.service.BackgroundImageService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BackgroundImageServiceImpl implements BackgroundImageService {
private final BackgroundImageMapper backgroundImageMapper;
@Override
public List<BackgroundImage> queryAll() {
return backgroundImageMapper.selectList(null);
}
@Override
public int add(BackgroundImage backgroundImage) {
return backgroundImageMapper.insert(backgroundImage);
}
@Override
public int update(BackgroundImage backgroundImage) {
return backgroundImageMapper.updateById(backgroundImage);
}
@Override
public int delete(Long backgroundId) {
return backgroundImageMapper.deleteById(backgroundId);
}
@Override
public BackgroundImage queryById(Long backgroundId) {
return backgroundImageMapper.selectById(backgroundId);
}
}

@ -27,8 +27,8 @@ import java.util.UUID;
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PhotoServiceImpl implements PhotoService
{
static final String BASE_PATH = "/home/PeterAlbus/assets/";
static final String BASE_URL = "https://file.peteralbus.com/assets/";
public static final String BASE_PATH = "/home/PeterAlbus/assets/";
public static final String BASE_URL = "https://file.peteralbus.com/assets/";
private final PhotoMapper photoMapper;
@Override
public List<Photo> queryAll()

Loading…
Cancel
Save