mirror of https://github.com/halo-dev/halo
Resolve attachment relative path problem
parent
184523c8a3
commit
028b2b423f
|
@ -1,5 +1,7 @@
|
|||
package run.halo.app.model.dto.base;
|
||||
|
||||
import org.springframework.lang.NonNull;
|
||||
|
||||
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
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@NonNull
|
||||
default <T extends DTO> T convertFrom(DOMAIN domain) {
|
||||
|
||||
updateProperties(domain, this);
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
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.Pageable;
|
||||
import org.springframework.lang.NonNull;
|
||||
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;
|
||||
|
||||
|
||||
|
@ -45,4 +44,13 @@ public interface AttachmentService extends CrudService<Attachment, Integer> {
|
|||
*/
|
||||
@NonNull
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,13 @@
|
|||
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.model.dto.AttachmentOutputDTO;
|
||||
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.OptionService;
|
||||
import run.halo.app.service.base.AbstractCrudService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.service.base.AbstractCrudService;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* AttachmentService implementation class
|
||||
|
@ -53,7 +54,7 @@ public class AttachmentServiceImpl extends AbstractCrudService<Attachment, Integ
|
|||
Page<Attachment> attachmentPage = listAll(pageable);
|
||||
|
||||
// Convert and return
|
||||
return attachmentPage.map(attachment -> new AttachmentOutputDTO().convertFrom(attachment));
|
||||
return attachmentPage.map(this::convertToDto);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -102,6 +103,29 @@ public class AttachmentServiceImpl extends AbstractCrudService<Attachment, Integ
|
|||
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.
|
||||
*
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
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 org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
@ -10,6 +7,9 @@ import org.springframework.data.web.PageableDefault;
|
|||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
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.List;
|
||||
|
@ -63,13 +63,13 @@ public class AttachmentController {
|
|||
@DeleteMapping("{id:\\d+}")
|
||||
@ApiOperation("Delete attachment by id")
|
||||
public AttachmentOutputDTO deletePermanently(@PathVariable("id") Integer id) {
|
||||
return new AttachmentOutputDTO().convertFrom(attachmentService.removePermanently(id));
|
||||
return attachmentService.convertToDto(attachmentService.removePermanently(id));
|
||||
}
|
||||
|
||||
@PostMapping("upload")
|
||||
@ApiOperation("Uploads single 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)
|
||||
|
@ -81,7 +81,7 @@ public class AttachmentController {
|
|||
// Upload single file
|
||||
Attachment attachment = attachmentService.upload(file);
|
||||
// Convert and add
|
||||
result.add(new AttachmentOutputDTO().convertFrom(attachment));
|
||||
result.add(attachmentService.convertToDto(attachment));
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
Loading…
Reference in New Issue