From 704e7207d74b6477c5f472c5aba92d484ab17f98 Mon Sep 17 00:00:00 2001 From: zhanghua <314079846@qq.com> Date: 星期三, 29 十一月 2023 17:07:11 +0800 Subject: [PATCH] Merge branch 'master' of http://42.193.1.25:9521/r/sccg_server --- ycl-platform/src/main/java/com/ycl/controller/dingding/DingController.java | 261 ++++++++++++++++++++++++++++++++++----------------- 1 files changed, 174 insertions(+), 87 deletions(-) diff --git a/ycl-platform/src/main/java/com/ycl/controller/dingding/DingController.java b/ycl-platform/src/main/java/com/ycl/controller/dingding/DingController.java index ed4f836..eeb2a40 100644 --- a/ycl-platform/src/main/java/com/ycl/controller/dingding/DingController.java +++ b/ycl-platform/src/main/java/com/ycl/controller/dingding/DingController.java @@ -2,37 +2,52 @@ import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSONObject; +import com.alibaba.xxpt.gateway.shared.api.request.OapiMediaUploadRequest; +import com.alibaba.xxpt.gateway.shared.api.response.OapiMediaUploadResponse; import com.alibaba.xxpt.gateway.shared.client.http.ExecutableClient; import com.alibaba.xxpt.gateway.shared.client.http.GetClient; +import com.alibaba.xxpt.gateway.shared.client.http.IntelligentPostClient; import com.alibaba.xxpt.gateway.shared.client.http.PostClient; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; +import com.aliyun.oss.common.utils.StringUtils; import com.aliyun.oss.model.ObjectMetadata; import com.ycl.api.CommonResult; import com.ycl.bo.AdminUserDetails; import com.ycl.common.dingding.DingCommon; +import com.ycl.config.DdTest; import com.ycl.config.DingConfig; import com.ycl.controller.BaseController; +import com.ycl.dto.video.OssFileDto; import com.ycl.entity.dingding.DingUserInfo; import com.ycl.service.auth.AuthService; import com.ycl.service.ding.BookRemarkService; import com.ycl.service.ding.DingService; +import com.ycl.service.oss.OssService; import com.ycl.utils.ConstantPropertiesUtils; +import com.ycl.utils.common.RandomUtils; import com.ycl.vo.AddressBookVO; import com.ycl.vo.NewAddressBookVO; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileItemFactory; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.MediaType; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.commons.CommonsMultipartFile; import javax.annotation.Resource; import java.io.*; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -73,7 +88,8 @@ private DingConfig dingConfig; - + @Autowired + private OssService ossService; @ApiOperation(value = "鏍规嵁閽夐拤鎺堟潈鐮佽幏鍙杢oken") @GetMapping("/dingLogin") @@ -174,111 +190,182 @@ return CommonResult.success(bookRemarkService.updateRemark(user, userId, remark)); } - private String getVideo(String MediaId,MultipartFile file) { + + @PostMapping("/oss/sign") + public CommonResult ossSign(@RequestBody OssFileDto dto) { + + if (StringUtils.isNullOrEmpty(dto.getMediaId()) || StringUtils.isNullOrEmpty(dto.getExtension())) { + return CommonResult.failed("鍙傛暟涓嶈兘涓虹┖锛�"); + } + ExecutableClient executableClient = DdTest.getExecutableClient(); CloseableHttpResponse response = null; - //璋冪敤API - GetClient getClient = executableClient.newGetClient(GET_VIDEO); - String token =getToken(); - //璁剧疆鍙傛暟 + OSS ossClient = null; + + DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + System.out.println("寮�濮嬭幏鍙�--" + dtf2.format(LocalDateTime.now())); try { - if (ObjectUtil.isNotNull(MediaId)) { - getClient.addParameter("accessToken", token); - getClient.addParameter("mediaId", MediaId); -// String result = getClient.get(); -// return parsingResult(result); + //浠庨拤閽夎幏鍙栬棰戞祦 + String api = "/media/download"; + GetClient getClient = executableClient.newGetClient(api); + //浠巖edis鑾峰彇token + String token = getToken(); + //璁剧疆鍙傛暟 + getClient.addParameter("access_token", token); + getClient.addParameter("media_id", dto.getMediaId()); response = getClient.getB(); HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); - File voice = new File("/Users/video/log1.xlsx"); - OutputStream fos = new FileOutputStream(voice); - int cache = 10 * 1024; - byte[] buffer = new byte[cache]; - int ch = 0; - while ((ch = is.read(buffer)) != -1) { - fos.write(buffer, 0, ch); - } - is.close(); - fos.flush(); - fos.close(); - } - if (file == null) { - return "涓婁紶瑙嗛涓虹┖"; - } - String endpoint = ConstantPropertiesUtils.END_POINT; - String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID; - String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET; - String bucketName = ConstantPropertiesUtils.BUCKET_NAME; - // 鍒涘缓OSSClient瀹炰緥 - OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); - // 鑾峰彇鏂囦欢鐨勫悕绉� - String fileName = "sczf/" + file.getOriginalFilename(); - ObjectMetadata objectMetadata = new ObjectMetadata(); - objectMetadata.setContentType(getcontentType(fileName.substring(fileName.lastIndexOf(".")))); - // 璋冪敤oss鐨勬柟娉曞疄鐜伴暱浼� - // 绗竴涓弬鏁� bucketName - // 绗簩涓弬鏁� 涓婁紶鍒皁ss鐨勬枃浠惰矾寰勫拰鏂囦欢鍚嶇О - ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(file.getBytes()), objectMetadata); - // 鍏抽棴OSSClient銆� - ossClient.shutdown(); - // 鎶婁笂浼犵殑鏂囦欢璺緞杩斿洖 锛堟墜鍔ㄦ嫾鎺ワ級 - // 杩欓噷璁剧疆鍥剧墖鏈夋晥鏃堕棿 鎴戣缃簡30骞� - Date expiration = new Date(System.currentTimeMillis() + (long) 946080000 * 1000); - String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString(); +// MultipartFile file = getMultipartFile(is, fileName); - return url; - }catch (Exception e) { + String url = ossService.uploadImages(is,dto.getExtension()); + return CommonResult.success(url); + } catch (Exception e) { e.printStackTrace(); - }finally { + System.out.println(e.getMessage()); + } finally { + if (ossClient != null) { + ossClient.shutdown(); + } if (response != null) { try { - //鐗瑰埆鎻愰啋锛氶渶瑕佽皟鐢╮esponse鐨刢lose鏂规硶鍏抽棴缃戠粶杩炴帴锛侊紒锛� response.close(); } catch (Exception e) { - e.printStackTrace(); + System.out.println(e); } } } return null; } - public static String getcontentType(String FilenameExtension) { - if (FilenameExtension.equalsIgnoreCase(".mp4")||FilenameExtension.equalsIgnoreCase(".mov")) { - return "video/mp4"; - } - if (FilenameExtension.equalsIgnoreCase(".bmp")) { - return "image/bmp"; - } - if (FilenameExtension.equalsIgnoreCase(".gif")) { - return "image/gif"; - } - if (FilenameExtension.equalsIgnoreCase(".jpeg") || - FilenameExtension.equalsIgnoreCase(".jpg") || - FilenameExtension.equalsIgnoreCase(".png")) { - return "image/jpg"; - } - if (FilenameExtension.equalsIgnoreCase(".html")) { - return "text/html"; - } - if (FilenameExtension.equalsIgnoreCase(".txt")) { - return "text/plain"; - } - if (FilenameExtension.equalsIgnoreCase(".vsd")) { - return "application/vnd.visio"; - } - if (FilenameExtension.equalsIgnoreCase(".pptx") || - FilenameExtension.equalsIgnoreCase(".ppt")) { - return "application/vnd.ms-powerpoint"; - } - if (FilenameExtension.equalsIgnoreCase(".docx") || - FilenameExtension.equalsIgnoreCase(".doc")) { - return "application/msword"; - } - if (FilenameExtension.equalsIgnoreCase(".xml")) { - return "text/xml"; - } - return "image/jpg"; + + @ApiOperation(value = "鑾峰彇瑙嗛") + @GetMapping("/getVideo/{mediaId}") + public CommonResult<String> getVideo(@PathVariable String mediaId) { + String accessToken = getToken(); + + + //executableClient淇濊瘉鍗曚緥 + IntelligentPostClient intelligentPostClient = executableClient.newIntelligentPostClient("/media/upload"); + OapiMediaUploadRequest oapiMediaUploadRequest = new OapiMediaUploadRequest(); + //璋冪敤鎺ュ彛鍑瘉 + oapiMediaUploadRequest.setAccess_token(accessToken); + //濯掍綋鏂囦欢绫诲瀷锛屽垎鍒湁鍥剧墖锛坕mage锛夈�佽闊筹紙voice锛夈�佹櫘閫氭枃浠�(file) + oapiMediaUploadRequest.setType("file"); + //orm-data涓獟浣撴枃浠舵爣璇嗭紝鏈塮ilename銆乫ilelength銆乧ontent-type绛変俊鎭� + oapiMediaUploadRequest.setMedia("filename"); + intelligentPostClient.addFile(new File("F:\\瑙嗛\\7鏈�29鏃�.mp4")); + //鑾峰彇缁撴灉 + OapiMediaUploadResponse apiResult = intelligentPostClient.post(oapiMediaUploadRequest); + + + MultipartFile file = getDownload(accessToken, mediaId); + String url = ossService.uploadImages(file); + return CommonResult.success(url); } + private MultipartFile getDownload(String access_token, String media_id) { + CloseableHttpResponse response = null; + try { + String api = "/media/download"; + GetClient getClient = executableClient.newGetClient(api); + //璁剧疆鍙傛暟 + getClient.addParameter("access_token", access_token); + getClient.addParameter("media_id", media_id); + response = getClient.getB(); + HttpEntity entity = response.getEntity(); + InputStream is = entity.getContent(); +// File voice = new File("D:/IMG_4401.MOV"); +// OutputStream fos = new FileOutputStream(voice); +// int cache = 10 * 1024; +// byte[] buffer = new byte[cache]; +// int ch = 0; +// while ((ch = is.read(buffer)) != -1) { +// fos.write(buffer, 0, ch); +// } +// is.close(); +// fos.flush(); +// fos.close(); + DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); + + String strDate2 = dtf2.format(LocalDateTime.now()); + String fileName = strDate2 + RandomUtils.generateRandomInt(4) + ".MOV"; + return getMultipartFile(is, fileName); + + } catch (Exception e) { + System.out.println(e); + } finally { + if (response != null) { + try { + //鐗瑰埆鎻愰啋锛氶渶瑕佽皟鐢╮esponse鐨刢lose鏂规硶鍏抽棴缃戠粶杩炴帴锛侊紒锛� + response.close(); + } catch (Exception e) { + System.out.println(e); + } + } + } + return null; + } + + /** + * 鑾峰彇灏佽寰桵ultipartFile + * + * @param inputStream inputStream + * @param fileName fileName + * @return MultipartFile + */ + public MultipartFile getMultipartFile(InputStream inputStream, String fileName) { + FileItem fileItem = createFileItem(inputStream, fileName); + //CommonsMultipartFile鏄痜eign瀵筸ultipartFile鐨勫皝瑁咃紝浣嗘槸瑕丗ileItem绫诲璞� + return new CommonsMultipartFile(fileItem); + } + + + /** + * FileItem绫诲璞″垱寤� + * + * @param inputStream inputStream + * @param fileName fileName + * @return FileItem + */ + public FileItem createFileItem(InputStream inputStream, String fileName) { + FileItemFactory factory = new DiskFileItemFactory(16, null); + String textFieldName = "file"; + FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName); + int bytesRead = 0; + byte[] buffer = new byte[8192]; + OutputStream os = null; + //浣跨敤杈撳嚭娴佽緭鍑鸿緭鍏ユ祦鐨勫瓧鑺� + try { + os = item.getOutputStream(); + while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) { + os.write(buffer, 0, bytesRead); + } + inputStream.close(); + } catch (IOException e) { + log.error("Stream copy exception", e); + throw new IllegalArgumentException("鏂囦欢涓婁紶澶辫触"); + } finally { + if (os != null) { + try { + os.close(); + } catch (IOException e) { + log.error("Stream close exception", e); + + } + } + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + log.error("Stream close exception", e); + } + } + } + + return item; + } } + -- Gitblit v1.8.0