Resolve attachment relative path problem

pull/146/head
johnniang 2019-04-12 19:13:11 +08:00
parent 184523c8a3
commit 028b2b423f
4 changed files with 55 additions and 20 deletions

View File

@ -1,5 +1,7 @@
package run.halo.app.model.dto.base; package run.halo.app.model.dto.base;
import org.springframework.lang.NonNull;
import static run.halo.app.utils.BeanUtils.updateProperties; import static run.halo.app.utils.BeanUtils.updateProperties;
/** /**
@ -20,6 +22,7 @@ public interface OutputConverter<DTO extends OutputConverter<DTO, DOMAIN>, DOMAI
* @return converted dto data * @return converted dto data
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@NonNull
default <T extends DTO> T convertFrom(DOMAIN domain) { default <T extends DTO> T convertFrom(DOMAIN domain) {
updateProperties(domain, this); updateProperties(domain, this);

View File

@ -1,13 +1,12 @@
package run.halo.app.service; package run.halo.app.service;
import run.halo.app.exception.FileOperationException;
import run.halo.app.model.dto.AttachmentOutputDTO;
import run.halo.app.model.entity.Attachment;
import run.halo.app.service.base.CrudService;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import run.halo.app.exception.FileOperationException;
import run.halo.app.model.dto.AttachmentOutputDTO;
import run.halo.app.model.entity.Attachment;
import run.halo.app.service.base.CrudService; import run.halo.app.service.base.CrudService;
@ -45,4 +44,13 @@ public interface AttachmentService extends CrudService<Attachment, Integer> {
*/ */
@NonNull @NonNull
Attachment removePermanently(@NonNull Integer id); Attachment removePermanently(@NonNull Integer id);
/**
* Converts to attachment output dto.
*
* @param attachment attachment must not be null
* @return an attachment output dto
*/
@NonNull
AttachmentOutputDTO convertToDto(@NonNull Attachment attachment);
} }

View File

@ -1,5 +1,13 @@
package run.halo.app.service.impl; package run.halo.app.service.impl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;
import run.halo.app.handler.file.FileHandlers; import run.halo.app.handler.file.FileHandlers;
import run.halo.app.model.dto.AttachmentOutputDTO; import run.halo.app.model.dto.AttachmentOutputDTO;
import run.halo.app.model.entity.Attachment; import run.halo.app.model.entity.Attachment;
@ -10,15 +18,8 @@ import run.halo.app.repository.AttachmentRepository;
import run.halo.app.service.AttachmentService; import run.halo.app.service.AttachmentService;
import run.halo.app.service.OptionService; import run.halo.app.service.OptionService;
import run.halo.app.service.base.AbstractCrudService; import run.halo.app.service.base.AbstractCrudService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page; import java.util.Objects;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;
import run.halo.app.handler.file.FileHandlers;
import run.halo.app.service.base.AbstractCrudService;
/** /**
* AttachmentService implementation class * AttachmentService implementation class
@ -53,7 +54,7 @@ public class AttachmentServiceImpl extends AbstractCrudService<Attachment, Integ
Page<Attachment> attachmentPage = listAll(pageable); Page<Attachment> attachmentPage = listAll(pageable);
// Convert and return // Convert and return
return attachmentPage.map(attachment -> new AttachmentOutputDTO().convertFrom(attachment)); return attachmentPage.map(this::convertToDto);
} }
@Override @Override
@ -102,6 +103,29 @@ public class AttachmentServiceImpl extends AbstractCrudService<Attachment, Integ
return deletedAttachment; return deletedAttachment;
} }
@Override
public AttachmentOutputDTO convertToDto(Attachment attachment) {
Assert.notNull(attachment, "Attachment must not be null");
// Get blog base url
String blogBaseUrl = optionService.getBlogBaseUrl();
// Convert to output dto
AttachmentOutputDTO attachmentOutputDTO = new AttachmentOutputDTO().convertFrom(attachment);
if (Objects.equals(attachmentOutputDTO.getType(), AttachmentType.LOCAL)) {
// Append blog base url to path and thumbnail
String fullPath = StringUtils.join(blogBaseUrl, "/", attachmentOutputDTO.getPath());
String fullThumbPath = StringUtils.join(blogBaseUrl, "/", attachmentOutputDTO.getThumbPath());
// Set full path and full thumb path
attachmentOutputDTO.setPath(fullPath);
attachmentOutputDTO.setThumbPath(fullThumbPath);
}
return attachmentOutputDTO;
}
/** /**
* Get attachment type from options. * Get attachment type from options.
* *

View File

@ -1,8 +1,5 @@
package run.halo.app.web.controller.admin.api; package run.halo.app.web.controller.admin.api;
import run.halo.app.model.dto.AttachmentOutputDTO;
import run.halo.app.model.entity.Attachment;
import run.halo.app.service.AttachmentService;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@ -10,6 +7,9 @@ import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import run.halo.app.model.dto.AttachmentOutputDTO;
import run.halo.app.model.entity.Attachment;
import run.halo.app.service.AttachmentService;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -63,13 +63,13 @@ public class AttachmentController {
@DeleteMapping("{id:\\d+}") @DeleteMapping("{id:\\d+}")
@ApiOperation("Delete attachment by id") @ApiOperation("Delete attachment by id")
public AttachmentOutputDTO deletePermanently(@PathVariable("id") Integer id) { public AttachmentOutputDTO deletePermanently(@PathVariable("id") Integer id) {
return new AttachmentOutputDTO().convertFrom(attachmentService.removePermanently(id)); return attachmentService.convertToDto(attachmentService.removePermanently(id));
} }
@PostMapping("upload") @PostMapping("upload")
@ApiOperation("Uploads single file") @ApiOperation("Uploads single file")
public AttachmentOutputDTO uploadAttachment(@RequestPart("file") MultipartFile file) { public AttachmentOutputDTO uploadAttachment(@RequestPart("file") MultipartFile file) {
return new AttachmentOutputDTO().convertFrom(attachmentService.upload(file)); return attachmentService.convertToDto(attachmentService.upload(file));
} }
@PostMapping(value = "uploads", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @PostMapping(value = "uploads", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ -81,7 +81,7 @@ public class AttachmentController {
// Upload single file // Upload single file
Attachment attachment = attachmentService.upload(file); Attachment attachment = attachmentService.upload(file);
// Convert and add // Convert and add
result.add(new AttachmentOutputDTO().convertFrom(attachment)); result.add(attachmentService.convertToDto(attachment));
} }
return result; return result;