Skip to content

Commit

Permalink
Merge pull request #151 from jamebal/develop
Browse files Browse the repository at this point in the history
perf: 忽略文件后缀大小写
  • Loading branch information
jamebal authored Aug 3, 2024
2 parents a0e0ffb + 368f1cc commit 6a5fd7e
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 156 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.jmal.clouddisk.controller.rest;

import cn.hutool.core.io.FileUtil;
import com.jmal.clouddisk.config.FileProperties;
import com.jmal.clouddisk.service.IShareService;
import com.jmal.clouddisk.util.FileContentTypeUtils;
import com.jmal.clouddisk.util.MyFileUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -38,7 +38,7 @@ public ResponseEntity<UrlResource> m3u8(@PathVariable String username, @PathVari
Path txtPath = Paths.get(fileProperties.getRootDir(), fileProperties.getChunkFileDir(), username, fileProperties.getVideoTranscodeCache(), fileId, suffix);
UrlResource videoResource = new UrlResource(txtPath.toUri());
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_TYPE, FileContentTypeUtils.getContentType(FileUtil.extName(suffix)))
.header(HttpHeaders.CONTENT_TYPE, FileContentTypeUtils.getContentType(MyFileUtils.extName(suffix)))
.header(HttpHeaders.CACHE_CONTROL, "max-age=600")
.body(videoResource);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ private void responseHeader(HttpServletResponse response, String fileName, byte[
if (!CharSequenceUtil.isBlank(fileName)) {
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "fileName=" + ContentDisposition.builder("attachment")
.filename(UriUtils.encode(fileName, StandardCharsets.UTF_8)));
response.setHeader(HttpHeaders.CONTENT_TYPE, FileContentTypeUtils.getContentType(FileUtil.extName(fileName)));
response.setHeader(HttpHeaders.CONTENT_TYPE, FileContentTypeUtils.getContentType(MyFileUtils.extName(fileName)));
}
if (img != null) {
response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(img.length));
Expand Down Expand Up @@ -374,9 +374,9 @@ public static byte[] imageCrop(File srcFile, String q, String w, String h) {
int srcHeight = bim.getHeight();

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

Thumbnails.Builder<? extends BufferedImage> thumbnail = Thumbnails.of(bim)
.outputFormat("jpg") // 指定输出格式
Expand Down Expand Up @@ -419,20 +419,20 @@ public static byte[] imageCrop(File srcFile, String q, String w, String h) {
return new byte[0];
}

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/jmal/clouddisk/lucene/LuceneService.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public void setFileIndex(FileIndex fileIndex) {

private void setType(File file, FileIndex fileIndex) {
String fileName = file.getName();
String suffix = FileUtil.extName(fileName);
String suffix = MyFileUtils.extName(fileName);
fileIndex.setType(Constants.OTHER);
if (StrUtil.isBlank(suffix)) {
fileIndex.setType(Constants.OTHER);
Expand Down Expand Up @@ -380,7 +380,7 @@ private boolean checkFileContent(File file) {
if (!file.isFile() || file.length() < 1) {
return false;
}
String type = FileTypeUtil.getType(file);
String type = FileTypeUtil.getType(file).toLowerCase();
if (MyFileUtils.hasContentFile(type)) {
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/jmal/clouddisk/oss/FileInfo.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.jmal.clouddisk.oss;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.io.FileUtil;
import com.jmal.clouddisk.model.FileDocument;
import com.jmal.clouddisk.model.FileIntroVO;
import com.jmal.clouddisk.oss.web.WebOssCommonService;
import com.jmal.clouddisk.util.CaffeineUtil;
import com.jmal.clouddisk.util.FileContentTypeUtils;
import com.jmal.clouddisk.util.MyFileUtils;
import lombok.Data;

import java.nio.file.Path;
Expand Down Expand Up @@ -54,7 +54,7 @@ public FileIntroVO toFileIntroVO(String ossPath, String userId) {
fileIntroVO.setName(fileName);
fileIntroVO.setPath(WebOssCommonService.getPath(key, rootName));
fileIntroVO.setSize(size);
String suffix = FileUtil.extName(fileName);
String suffix = MyFileUtils.extName(fileName);
fileIntroVO.setSuffix(suffix);
fileIntroVO.setMd5(eTag);
fileIntroVO.setContentType(FileContentTypeUtils.getContentType(suffix));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ public String createFile(String username, File file, String userId, Boolean isPu
if (CaffeineUtil.hasUploadFileCache(file.getAbsolutePath())) {
return null;
}
log.info("createFile");
if (CharSequenceUtil.isBlank(username)) {
return null;
}
Expand All @@ -313,7 +312,7 @@ public String createFile(String username, File file, String userId, Boolean isPu
}
}
String fileName = file.getName();
String suffix = FileUtil.extName(fileName);
String suffix = MyFileUtils.extName(fileName);
String contentType = getContentType(file, FileContentTypeUtils.getContentType(suffix));
if (contentType.startsWith(Constants.CONTENT_TYPE_IMAGE)) {
// 换成webp格式的图片
Expand Down Expand Up @@ -342,7 +341,7 @@ public String createFile(String username, File file, String userId, Boolean isPu
Update update = new Update();
updateExifInfo(file, fileExists, contentType, suffix, update);
updateVideoInfo(file, fileExists, contentType, update);
updateOtherInfo(fileExists, contentType, update);
updateOtherInfo(fileExists, contentType, suffix, update);
if (!update.getUpdateObject().isEmpty()) {
mongoTemplate.updateFirst(query, update, COLLECTION_NAME);
}
Expand Down Expand Up @@ -387,10 +386,13 @@ public String createFile(String username, File file, String userId, Boolean isPu
return fileId;
}

private void updateOtherInfo(FileDocument fileExists, String contentType, Update update) {
private void updateOtherInfo(FileDocument fileExists, String contentType, String suffix, Update update) {
if (!contentType.equals(fileExists.getContentType())) {
update.set(Constants.CONTENT_TYPE, contentType);
}
if (StrUtil.isNotBlank(suffix) && !suffix.equals(fileExists.getSuffix())) {
update.set(Constants.SUFFIX, suffix);
}
}

private String getRelativePath(String username, String fileAbsolutePath, String fileName) {
Expand Down Expand Up @@ -950,7 +952,7 @@ public void modifyFile(String username, File file) {
query.addCriteria(Criteria.where("path").is(relativePath));
query.addCriteria(Criteria.where("name").is(fileName));

String suffix = FileUtil.extName(fileName);
String suffix = MyFileUtils.extName(fileName);
String contentType = FileContentTypeUtils.getContentType(suffix);
// 文件是否存在
FileDocument fileDocument = mongoTemplate.findOne(query, FileDocument.class, COLLECTION_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ private static StreamingResponseBody getStreamingResponseBody(File file) {
return outputStream -> {
try (BufferedReader bufferedReader = ReaderFactory.createBufferedReader(file)) {
// 判断file是否为log文件
boolean logFile = file.length() > 0 && FileTypeUtil.getType(file).equals("log");
boolean logFile = file.length() > 0 && FileTypeUtil.getType(file).equalsIgnoreCase("log");
String line;
while ((line = bufferedReader.readLine()) != null) {
if (logFile) {
Expand Down Expand Up @@ -1153,7 +1153,7 @@ public ResponseResult<Object> unzip(String fileId, String destFileId) throws Com
} else {
if (fileId.equals(destFileId)) {
// 解压到当前文件夹
destDir = filePath.substring(0, filePath.length() - FileUtil.extName(new File(filePath)).length() - 1);
destDir = filePath.substring(0, filePath.length() - MyFileUtils.extName(new File(filePath)).length() - 1);
} else {
// 其他目录
FileDocument dest = getById(destFileId);
Expand Down Expand Up @@ -1302,7 +1302,7 @@ public ResponseResult<FileIntroVO> addFile(String fileName, Boolean isFolder, St
fileIntroVO.setUserId(userId);
fileIntroVO.setPath(resPath);
fileIntroVO.setIsFolder(isFolder);
fileIntroVO.setSuffix(FileUtil.extName(fileName));
fileIntroVO.setSuffix(MyFileUtils.extName(fileName));
String fileId = uploadFile(username, path.toFile());
fileIntroVO.setId(fileId);
return ResultUtil.success(fileIntroVO);
Expand Down Expand Up @@ -1417,7 +1417,7 @@ private List<FileIntroVO> listFile(String username, String dirPath, boolean temp
return Arrays.stream(fileList).map(file -> {
FileIntroVO fileDocument = new FileIntroVO();
String filename = file.getName();
String suffix = FileUtil.extName(filename);
String suffix = MyFileUtils.extName(filename);
boolean isFolder = file.isDirectory();
fileDocument.setName(filename);
fileDocument.setIsFolder(isFolder);
Expand Down Expand Up @@ -1690,7 +1690,7 @@ private boolean renameFileError(String newFileName, String fileId, String filePa
query.addCriteria(Criteria.where("_id").is(fileId));
Update update = new Update();
update.set("name", newFileName);
update.set(Constants.SUFFIX, FileUtil.extName(newFileName));
update.set(Constants.SUFFIX, MyFileUtils.extName(newFileName));
update.set("updateDate", LocalDateTime.now(TimeUntils.ZONE_ID));
mongoTemplate.upsert(query, update, COLLECTION_NAME);
} else {
Expand Down Expand Up @@ -1734,7 +1734,7 @@ public ResponseResult<Object> upload(UploadApiParamDTO upload) throws IOExceptio
// 保存文件信息
upload.setInputStream(file.getInputStream());
upload.setContentType(file.getContentType());
upload.setSuffix(FileUtil.extName(filename));
upload.setSuffix(MyFileUtils.extName(filename));
FileUtil.writeFromStream(file.getInputStream(), chunkFile);
uploadFile(upload.getUsername(), chunkFile);
uploadResponse.setUpload(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public ResponseResult<Object> editMarkdown(ArticleParamDTO upload) {
String filename = upload.getFilename();
// 同步文档文件
String currentDirectory = syncDocFile(upload, uploadDate, fileDocument, filename);
fileDocument.setSuffix(FileUtil.extName(filename));
fileDocument.setSuffix(MyFileUtils.extName(filename));
fileDocument.setUserId(upload.getUserId());
fileDocument.setUpdateDate(nowDate);
fileDocument.setPath(currentDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.jmal.clouddisk.repository.IAuthDAO;
import com.jmal.clouddisk.service.Constants;
import com.jmal.clouddisk.util.MongoUtil;
import com.jmal.clouddisk.util.MyFileUtils;
import com.jmal.clouddisk.util.ResponseResult;
import com.jmal.clouddisk.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -71,7 +72,7 @@ public class SettingService {
* @param file logo文件
*/
public ResponseResult<Object> uploadLogo(MultipartFile file) {
String filename = "logo-" + System.currentTimeMillis() + "." + FileUtil.extName(file.getOriginalFilename());
String filename = "logo-" + System.currentTimeMillis() + "." + MyFileUtils.extName(file.getOriginalFilename());
File dist = new File(fileProperties.getRootDir() + File.separator + filename);
try {
String oldFilename = null;
Expand Down
126 changes: 0 additions & 126 deletions src/main/java/com/jmal/clouddisk/util/FileFinderUtil.java

This file was deleted.

Loading

0 comments on commit 6a5fd7e

Please sign in to comment.