Skip to content

Commit

Permalink
feat($static-resource-center): get resource from Minio
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Jun 20, 2021
1 parent b5e1340 commit da01f3c
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 19 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.jmsoftware.maf.authcenter.read.controller;

import com.jmsoftware.maf.authcenter.read.entity.GetSingleResourcePayload;
import com.jmsoftware.maf.authcenter.read.service.ReadResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
* <h1>ReadResourceController</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 6/20/21 5:16 PM
**/
@Validated
@RestController
@RequiredArgsConstructor
@Api(tags = {"Read resource API"})
public class ReadResourceController {
private final ReadResourceService readResourceService;

@GetMapping("/{bucket}/{object}")
@ApiOperation(value = "Get single resource", notes = "Get or download single resource")
public ResponseEntity<Resource> getSingleResource(@PathVariable String bucket, @PathVariable String object,
@Valid GetSingleResourcePayload payload) {
return readResourceService.getSingleResource(bucket, object, payload);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.jmsoftware.maf.authcenter.read.entity;

import lombok.Data;

/**
* <h1>GetSingleResourcePayload</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 6/20/21 7:16 PM
**/
@Data
public class GetSingleResourcePayload {
private Boolean downloadable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.jmsoftware.maf.authcenter.read;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jmsoftware.maf.authcenter.read.service;

import com.jmsoftware.maf.authcenter.read.entity.GetSingleResourcePayload;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
* <h1>ReadResourceService</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 6/20/21 5:17 PM
**/
@Validated
public interface ReadResourceService {
String BUCKET_OBJECT_NAME_REGEX = "^.+/.+$";

/**
* Gets single resource.
*
* @param bucket the bucket
* @param object the object
* @param payload the payload
* @return the single resource
*/
ResponseEntity<Resource> getSingleResource(@NotBlank String bucket, @NotBlank String object,
@Valid @NotNull GetSingleResourcePayload payload);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.jmsoftware.maf.authcenter.read.service.impl;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jmsoftware.maf.authcenter.read.entity.GetSingleResourcePayload;
import com.jmsoftware.maf.authcenter.read.service.ReadResourceService;
import com.jmsoftware.maf.springcloudstarter.helper.MinioHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
* <h1>ReadResourceServiceImpl</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 6/20/21 5:18 PM
**/
@Slf4j
@Service
@RequiredArgsConstructor
public class ReadResourceServiceImpl implements ReadResourceService {
private final MinioHelper minioHelper;

@Override
public ResponseEntity<Resource> getSingleResource(@NotBlank String bucket, @NotBlank String object,
@Valid @NotNull GetSingleResourcePayload payload) {
val statObjectResponse = minioHelper.statObject(bucket, object);
if (ObjectUtil.isNull(statObjectResponse)) {
return ResponseEntity.notFound().build();
}
val inputStream = minioHelper.getObject(bucket, object);
val bodyBuilder = ResponseEntity.ok();
if (BooleanUtil.isTrue(payload.getDownloadable())) {
String contentDisposition = ContentDisposition.builder("attachment").filename(object).build().toString();
bodyBuilder.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition);
}
return bodyBuilder
.header(HttpHeaders.ACCEPT_RANGES, "bytes")
.contentLength(statObjectResponse.size())
.contentType(MediaType.parseMediaType(statObjectResponse.contentType()))
.body(new InputStreamResource(inputStream));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.jmsoftware.maf.authcenter.read.service.impl;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.jmsoftware.maf.authcenter.read.service;

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.jmsoftware.maf.authcenter.upload.controller;
package com.jmsoftware.maf.authcenter.write.controller;

import com.jmsoftware.maf.authcenter.upload.service.UploadService;
import com.jmsoftware.maf.authcenter.write.service.WriteResourceService;
import com.jmsoftware.maf.common.bean.ResponseBodyBean;
import com.jmsoftware.maf.common.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
* <h1>UploadController</h1>
* <h1>WriteResourceController</h1>
* <p>
* Change description here.
*
Expand All @@ -24,13 +25,15 @@
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/upload")
public class UploadController {
private final UploadService uploadService;
@Api(tags = {"Write resource API"})
public class WriteResourceController {
private final WriteResourceService writeResourceService;

@PostMapping("/single")
@PostMapping("/upload/single")
@SneakyThrows({IOException.class, BusinessException.class})
@ApiOperation(value = "Upload single resource", notes = "Upload single resource")
public ResponseBodyBean<String> uploadSingleResource(@RequestParam("file") MultipartFile multipartFile) {
return ResponseBodyBean.ofSuccess(uploadService.uploadSingleResource(multipartFile), "Succeed to upload");
return ResponseBodyBean.ofSuccess(writeResourceService.uploadSingleResource(multipartFile),
"Succeed to upload");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.jmsoftware.maf.authcenter.write;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jmsoftware.maf.authcenter.upload.service;
package com.jmsoftware.maf.authcenter.write.service;

import com.jmsoftware.maf.common.exception.BusinessException;
import org.springframework.validation.annotation.Validated;
Expand All @@ -8,14 +8,14 @@
import java.io.IOException;

/**
* <h1>UploadService</h1>
* <h1>WriteResourceService</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 6/20/21 2:19 PM
**/
@Validated
public interface UploadService {
public interface WriteResourceService {
/**
* Upload single resource string.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jmsoftware.maf.authcenter.upload.service.impl;
package com.jmsoftware.maf.authcenter.write.service.impl;

import cn.hutool.core.util.StrUtil;
import com.jmsoftware.maf.authcenter.upload.service.UploadService;
import com.jmsoftware.maf.authcenter.write.service.WriteResourceService;
import com.jmsoftware.maf.common.exception.BusinessException;
import com.jmsoftware.maf.springcloudstarter.helper.MinioHelper;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,7 +16,7 @@
import java.io.IOException;

/**
* <h1>UploadServiceImpl</h1>
* <h1>WriteResourceServiceImpl</h1>
* <p>
* Change description here.
*
Expand All @@ -25,7 +25,7 @@
@Slf4j
@Service
@RequiredArgsConstructor
public class UploadServiceImpl implements UploadService {
public class WriteResourceServiceImpl implements WriteResourceService {
private final MinioHelper minioHelper;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.jmsoftware.maf.authcenter.write.service;

0 comments on commit da01f3c

Please sign in to comment.