|
@@ -1,10 +1,30 @@
|
|
|
package com.fdkankan.agent.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.fdkankan.agent.entity.VaildCameras;
|
|
|
import com.fdkankan.agent.mapper.IVaildCamerasMapper;
|
|
|
import com.fdkankan.agent.service.IVaildCamerasService;
|
|
|
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fdkankan.common.constant.CameraConstant;
|
|
|
+import com.fdkankan.common.constant.ConstantFilePath;
|
|
|
+import com.fdkankan.common.constant.ErrorCode;
|
|
|
+import com.fdkankan.common.exception.BusinessException;
|
|
|
+import com.fdkankan.common.util.AesUtil;
|
|
|
+import com.fdkankan.common.util.FileUtil;
|
|
|
+import com.fdkankan.common.util.FileUtils;
|
|
|
+import com.fdkankan.common.util.RSAEncrypt;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -17,4 +37,74 @@ import org.springframework.stereotype.Service;
|
|
|
@Service
|
|
|
public class VaildCamerasServiceImpl extends ServiceImpl<IVaildCamerasMapper, VaildCameras> implements IVaildCamerasService {
|
|
|
|
|
|
+ private static Logger log = LoggerFactory.getLogger("programLog");
|
|
|
+
|
|
|
+ @Value("${main.url:https://test.4dkankan.com/}")
|
|
|
+ private String mainUrl;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HashMap<String,Object> getVaildCameras(String file, String companyName) throws Exception{
|
|
|
+ if(StringUtils.isEmpty(file) || StringUtils.isEmpty(companyName)){
|
|
|
+ log.info("file:" + file);
|
|
|
+ log.info("companyName:" + companyName);
|
|
|
+ throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
|
|
|
+ }
|
|
|
+ List<VaildCameras> vaildCamerasEntityList = getListByCompanyName(companyName);
|
|
|
+ if(vaildCamerasEntityList == null || vaildCamerasEntityList.size() <= 0){
|
|
|
+ throw new BusinessException(CameraConstant.FAILURE_6011);
|
|
|
+ }
|
|
|
+ //生成需要加密的文件
|
|
|
+ UUID uuid = UUID.randomUUID();
|
|
|
+ String key = uuid.toString().replace("-","").substring(0,16);
|
|
|
+ JSONArray array = new JSONArray();
|
|
|
+ JSONObject object = null;
|
|
|
+ for(VaildCameras vaildCamerasEntity: vaildCamerasEntityList){
|
|
|
+ object = new JSONObject();
|
|
|
+ object.put("Devicemac", vaildCamerasEntity.getDevicemac());
|
|
|
+ array.add(object);
|
|
|
+ }
|
|
|
+ String aesData = AesUtil.encryptAES(array.toString(), key, key);
|
|
|
+
|
|
|
+ String dataPath = ConstantFilePath.AGENT_PATH + companyName + File.separator + System.currentTimeMillis() + "_" + Math.random()*100 + "_key.txt";
|
|
|
+ FileUtils.writeFile(dataPath, key);
|
|
|
+
|
|
|
+ String secretPath = ConstantFilePath.AGENT_PATH + companyName + "/secret/";
|
|
|
+ String fileName = System.currentTimeMillis() + "_public.pem";
|
|
|
+ File targetFile = new File(secretPath + fileName);
|
|
|
+ if(!targetFile.getParentFile().exists()){
|
|
|
+ targetFile.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ FileUtils.writeFile(secretPath + fileName, file);
|
|
|
+ if(!new File(secretPath + fileName).exists()){
|
|
|
+ throw new BusinessException(ErrorCode.FILE_NOT_EXIST);
|
|
|
+ }
|
|
|
+
|
|
|
+ byte[] data = RSAEncrypt.encrypt(RSAEncrypt.loadPublicKeyByStr(RSAEncrypt.loadPublicKeyByFile(secretPath + fileName)),
|
|
|
+ FileUtil.toByteArray3(dataPath));
|
|
|
+
|
|
|
+ //加密后的文件地址
|
|
|
+ String resultPath = ConstantFilePath.AGENT_PATH + companyName + "/secretData/";
|
|
|
+ String resultName = System.currentTimeMillis() + "_data.txt";
|
|
|
+ FileUtils.writeFile(resultPath, resultName, data);
|
|
|
+ HashMap<String,Object> map = new HashMap<>();
|
|
|
+ map.put("aecData", aesData);
|
|
|
+ map.put("keyUrl", mainUrl + "agent/" + companyName + "/secretData/" + resultName);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String addVaildCameras(String macId, String wifiName, String companyName) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String deleteVaildCameras(String macId, String companyName) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<VaildCameras> getListByCompanyName(String companyName){
|
|
|
+ QueryWrapper<VaildCameras> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.lambda().eq(VaildCameras::getCompanyName,companyName);
|
|
|
+ return this.list(queryWrapper);
|
|
|
+ }
|
|
|
}
|