mirror of https://github.com/elunez/eladmin
去除免费图床,需要使用的请前往:https://sm.ms/
parent
554aa58c40
commit
467e04adca
|
@ -1,53 +0,0 @@
|
|||
package me.zhengjie.domain;
|
||||
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
/**
|
||||
* sm.ms图床
|
||||
*
|
||||
* @author Zheng Jie
|
||||
* @date 2018-12-27
|
||||
*/
|
||||
@Data
|
||||
@Entity
|
||||
@Table(name = "picture")
|
||||
public class Picture implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String filename;
|
||||
|
||||
private String url;
|
||||
|
||||
private String size;
|
||||
|
||||
private String height;
|
||||
|
||||
private String width;
|
||||
|
||||
@Column(name = "delete_url")
|
||||
private String delete;
|
||||
|
||||
private String username;
|
||||
|
||||
@CreationTimestamp
|
||||
@Column(name = "create_time")
|
||||
private Timestamp createTime;
|
||||
|
||||
/** 用于检测文件是否重复 */
|
||||
private String md5Code;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Picture{" +
|
||||
"filename='" + filename + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package me.zhengjie.repository;
|
||||
|
||||
import me.zhengjie.domain.Picture;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-12-27
|
||||
*/
|
||||
public interface PictureRepository extends JpaRepository<Picture,Long>, JpaSpecificationExecutor<Picture> {
|
||||
|
||||
/**
|
||||
* 根据 Mds 值查询文件
|
||||
* @param code 值
|
||||
* @return /
|
||||
*/
|
||||
Picture findByMd5Code(String code);
|
||||
}
|
|
@ -1,84 +0,0 @@
|
|||
package me.zhengjie.rest;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.domain.Picture;
|
||||
import me.zhengjie.service.PictureService;
|
||||
import me.zhengjie.service.dto.PictureQueryCriteria;
|
||||
import me.zhengjie.utils.SecurityUtils;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 郑杰
|
||||
* @date 2018/09/20 14:13:32
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/pictures")
|
||||
@Api(tags = "工具:免费图床管理")
|
||||
public class PictureController {
|
||||
|
||||
private final PictureService pictureService;
|
||||
|
||||
public PictureController(PictureService pictureService) {
|
||||
this.pictureService = pictureService;
|
||||
}
|
||||
|
||||
@Log("查询图片")
|
||||
@PreAuthorize("@el.check('pictures:list')")
|
||||
@GetMapping
|
||||
@ApiOperation("查询图片")
|
||||
public ResponseEntity<Object> getRoles(PictureQueryCriteria criteria, Pageable pageable){
|
||||
return new ResponseEntity<>(pictureService.queryAll(criteria,pageable),HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Log("导出数据")
|
||||
@ApiOperation("导出数据")
|
||||
@GetMapping(value = "/download")
|
||||
@PreAuthorize("@el.check('pictures:list')")
|
||||
public void download(HttpServletResponse response, PictureQueryCriteria criteria) throws IOException {
|
||||
pictureService.download(pictureService.queryAll(criteria), response);
|
||||
}
|
||||
|
||||
@Log("上传图片")
|
||||
@PreAuthorize("@el.check('pictures:add')")
|
||||
@PostMapping
|
||||
@ApiOperation("上传图片")
|
||||
public ResponseEntity<Object> upload(@RequestParam MultipartFile file){
|
||||
String userName = SecurityUtils.getUsername();
|
||||
Picture picture = pictureService.upload(file,userName);
|
||||
Map<String,Object> map = new HashMap<>(3);
|
||||
map.put("errno",0);
|
||||
map.put("id",picture.getId());
|
||||
map.put("data",new String[]{picture.getUrl()});
|
||||
return new ResponseEntity<>(map,HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Log("删除图片")
|
||||
@ApiOperation("删除图片")
|
||||
@PreAuthorize("@el.check('pictures:del')")
|
||||
@DeleteMapping(value = "/{id}")
|
||||
public ResponseEntity<Object> delete(@PathVariable Long id) {
|
||||
pictureService.delete(pictureService.findById(id));
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Log("多选删除图片")
|
||||
@ApiOperation("多选删除图片")
|
||||
@PreAuthorize("@el.check('pictures:del')")
|
||||
@DeleteMapping
|
||||
public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
|
||||
pictureService.deleteAll(ids);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
}
|
|
@ -1,67 +0,0 @@
|
|||
package me.zhengjie.service;
|
||||
|
||||
import me.zhengjie.domain.Picture;
|
||||
import me.zhengjie.service.dto.PictureQueryCriteria;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-12-27
|
||||
*/
|
||||
public interface PictureService {
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
* @param criteria 条件
|
||||
* @param pageable 分页参数
|
||||
* @return /
|
||||
*/
|
||||
Object queryAll(PictureQueryCriteria criteria, Pageable pageable);
|
||||
|
||||
/**
|
||||
* 查询全部数据
|
||||
* @param criteria 条件
|
||||
* @return /
|
||||
*/
|
||||
List<Picture> queryAll(PictureQueryCriteria criteria);
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param file /
|
||||
* @param username /
|
||||
* @return /
|
||||
*/
|
||||
Picture upload(MultipartFile file, String username);
|
||||
|
||||
/**
|
||||
* 根据ID查询
|
||||
* @param id /
|
||||
* @return /
|
||||
*/
|
||||
Picture findById(Long id);
|
||||
|
||||
/**
|
||||
* 删除图片
|
||||
* @param picture /
|
||||
*/
|
||||
void delete(Picture picture);
|
||||
|
||||
/**
|
||||
* 多选删除
|
||||
* @param ids /
|
||||
*/
|
||||
void deleteAll(Long[] ids);
|
||||
|
||||
/**
|
||||
* 导出
|
||||
* @param queryAll 待导出的数据
|
||||
* @param response /
|
||||
* @throws IOException /
|
||||
*/
|
||||
void download(List<Picture> queryAll, HttpServletResponse response) throws IOException;
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
package me.zhengjie.service.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import me.zhengjie.annotation.Query;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* sm.ms图床
|
||||
*
|
||||
* @author Zheng Jie
|
||||
* @date 2019-6-4 09:52:09
|
||||
*/
|
||||
@Data
|
||||
public class PictureQueryCriteria{
|
||||
|
||||
@Query(type = Query.Type.INNER_LIKE)
|
||||
private String filename;
|
||||
|
||||
@Query(type = Query.Type.INNER_LIKE)
|
||||
private String username;
|
||||
|
||||
@Query(type = Query.Type.BETWEEN)
|
||||
private List<Timestamp> createTime;
|
||||
}
|
|
@ -1,135 +0,0 @@
|
|||
package me.zhengjie.service.impl;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.domain.Picture;
|
||||
import me.zhengjie.repository.PictureRepository;
|
||||
import me.zhengjie.service.PictureService;
|
||||
import me.zhengjie.service.dto.PictureQueryCriteria;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.utils.*;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-12-27
|
||||
*/
|
||||
@Slf4j
|
||||
@Service(value = "pictureService")
|
||||
@CacheConfig(cacheNames = "picture")
|
||||
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
|
||||
public class PictureServiceImpl implements PictureService {
|
||||
|
||||
private final PictureRepository pictureRepository;
|
||||
|
||||
private static final String SUCCESS = "success";
|
||||
|
||||
private static final String CODE = "code";
|
||||
|
||||
private static final String MSG = "message";
|
||||
|
||||
public PictureServiceImpl(PictureRepository pictureRepository) {
|
||||
this.pictureRepository = pictureRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable
|
||||
public Object queryAll(PictureQueryCriteria criteria, Pageable pageable){
|
||||
return PageUtil.toPage(pictureRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Picture> queryAll(PictureQueryCriteria criteria) {
|
||||
return pictureRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
|
||||
}
|
||||
|
||||
@Override
|
||||
@CacheEvict(allEntries = true)
|
||||
@Transactional(rollbackFor = Throwable.class)
|
||||
public Picture upload(MultipartFile multipartFile, String username) {
|
||||
File file = FileUtil.toFile(multipartFile);
|
||||
// 验证是否重复上传
|
||||
Picture picture = pictureRepository.findByMd5Code(FileUtil.getMd5(file));
|
||||
if(picture != null){
|
||||
return picture;
|
||||
}
|
||||
HashMap<String, Object> paramMap = new HashMap<>(1);
|
||||
paramMap.put("smfile", file);
|
||||
String result= HttpUtil.post(ElAdminConstant.Url.SM_MS_URL, paramMap);
|
||||
JSONObject jsonObject = JSONUtil.parseObj(result);
|
||||
if(!jsonObject.get(CODE).toString().equals(SUCCESS)){
|
||||
throw new BadRequestException(TranslatorUtil.translate(jsonObject.get(MSG).toString()));
|
||||
}
|
||||
picture = JSON.parseObject(jsonObject.get("data").toString(), Picture.class);
|
||||
picture.setSize(FileUtil.getSize(Integer.parseInt(picture.getSize())));
|
||||
picture.setUsername(username);
|
||||
picture.setMd5Code(FileUtil.getMd5(file));
|
||||
picture.setFilename(FileUtil.getFileNameNoEx(multipartFile.getOriginalFilename())+"."+FileUtil.getExtensionName(multipartFile.getOriginalFilename()));
|
||||
pictureRepository.save(picture);
|
||||
//删除临时文件
|
||||
FileUtil.del(file);
|
||||
return picture;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "#p0")
|
||||
public Picture findById(Long id) {
|
||||
Picture picture = pictureRepository.findById(id).orElseGet(Picture::new);
|
||||
ValidationUtil.isNull(picture.getId(),"Picture","id",id);
|
||||
return picture;
|
||||
}
|
||||
|
||||
@Override
|
||||
@CacheEvict(allEntries = true)
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(Picture picture) {
|
||||
try {
|
||||
HttpUtil.get(picture.getDelete());
|
||||
pictureRepository.delete(picture);
|
||||
} catch(Exception e){
|
||||
pictureRepository.delete(picture);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@CacheEvict(allEntries = true)
|
||||
public void deleteAll(Long[] ids) {
|
||||
for (Long id : ids) {
|
||||
delete(findById(id));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void download(List<Picture> queryAll, HttpServletResponse response) throws IOException {
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
for (Picture picture : queryAll) {
|
||||
Map<String,Object> map = new LinkedHashMap<>();
|
||||
map.put("文件名", picture.getFilename());
|
||||
map.put("图片地址", picture.getUrl());
|
||||
map.put("文件大小", picture.getSize());
|
||||
map.put("操作人", picture.getUsername());
|
||||
map.put("高度", picture.getHeight());
|
||||
map.put("宽度", picture.getWidth());
|
||||
map.put("删除地址", picture.getDelete());
|
||||
map.put("创建日期", picture.getCreateTime());
|
||||
list.add(map);
|
||||
}
|
||||
FileUtil.downloadExcel(list, response);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue