123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- package cn.rankin.resourceservice.service;
- import cn.rankin.common.utils.api.model.APICode;
- import cn.rankin.common.utils.api.model.APIResult;
- import cn.rankin.common.utils.api.page.Page;
- import cn.rankin.common.utils.constant.ResourceType;
- import cn.rankin.common.utils.dto.resource.ResourceSearchDTO;
- import cn.rankin.common.utils.exception.DuplicateValueException;
- import cn.rankin.common.utils.exception.NotFoundException;
- import cn.rankin.common.utils.exception.UnsupportedOperationException;
- import cn.rankin.resourceservice.dto.ResourceDetail;
- import cn.rankin.resourceservice.dto.ResourceRemote;
- import cn.rankin.data.api.resource.entity.Resource;
- import cn.rankin.resourceservice.proxy.RemoteResourceProxy;
- import cn.rankin.resourceservice.repository.ResourceRepository;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.util.StringUtils;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- @Slf4j
- @Service
- public class ResourceService {
- @Autowired
- private ResourceRepository resourceRepository;
- @Autowired
- private RemoteResourceProxy resourceProxy;
- @Value("${resource.img.domain}")
- private String imgDomain;
- @Value("${resource.video.domain}")
- private String videoDomain;
- public boolean exists(String code) {
- Long count = resourceRepository.countByCode(code);
- return count > 0L;
- }
- public Resource save(Resource resource) {
- return resourceRepository.save(resource);
- }
- public APIResult<Page<Resource>> search(Resource resource, Integer pageNo, Integer pageSize) {
- Long count = resourceRepository.count(resource);
- Page<Resource> page = new Page(count, pageNo, pageSize);
- if (count == 0) {
- return APIResult.ok(page);
- }
- List<Resource> resourceList = resourceRepository.find(resource, page.getStart(), pageSize, null);
- resourceList.forEach(r -> {
- initResourceUrl(r);
- });
- page.setList(resourceList);
- return APIResult.ok(page);
- }
- public APIResult<Map<String, Resource>> findMixByIds(List<String> ids) {
- Map<String, Resource> resourceMap = new HashMap<>();
- // 取本地
- List<Resource> nativeResourceList = resourceRepository.findByIds(ids);
- nativeResourceList.forEach(resource -> {
- initResourceUrl(resource);
- resourceMap.put(resource.getId(), resource);
- });
- // 本地取够了不取了
- if (nativeResourceList.size() == ids.size() && nativeResourceList.size() > 0) {
- return APIResult.ok(resourceMap);
- }
- // 取远程
- APIResult<List<ResourceDetail>> remoteResult = resourceProxy.getBatch(ids);
- if (remoteResult.getSuccess() && remoteResult.getData() != null) {
- List<ResourceDetail> remoteList = remoteResult.getData();
- remoteList.forEach(remote -> {
- Resource resource = remote.toNativeResource();
- resourceMap.put(resource.getId(), resource);
- });
- } else {
- log.error("remote resource empty or error!");
- }
- return APIResult.ok(resourceMap);
- }
- public APIResult<Page<Resource>> findRemotePage(ResourceSearchDTO resourceSearchDTO) {
- APIResult<Page<ResourceRemote>> result = resourceProxy.findPage(new HashMap<String, Object>(){
- {
- this.put("title", resourceSearchDTO.getName());
- this.put("pageNum", resourceSearchDTO.getPageNo());
- this.put("pageSize", resourceSearchDTO.getPageSize());
- this.put("type", resourceSearchDTO.getType());
- }
- });
- if (!result.getSuccess()) {
- log.error("Remote Server Error: code={}, message={}", result.getCode(), result.getMessage());
- APIResult errResult = APIResult.error(APICode.REMOTE_SERVER_ERROR);
- result.setMessage(result.getMessage());
- return errResult;
- }
- Page<ResourceRemote> remotePage = result.getData();
- Page<Resource> page = new Page<>(remotePage.getTotalSize(), remotePage.getPageNo(), remotePage.getPageSize());
- List<Resource> resourceList = new ArrayList<>();
- for (ResourceRemote resourceRemote : remotePage.getList()) {
- resourceList.addAll(resourceRemote.toNativeResource());
- }
- page.setList(resourceList);
- return APIResult.ok(page);
- }
- public APIResult<Page<Resource>> findRemoteByCode(String code) {
- APIResult<ResourceRemote> result = resourceProxy.findByCode(code);
- if (!result.getSuccess()) {
- log.error("Remote Server Error: code={}, message={}", result.getCode(), result.getMessage());
- APIResult errResult = APIResult.error(APICode.REMOTE_SERVER_ERROR);
- result.setMessage(result.getMessage());
- return errResult;
- }
- ResourceRemote resourceRemote = result.getData();
- if (resourceRemote == null) {
- return APIResult.ok(new Page<>());
- }
- List<Resource> resourceList = result.getData().toNativeResource();
- Page<Resource> page = new Page((long) resourceList.size());
- page.setList(resourceList);
- return APIResult.ok(page);
- }
- public void initResourceUrl(Resource resource) {
- if (resource == null) {
- return;
- }else if (resource.getPath() == null) {
- return;
- }
- String path = resource.getPath();
- if (ResourceType.VIDEO == resource.getType()) {
- resource.setUrl(concatUrl(videoDomain, path));
- }else if (ResourceType.IMG == resource.getType()) {
- resource.setUrl(concatUrl(imgDomain, path));
- }
- }
- public static String concatUrl(String domain, String path) {
- if (StringUtils.isEmpty(domain) || StringUtils.isEmpty(path)) {
- return null;
- }
- if (path.startsWith("/") && domain.endsWith("/")) {
- return domain.substring(0, domain.length()) + path;
- }else if(path.startsWith("/") || domain.endsWith("/")){
- return domain + path;
- }else {
- return domain + "/" + path;
- }
- }
- /**
- *
- * @param resource
- * @throws DuplicateValueException
- * @throws NotFoundException
- */
- public void update(Resource resource) throws DuplicateValueException, NotFoundException, UnsupportedOperationException {
- if (null == resource){
- log.error("Resource is null");
- return;
- }
- if (ResourceType.IMG == resource.getType()){
- updateImg(resource);
- }
- else{
- log.error("Not-Supported-Resource-Type, id={}, type={}", resource.getId(), resource.getType());
- throw new UnsupportedOperationException("Not-Supported-Resource-Type");
- }
- }
- /**
- * update Img
- * @param resource
- * @throws DuplicateValueException
- * @throws NotFoundException
- */
- private void updateImg(Resource resource) throws DuplicateValueException, NotFoundException {
- if (ResourceType.IMG != resource.getType()){
- log.error("Not-Img-Resource");
- return;
- }
- //find target resource
- Resource targetResouce = resourceRepository.find(resource.getId());
- if(null == targetResouce){
- log.error("Cannot Find Resource, id={}", resource.getId());
- throw new NotFoundException("Resource-Not-Found");
- }
- //only support code,title,path
- //first, make sure code is not duplicated
- List<Resource> foundedResource = resourceRepository.findByCode(resource.getCode());
- foundedResource.forEach( (each) -> {
- if ( !resource.getId().equals(each.getId()) ){
- log.error("Code-Already-Exist, code={}", resource.getCode());
- throw new DuplicateValueException("Code-Already-Exist");
- }
- });
- //set and save
- targetResouce.setCode(resource.getCode());
- targetResouce.setName(resource.getName());
- targetResouce.setPath(resource.getPath());
- resourceRepository.save(targetResouce);
- }
- /**
- * upload ViDEO
- * @param resource
- * @return
- */
- public APIResult<Resource> saveRemoteResource(Resource resource) {
- List<Map<String, Object>> videos = new ArrayList<>();
- Map<String, Object> video = new HashMap<>();
- video.put("bucket","");
- video.put("path",resource.getPath());
- video.put("format",resource.getFormat());
- video.put("quality",resource.getQuality());
- video.put("size",resource.getSize());
- videos.add(video);
- APIResult<ResourceDetail> apiResult = resourceProxy.saveResources(new HashMap<String, Object>(){
- {
- this.put("no", resource.getCode());
- this.put("title", resource.getName());
- this.put("type", resource.getType());
- this.put("videos", videos);
- }
- });
- if (!apiResult.getSuccess()) {
- log.error("Remote Server Error: code={}, message={}", apiResult.getCode(), apiResult.getMessage());
- APIResult errResult = APIResult.error(APICode.REMOTE_SERVER_ERROR);
- apiResult.setMessage(apiResult.getMessage());
- return errResult;
- }
- Resource result = apiResult.getData().toNativeResource();
- return APIResult.ok(result);
- }
- }
|