Skip to content

Commit

Permalink
Merge pull request #117 from jamebal/recycle
Browse files Browse the repository at this point in the history
feat: 新增功能-回收站
  • Loading branch information
jamebal authored Jul 7, 2024
2 parents 23d1d21 + b748e28 commit c5735f6
Show file tree
Hide file tree
Showing 14 changed files with 426 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,40 @@ public ResponseResult<Object> delete(@RequestParam String username, @RequestPara
}
}

@Operation(summary = "返回原处")
@PostMapping("/restore")
@LogOperatingFun
@Permission("cloud:file:upload")
public ResponseResult<Object> restore(@RequestParam String[] fileIds) {
if (fileIds != null && fileIds.length > 0) {
List<String> list = Arrays.asList(fileIds);
return fileService.restore(list, userLoginHolder.getUsername());
} else {
throw new CommonException(ExceptionType.MISSING_PARAMETERS.getCode(), ExceptionType.MISSING_PARAMETERS.getMsg());
}
}

@Operation(summary = "彻底删除")
@DeleteMapping("/sweep")
@LogOperatingFun
@Permission("cloud:file:delete")
public ResponseResult<Object> sweep(@RequestParam String[] fileIds) {
if (fileIds != null && fileIds.length > 0) {
List<String> list = Arrays.asList(fileIds);
return fileService.sweep(list, userLoginHolder.getUsername());
} else {
throw new CommonException(ExceptionType.MISSING_PARAMETERS.getCode(), ExceptionType.MISSING_PARAMETERS.getMsg());
}
}

@Operation(summary = "清空回收站")
@DeleteMapping("/clear-trash")
@LogOperatingFun
@Permission("cloud:file:delete")
public ResponseResult<Object> clearTrash() {
return fileService.clearTrash(userLoginHolder.getUsername());
}

@Operation(summary = "重命名")
@GetMapping("/rename")
@LogOperatingFun
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/com/jmal/clouddisk/exception/CommonException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jmal.clouddisk.exception;

import lombok.Getter;
import org.springframework.data.annotation.Transient;

import java.io.Serial;
Expand All @@ -10,6 +11,7 @@
* @Date 2019-08-15 10:54
* @author jmal
*/
@Getter
public class CommonException extends RuntimeException {

/**
Expand Down Expand Up @@ -47,16 +49,8 @@ public CommonException(String msg) {
this.data = null;
}

public int getCode() {
return code;
}

public String getMsg() {
public String getMessage() {
return msg;
}

public Object getData() {
return data;
}

}
78 changes: 62 additions & 16 deletions src/main/java/com/jmal/clouddisk/interceptor/FileInterceptor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.jmal.clouddisk.interceptor;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.net.URLDecoder;
import cn.hutool.core.text.CharSequenceUtil;
Expand Down Expand Up @@ -31,11 +30,9 @@
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.util.UriUtils;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.*;
import javax.imageio.stream.FileCacheImageOutputStream;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
Expand All @@ -44,6 +41,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;

/**
* @author jmal
Expand Down Expand Up @@ -354,40 +352,88 @@ private void responseHeader(HttpServletResponse response, String fileName, byte[
* @return 剪裁后的文件
*/
public static byte[] imageCrop(File srcFile, String q, String w, String h) {
ByteArrayOutputStream out = null;
ImageInputStream iis = null;
try {
Thumbnails.Builder<? extends File> thumbnail = Thumbnails.of(srcFile);
//获取图片信息
BufferedImage bim = ImageIO.read(srcFile);
// 使用ImageInputStream来处理大文件
iis = ImageIO.createImageInputStream(srcFile);
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
if (!readers.hasNext()) {
return new byte[0];
}
ImageReader reader = readers.next();
reader.setInput(iis, true);

// 获取图像元数据
BufferedImage bim = reader.read(0);
if (bim == null) {
return new byte[0];
}

int srcWidth = bim.getWidth();
int srcHeight = bim.getHeight();
double quality = Convert.toDouble(q, 0.8);
if (quality >= 0 && quality <= 1) {
thumbnail.outputQuality(quality);
}
int width = Convert.toInt(w, -1);
int height = Convert.toInt(h, -1);

// 处理质量参数
double quality = parseQuality(q, 0.8);
int width = parseDimension(w, -1);
int height = parseDimension(h, -1);

Thumbnails.Builder<? extends BufferedImage> thumbnail = Thumbnails.of(bim)
.outputFormat("jpg") // 指定输出格式
.outputQuality(quality);

if (width > 0 && srcWidth > width) {
if (height <= 0 || srcHeight <= height) {
height = (int) (width / (double) srcWidth * srcHeight);
height = height == 0 ? width : height;
}
thumbnail.size(width, height);
} else {
//宽高均小,指定原大小
// 宽高均小,指定原大小
thumbnail.size(srcWidth, srcHeight);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();

out = new ByteArrayOutputStream();
thumbnail.toOutputStream(out);
return out.toByteArray();
} catch (UnsupportedFormatException e) {
log.warn(e.getMessage(), e);
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
log.error("Error closing ByteArrayOutputStream", e);
}
}
if (iis != null) {
try {
iis.close();
} catch (IOException e) {
log.error("Error closing ImageInputStream", e);
}
}
}
return new byte[0];
}

private static double parseQuality(String q, double defaultQuality) {
try {
double quality = Double.parseDouble(q);
return (quality >= 0 && quality <= 1) ? quality : defaultQuality;
} catch (NumberFormatException e) {
return defaultQuality;
}
}

private static int parseDimension(String dim, int defaultValue) {
try {
return Integer.parseInt(dim);
} catch (NumberFormatException e) {
return defaultValue;
}
}

}
29 changes: 29 additions & 0 deletions src/main/java/com/jmal/clouddisk/model/FileDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ public class FileDocument extends FileBase {
* m3u8文件路径(相对路径)
*/
private String m3u8;
/**
* vtt文件路径(相对路径)
*/
private String vtt;
/***
* 是否发布,适用于文档类型
*/
Expand Down Expand Up @@ -213,4 +217,29 @@ public int hashCode() {
hash = 89 * hash + (this.id != null ? this.id.hashCode() : 0);
return hash;
}

public Trash toTrash(boolean hidden) {
Trash trash = new Trash();
trash.setId(this.getId());
trash.setName(this.getName());
trash.setPath(this.getPath());
trash.setUserId(this.getUserId());
trash.setH(this.getH());
trash.setW(this.getW());
trash.setSuffix(this.getSuffix());
trash.setIsFolder(this.getIsFolder());
trash.setContent(this.getContent());
trash.setExif(this.getExif());
trash.setMusic(this.getMusic());
trash.setVideo(this.getVideo());
trash.setContentType(this.getContentType());
trash.setSize(this.getSize());
trash.setUploadDate(this.getUploadDate());
trash.setUpdateDate(this.getUpdateDate());
trash.setMd5(this.getMd5());
trash.setM3u8(this.getM3u8());
trash.setVtt(this.getVtt());
trash.setHidden(hidden);
return trash;
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/jmal/clouddisk/model/OperationTips.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.jmal.clouddisk.model;

import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
@Builder
public class OperationTips {
/**
* 操作结果说明
*/
String msg;
/**
* 操作结果状态
*/
Boolean success;
/**
* 操作
*/
String operation;
}
87 changes: 87 additions & 0 deletions src/main/java/com/jmal/clouddisk/model/Trash.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.jmal.clouddisk.model;

import com.jmal.clouddisk.service.impl.FileServiceImpl;
import com.jmal.clouddisk.video.VideoInfoDO;
import lombok.Data;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.CompoundIndexes;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Objects;

/**
* Trash 文件模型
*
* @author jmal
*/
@Data
@Document(collection = FileServiceImpl.TRASH_COLLECTION_NAME)
@CompoundIndexes({
@CompoundIndex(name = "name_1", def = "{'name': 1}"),
@CompoundIndex(name = "size_1", def = "{'size': 1}"),
@CompoundIndex(name = "updateDate_1", def = "{'updateDate': 1}"),
@CompoundIndex(name = "hidden_1", def = "{'hidden': 1}"),
})
public class Trash extends FileBase {
private String userId;
private String path;
/**
* 图片的宽度
*/
private String w;
/**
* 图片的高度
*/
private String h;
/**
* 文件内容
*/
private byte[] content;
/***
* 文件后缀名
*/
private String suffix;
/**
* 音乐
*/
private Music music;
/**
* 照片exif信息
*/
private ExifInfo exif;
/**
* 视频信息
*/
private VideoInfoDO video;
/**
* m3u8文件路径(相对路径)
*/
private String m3u8;
/**
* vtt文件路径(相对路径)
*/
private String vtt;
/**
* 是否隐藏显示
*/
private boolean hidden;

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final FileBase other = (FileBase) obj;
return Objects.equals(this.id, other.id);
}

@Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + (this.id != null ? this.id.hashCode() : 0);
return hash;
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/jmal/clouddisk/model/UploadApiParamDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ public class UploadApiParamDTO {
* tagId
*/
String tagId;
/**
* 是否在回收站
*/
Boolean isTrash;
/***
* 封面
*/
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/jmal/clouddisk/oss/minio/MinIOService.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,13 @@ public List<FileInfo> getFileInfoList(String objectName) {
private List<FileInfo> setLastModified(List<FileInfo> fileInfoList) {
fileInfoList = fileInfoList.parallelStream().peek(fileInfo -> {
if (fileInfo.getLastModified() == null) {
Date date = new Date(0);
try {
Date date = getLastModified(fileInfo.getKey());
fileInfo.setLastModified(date);
date = getLastModified(fileInfo.getKey());
} catch (Exception e) {
log.error(e.getMessage(), e);
log.warn("getLastModified Failed: {}, key: {}", e.getMessage(), fileInfo.getKey());
}
fileInfo.setLastModified(date);
}
}).collect(Collectors.toList());
return fileInfoList;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/jmal/clouddisk/service/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ private Constants() { }

public static final String OPERATION_FILE = "operationFile";

public static final String OPERATION_TIPS = "operationTips";

public static final String UPLOADER_CHUNK_SIZE = "uploaderChunkSize";

public static final int OSS_CHUNK_SIZE = 5 * 1024 * 1024;
Expand Down
Loading

0 comments on commit c5735f6

Please sign in to comment.