forked from pnhq5agf9/poetize
parent
cb46d3ca98
commit
eff348f031
@ -0,0 +1,54 @@
|
||||
package com.ld.poetry.utils.storage;
|
||||
|
||||
import com.ld.poetry.entity.User;
|
||||
import com.ld.poetry.utils.CommonConst;
|
||||
import com.ld.poetry.utils.PoetryCache;
|
||||
import com.ld.poetry.utils.PoetryUtil;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Component
|
||||
public class FileFilter {
|
||||
|
||||
private final AntPathMatcher matcher = new AntPathMatcher();
|
||||
|
||||
public boolean doFilterFile(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
|
||||
if (matcher.match("/resource/upload", httpServletRequest.getRequestURI())) {
|
||||
String token = PoetryUtil.getToken();
|
||||
if (StringUtils.hasText(token)) {
|
||||
User user = (User) PoetryCache.get(token);
|
||||
|
||||
if (user != null) {
|
||||
if (user.getId().intValue() == PoetryUtil.getAdminUser().getId().intValue()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
AtomicInteger atomicInteger = (AtomicInteger) PoetryCache.get(CommonConst.SAVE_COUNT_USER_ID + user.getId().toString());
|
||||
if (atomicInteger == null) {
|
||||
atomicInteger = new AtomicInteger();
|
||||
PoetryCache.put(CommonConst.SAVE_COUNT_USER_ID + user.getId().toString(), atomicInteger, CommonConst.SAVE_EXPIRE);
|
||||
}
|
||||
int userIdCount = atomicInteger.getAndIncrement();
|
||||
|
||||
String ip = PoetryUtil.getIpAddr(PoetryUtil.getRequest());
|
||||
AtomicInteger atomic = (AtomicInteger) PoetryCache.get(CommonConst.SAVE_COUNT_IP + ip);
|
||||
if (atomic == null) {
|
||||
atomic = new AtomicInteger();
|
||||
PoetryCache.put(CommonConst.SAVE_COUNT_IP + ip, atomic, CommonConst.SAVE_EXPIRE);
|
||||
}
|
||||
int ipCount = atomic.getAndIncrement();
|
||||
|
||||
return userIdCount >= CommonConst.SAVE_MAX_COUNT || ipCount >= CommonConst.SAVE_MAX_COUNT;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,101 @@
|
||||
package com.ld.poetry.utils.storage;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.ld.poetry.entity.Resource;
|
||||
import com.ld.poetry.handle.PoetryRuntimeException;
|
||||
import com.ld.poetry.service.ResourceService;
|
||||
import com.ld.poetry.utils.StringUtil;
|
||||
import com.ld.poetry.vo.FileVO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@ConditionalOnProperty(name = "local.enable", havingValue = "true")
|
||||
public class LocalUtil implements StoreService {
|
||||
|
||||
@Value("${local.uploadUrl}")
|
||||
private String uploadUrl;
|
||||
|
||||
@Value("${local.downloadUrl}")
|
||||
private String downloadUrl;
|
||||
|
||||
@Autowired
|
||||
private ResourceService resourceService;
|
||||
|
||||
@Override
|
||||
public void deleteFile(List<String> files) {
|
||||
if (CollectionUtils.isEmpty(files)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (String filePath : files) {
|
||||
File file = new File(filePath.replace(downloadUrl, uploadUrl));
|
||||
if (file.exists() && file.isFile()) {
|
||||
if (file.delete()) {
|
||||
log.info("文件删除成功:" + filePath);
|
||||
resourceService.lambdaUpdate().eq(Resource::getPath, filePath).remove();
|
||||
} else {
|
||||
log.error("文件删除失败:" + filePath);
|
||||
}
|
||||
} else {
|
||||
log.error("文件不存在或者不是一个文件:" + filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVO saveFile(FileVO fileVO) {
|
||||
if (!StringUtils.hasText(fileVO.getRelativePath()) ||
|
||||
fileVO.getRelativePath().startsWith("/") ||
|
||||
fileVO.getRelativePath().endsWith("/")) {
|
||||
throw new PoetryRuntimeException("文件路径不合法!");
|
||||
}
|
||||
|
||||
String path = fileVO.getRelativePath();
|
||||
if (path.contains("/")) {
|
||||
String[] split = path.split("/");
|
||||
if (split.length > 5) {
|
||||
throw new PoetryRuntimeException("文件路径不合法!");
|
||||
}
|
||||
for (int i = 0; i < split.length - 1; i++) {
|
||||
if (!StringUtil.isValidDirectoryName(split[i])) {
|
||||
throw new PoetryRuntimeException("文件路径不合法!");
|
||||
}
|
||||
}
|
||||
if (!StringUtil.isValidFileName(split[split.length - 1])) {
|
||||
throw new PoetryRuntimeException("文件路径不合法!");
|
||||
}
|
||||
}
|
||||
String absolutePath = uploadUrl + path;
|
||||
if (FileUtil.exist(absolutePath)) {
|
||||
throw new PoetryRuntimeException("文件已存在!");
|
||||
}
|
||||
try {
|
||||
File newFile = FileUtil.touch(absolutePath);
|
||||
fileVO.getFile().transferTo(newFile);
|
||||
FileVO result = new FileVO();
|
||||
result.setAbsolutePath(absolutePath);
|
||||
result.setVisitPath(downloadUrl + path);
|
||||
return result;
|
||||
} catch (IOException e) {
|
||||
log.error("文件上传失败:", e);
|
||||
FileUtil.del(absolutePath);
|
||||
throw new PoetryRuntimeException("文件上传失败!");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStoreName() {
|
||||
return StoreEnum.LOCAL.getCode();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
package com.ld.poetry.utils.storage;
|
||||
|
||||
import com.ld.poetry.handle.PoetryRuntimeException;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public enum StoreEnum {
|
||||
|
||||
QINIU("qiniu"),
|
||||
LOCAL("local");
|
||||
|
||||
private String code;
|
||||
|
||||
StoreEnum(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public static StoreEnum existCode(String code) {
|
||||
if (StringUtils.hasText(code)) {
|
||||
StoreEnum[] values = StoreEnum.values();
|
||||
for (StoreEnum typeEnum : values) {
|
||||
if (typeEnum.getCode().equals(code)) {
|
||||
return typeEnum;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new PoetryRuntimeException("存储平台不支持:" + code);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package com.ld.poetry.utils.storage;
|
||||
|
||||
import com.ld.poetry.vo.FileVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 储存服务
|
||||
*/
|
||||
public interface StoreService {
|
||||
|
||||
void deleteFile(List<String> files);
|
||||
|
||||
FileVO saveFile(FileVO fileVO);
|
||||
|
||||
String getStoreName();
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
package com.ld.poetry.vo;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Data
|
||||
public class FileVO {
|
||||
|
||||
private String type;
|
||||
|
||||
private String storeType;
|
||||
|
||||
private String relativePath;
|
||||
|
||||
private String absolutePath;
|
||||
|
||||
private String visitPath;
|
||||
|
||||
private MultipartFile file;
|
||||
}
|
||||
Binary file not shown.
Loading…
Reference in new issue