ResourceService.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package cn.rankin.resourceservice.service;
  2. import cn.rankin.common.utils.api.model.APICode;
  3. import cn.rankin.common.utils.api.model.APIResult;
  4. import cn.rankin.common.utils.api.page.Page;
  5. import cn.rankin.common.utils.constant.ResourceType;
  6. import cn.rankin.common.utils.dto.resource.ResourceSearchDTO;
  7. import cn.rankin.common.utils.exception.DuplicateValueException;
  8. import cn.rankin.common.utils.exception.NotFoundException;
  9. import cn.rankin.common.utils.exception.UnsupportedOperationException;
  10. import cn.rankin.resourceservice.dto.ResourceDetail;
  11. import cn.rankin.resourceservice.dto.ResourceRemote;
  12. import cn.rankin.data.api.resource.entity.Resource;
  13. import cn.rankin.resourceservice.proxy.RemoteResourceProxy;
  14. import cn.rankin.resourceservice.repository.ResourceRepository;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.util.StringUtils;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. @Slf4j
  25. @Service
  26. public class ResourceService {
  27. @Autowired
  28. private ResourceRepository resourceRepository;
  29. @Autowired
  30. private RemoteResourceProxy resourceProxy;
  31. @Value("${resource.img.domain}")
  32. private String imgDomain;
  33. @Value("${resource.video.domain}")
  34. private String videoDomain;
  35. public boolean exists(String code) {
  36. Long count = resourceRepository.countByCode(code);
  37. return count > 0L;
  38. }
  39. public Resource save(Resource resource) {
  40. return resourceRepository.save(resource);
  41. }
  42. public APIResult<Page<Resource>> search(Resource resource, Integer pageNo, Integer pageSize) {
  43. Long count = resourceRepository.count(resource);
  44. Page<Resource> page = new Page(count, pageNo, pageSize);
  45. if (count == 0) {
  46. return APIResult.ok(page);
  47. }
  48. List<Resource> resourceList = resourceRepository.find(resource, page.getStart(), pageSize, null);
  49. resourceList.forEach(r -> {
  50. initResourceUrl(r);
  51. });
  52. page.setList(resourceList);
  53. return APIResult.ok(page);
  54. }
  55. public APIResult<Map<String, Resource>> findMixByIds(List<String> ids) {
  56. Map<String, Resource> resourceMap = new HashMap<>();
  57. // 取本地
  58. List<Resource> nativeResourceList = resourceRepository.findByIds(ids);
  59. nativeResourceList.forEach(resource -> {
  60. initResourceUrl(resource);
  61. resourceMap.put(resource.getId(), resource);
  62. });
  63. // 本地取够了不取了
  64. if (nativeResourceList.size() == ids.size() && nativeResourceList.size() > 0) {
  65. return APIResult.ok(resourceMap);
  66. }
  67. // 取远程
  68. APIResult<List<ResourceDetail>> remoteResult = resourceProxy.getBatch(ids);
  69. if (remoteResult.getSuccess() && remoteResult.getData() != null) {
  70. List<ResourceDetail> remoteList = remoteResult.getData();
  71. remoteList.forEach(remote -> {
  72. Resource resource = remote.toNativeResource();
  73. resourceMap.put(resource.getId(), resource);
  74. });
  75. } else {
  76. log.error("remote resource empty or error!");
  77. }
  78. return APIResult.ok(resourceMap);
  79. }
  80. public APIResult<Page<Resource>> findRemotePage(ResourceSearchDTO resourceSearchDTO) {
  81. APIResult<Page<ResourceRemote>> result = resourceProxy.findPage(new HashMap<String, Object>(){
  82. {
  83. this.put("title", resourceSearchDTO.getName());
  84. this.put("pageNum", resourceSearchDTO.getPageNo());
  85. this.put("pageSize", resourceSearchDTO.getPageSize());
  86. this.put("type", resourceSearchDTO.getType());
  87. }
  88. });
  89. if (!result.getSuccess()) {
  90. log.error("Remote Server Error: code={}, message={}", result.getCode(), result.getMessage());
  91. APIResult errResult = APIResult.error(APICode.REMOTE_SERVER_ERROR);
  92. result.setMessage(result.getMessage());
  93. return errResult;
  94. }
  95. Page<ResourceRemote> remotePage = result.getData();
  96. Page<Resource> page = new Page<>(remotePage.getTotalSize(), remotePage.getPageNo(), remotePage.getPageSize());
  97. List<Resource> resourceList = new ArrayList<>();
  98. for (ResourceRemote resourceRemote : remotePage.getList()) {
  99. resourceList.addAll(resourceRemote.toNativeResource());
  100. }
  101. page.setList(resourceList);
  102. return APIResult.ok(page);
  103. }
  104. public APIResult<Page<Resource>> findRemoteByCode(String code) {
  105. APIResult<ResourceRemote> result = resourceProxy.findByCode(code);
  106. if (!result.getSuccess()) {
  107. log.error("Remote Server Error: code={}, message={}", result.getCode(), result.getMessage());
  108. APIResult errResult = APIResult.error(APICode.REMOTE_SERVER_ERROR);
  109. result.setMessage(result.getMessage());
  110. return errResult;
  111. }
  112. ResourceRemote resourceRemote = result.getData();
  113. if (resourceRemote == null) {
  114. return APIResult.ok(new Page<>());
  115. }
  116. List<Resource> resourceList = result.getData().toNativeResource();
  117. Page<Resource> page = new Page((long) resourceList.size());
  118. page.setList(resourceList);
  119. return APIResult.ok(page);
  120. }
  121. public void initResourceUrl(Resource resource) {
  122. if (resource == null) {
  123. return;
  124. }else if (resource.getPath() == null) {
  125. return;
  126. }
  127. String path = resource.getPath();
  128. if (ResourceType.VIDEO == resource.getType()) {
  129. resource.setUrl(concatUrl(videoDomain, path));
  130. }else if (ResourceType.IMG == resource.getType()) {
  131. resource.setUrl(concatUrl(imgDomain, path));
  132. }
  133. }
  134. public static String concatUrl(String domain, String path) {
  135. if (StringUtils.isEmpty(domain) || StringUtils.isEmpty(path)) {
  136. return null;
  137. }
  138. if (path.startsWith("/") && domain.endsWith("/")) {
  139. return domain.substring(0, domain.length()) + path;
  140. }else if(path.startsWith("/") || domain.endsWith("/")){
  141. return domain + path;
  142. }else {
  143. return domain + "/" + path;
  144. }
  145. }
  146. /**
  147. *
  148. * @param resource
  149. * @throws DuplicateValueException
  150. * @throws NotFoundException
  151. */
  152. public void update(Resource resource) throws DuplicateValueException, NotFoundException, UnsupportedOperationException {
  153. if (null == resource){
  154. log.error("Resource is null");
  155. return;
  156. }
  157. if (ResourceType.IMG == resource.getType()){
  158. updateImg(resource);
  159. }
  160. else{
  161. log.error("Not-Supported-Resource-Type, id={}, type={}", resource.getId(), resource.getType());
  162. throw new UnsupportedOperationException("Not-Supported-Resource-Type");
  163. }
  164. }
  165. /**
  166. * update Img
  167. * @param resource
  168. * @throws DuplicateValueException
  169. * @throws NotFoundException
  170. */
  171. private void updateImg(Resource resource) throws DuplicateValueException, NotFoundException {
  172. if (ResourceType.IMG != resource.getType()){
  173. log.error("Not-Img-Resource");
  174. return;
  175. }
  176. //find target resource
  177. Resource targetResouce = resourceRepository.find(resource.getId());
  178. if(null == targetResouce){
  179. log.error("Cannot Find Resource, id={}", resource.getId());
  180. throw new NotFoundException("Resource-Not-Found");
  181. }
  182. //only support code,title,path
  183. //first, make sure code is not duplicated
  184. List<Resource> foundedResource = resourceRepository.findByCode(resource.getCode());
  185. foundedResource.forEach( (each) -> {
  186. if ( !resource.getId().equals(each.getId()) ){
  187. log.error("Code-Already-Exist, code={}", resource.getCode());
  188. throw new DuplicateValueException("Code-Already-Exist");
  189. }
  190. });
  191. //set and save
  192. targetResouce.setCode(resource.getCode());
  193. targetResouce.setName(resource.getName());
  194. targetResouce.setPath(resource.getPath());
  195. resourceRepository.save(targetResouce);
  196. }
  197. /**
  198. * upload ViDEO
  199. * @param resource
  200. * @return
  201. */
  202. public APIResult<Resource> saveRemoteResource(Resource resource) {
  203. List<Map<String, Object>> videos = new ArrayList<>();
  204. Map<String, Object> video = new HashMap<>();
  205. video.put("bucket","");
  206. video.put("path",resource.getPath());
  207. video.put("format",resource.getFormat());
  208. video.put("quality",resource.getQuality());
  209. video.put("size",resource.getSize());
  210. videos.add(video);
  211. APIResult<ResourceDetail> apiResult = resourceProxy.saveResources(new HashMap<String, Object>(){
  212. {
  213. this.put("no", resource.getCode());
  214. this.put("title", resource.getName());
  215. this.put("type", resource.getType());
  216. this.put("videos", videos);
  217. }
  218. });
  219. if (!apiResult.getSuccess()) {
  220. log.error("Remote Server Error: code={}, message={}", apiResult.getCode(), apiResult.getMessage());
  221. APIResult errResult = APIResult.error(APICode.REMOTE_SERVER_ERROR);
  222. apiResult.setMessage(apiResult.getMessage());
  223. return errResult;
  224. }
  225. Resource result = apiResult.getData().toNativeResource();
  226. return APIResult.ok(result);
  227. }
  228. }