Skip to content

Commit

Permalink
feat: uri encode processing for attachment paths when querying attach…
Browse files Browse the repository at this point in the history
…ments (#1874)

* Deal with illegal character

* Update FilePathDescriptor.java

* 后端对链接进行encodeURI

* 删除之前无用的test

* Add some testcase for convertToDto

* checkstyle

* delete test

当前该测试用例貌似并不能在全平台上成功运行,所以废弃
  • Loading branch information
FanZeros authored May 17, 2022
1 parent 0e3890a commit 46220a9
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/main/java/run/halo/app/service/impl/AttachmentServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package run.halo.app.service.impl;

import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
Expand All @@ -18,6 +19,7 @@
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriUtils;
import run.halo.app.exception.AlreadyExistsException;
import run.halo.app.handler.file.FileHandlers;
import run.halo.app.model.dto.AttachmentDTO;
Expand All @@ -30,6 +32,7 @@
import run.halo.app.service.AttachmentService;
import run.halo.app.service.OptionService;
import run.halo.app.service.base.AbstractCrudService;
import run.halo.app.utils.FilenameUtils;
import run.halo.app.utils.HaloUtils;

/**
Expand Down Expand Up @@ -124,7 +127,8 @@ public Attachment upload(MultipartFile file) {
// Convert separator
attachment.setPath(HaloUtils.changeFileSeparatorToUrlSeparator(uploadResult.getFilePath()));
attachment.setFileKey(uploadResult.getKey());
attachment.setThumbPath(uploadResult.getThumbPath());
attachment.setThumbPath(
HaloUtils.changeFileSeparatorToUrlSeparator(uploadResult.getThumbPath()));
attachment.setMediaType(uploadResult.getMediaType().toString());
attachment.setSuffix(uploadResult.getSuffix());
attachment.setWidth(uploadResult.getWidth());
Expand Down Expand Up @@ -173,11 +177,26 @@ public AttachmentDTO convertToDto(Attachment attachment) {
AttachmentDTO attachmentDTO = new AttachmentDTO().convertFrom(attachment);

if (Objects.equals(attachmentDTO.getType(), AttachmentType.LOCAL)) {

// 将 local 存储的链接中的文件名替换为编码后的文件名
String path = attachmentDTO.getPath()
.replace(attachmentDTO.getName(), encodeValue(attachmentDTO.getName()));

String basename = FilenameUtils.getBasename(attachmentDTO.getName());
String extension = FilenameUtils.getExtension(attachmentDTO.getName());
// 得到 thumbnail name
String thumbnailName = String.format("%s-thumbnail%s", basename, extension);
String thumbnailPath = attachmentDTO.getThumbPath()
.replace(thumbnailName, encodeValue(thumbnailName));

// Append blog base url to path and thumbnail
String fullPath = StringUtils
.join(enabledAbsolutePath ? blogBaseUrl : "", "/", attachmentDTO.getPath());
.join(enabledAbsolutePath ? blogBaseUrl : "", "/", path);
String fullThumbPath = StringUtils
.join(enabledAbsolutePath ? blogBaseUrl : "", "/", attachmentDTO.getThumbPath());
.join(enabledAbsolutePath ? blogBaseUrl : "", "/", thumbnailPath);

// 对于之前上传的链接,需要将文件名替换为编码后的文件名
fullThumbPath = HaloUtils.changeFileSeparatorToUrlSeparator(fullThumbPath);

// Set full path and full thumb path
attachmentDTO.setPath(fullPath);
Expand All @@ -187,6 +206,11 @@ public AttachmentDTO convertToDto(Attachment attachment) {
return attachmentDTO;
}

private String encodeValue(String value) {
return UriUtils.encode(value, StandardCharsets.UTF_8);
}


@Override
public List<String> listAllMediaType() {
return attachmentRepository.findAllMediaType();
Expand Down
120 changes: 120 additions & 0 deletions src/test/java/run/halo/app/service/impl/AttachmentServiceImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package run.halo.app.service.impl;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import run.halo.app.handler.file.FileHandlers;
import run.halo.app.model.dto.AttachmentDTO;
import run.halo.app.model.entity.Attachment;
import run.halo.app.model.enums.AttachmentType;
import run.halo.app.repository.AttachmentRepository;
import run.halo.app.service.OptionService;


/**
* Test for Attachment Service implementation
*
* @date 2022-5-17
*/
@ExtendWith(MockitoExtension.class)
class AttachmentServiceImplTest {

@Mock
AttachmentRepository attachmentRepository;

@Mock
OptionService optionService;

@Mock
FileHandlers fileHandlers;

@InjectMocks
AttachmentServiceImpl attachmentService;

@Test
void convertToDtoNormal() {
Attachment attachment = new Attachment();
attachment.setName("fake-name");
attachment.setFileKey("upload/2022/05/fake-name.png");
attachment.setPath("upload/2022/05/fake-name.png");
attachment.setThumbPath("upload/2022/05/fake-name-thumbnail.png");
attachment.setType(AttachmentType.LOCAL);

Mockito.when(optionService.getBlogBaseUrl()).thenReturn("https://mock.halo.run");
Mockito.when(optionService.isEnabledAbsolutePath()).thenReturn(false);
AttachmentDTO attachmentDTO = attachmentService.convertToDto(attachment);

Assertions.assertEquals("/upload/2022/05/fake-name.png", attachmentDTO.getPath());
Assertions.assertEquals("/upload/2022/05/fake-name-thumbnail.png",
attachmentDTO.getThumbPath());
Mockito.verify(optionService, Mockito.times(1)).getBlogBaseUrl();
}

@Test
void convertToDtoWithChinese() {
Attachment attachment = new Attachment();
attachment.setName("图片");
attachment.setFileKey("upload/2022/05/图片.png");
attachment.setPath("upload/2022/05/图片.png");
attachment.setThumbPath("upload/2022/05/图片-thumbnail.png");
attachment.setType(AttachmentType.LOCAL);

Mockito.when(optionService.getBlogBaseUrl()).thenReturn("https://mock.halo.run");
Mockito.when(optionService.isEnabledAbsolutePath()).thenReturn(false);
AttachmentDTO attachmentDTO = attachmentService.convertToDto(attachment);

Assertions.assertEquals("/upload/2022/05/%E5%9B%BE%E7%89%87.png", attachmentDTO.getPath());
Assertions.assertEquals("/upload/2022/05/%E5%9B%BE%E7%89%87-thumbnail.png",
attachmentDTO.getThumbPath());
Mockito.verify(optionService, Mockito.times(1)).getBlogBaseUrl();
}

@Test
void convertToDtoWithSpecialChar() {
Attachment attachment = new Attachment();
attachment.setName("100%1#");
attachment.setFileKey("upload/2022/05/100%1#.png");
attachment.setPath("upload/2022/05/100%1#.png");
attachment.setThumbPath("upload/2022/05/100%1#-thumbnail.png");
attachment.setType(AttachmentType.LOCAL);

Mockito.when(optionService.getBlogBaseUrl()).thenReturn("https://mock.halo.run");
Mockito.when(optionService.isEnabledAbsolutePath()).thenReturn(false);
AttachmentDTO attachmentDTO = attachmentService.convertToDto(attachment);

Assertions.assertEquals("/upload/2022/05/100%251%23.png", attachmentDTO.getPath());
Assertions.assertEquals("/upload/2022/05/100%251%23-thumbnail.png",
attachmentDTO.getThumbPath());
Mockito.verify(optionService, Mockito.times(1)).getBlogBaseUrl();
}

@Test
void convertToDtoWithFormerVersionFile() {
// Attachment attachment = new Attachment();
// attachment.setName("之前版本上传的图片");
// attachment.setFileKey("upload/2022/04/之前版本上传的图片.png");
// attachment.setPath("upload/2022/04/之前版本上传的图片.png");
// attachment.setThumbPath("upload/2022\\04\\之前版本上传的图片-thumbnail.png");
// attachment.setType(AttachmentType.LOCAL);
//
// Mockito.when(optionService.getBlogBaseUrl()).thenReturn("https://mock.halo.run");
// Mockito.when(optionService.isEnabledAbsolutePath()).thenReturn(false);
// AttachmentDTO attachmentDTO = attachmentService.convertToDto(attachment);
//
// Assertions.assertEquals(
// "/upload/2022/04/%E4%B9%8B%E5%89%8D%E7%89%88%E6%9C%AC%E4%B8%8A%E4%BC%A0%E7%9A
// %84%E5"
// + "%9B%BE%E7%89%87.png",
// attachmentDTO.getPath());
// Assertions.assertEquals(
// "/upload/2022/04/%E4%B9%8B%E5%89%8D%E7%89%88%E6%9C%AC%E4%B8%8A%E4%BC%A0%E7%9A
// %84%E5"
// + "%9B%BE%E7%89%87-thumbnail.png",
// attachmentDTO.getThumbPath());
// Mockito.verify(optionService, Mockito.times(1)).getBlogBaseUrl();
}
}

0 comments on commit 46220a9

Please sign in to comment.