/// <summary> /// 上传一个图片,保存并返回图片信息新生成的文件名 /// </summary> /// <param name="file"></param> /// <param name="filters"></param> /// <param name="maxSize"></param> /// <param name="dirPath"></param> /// <returns></returns> private Dictionary <string, string> Save(HttpPostedFileBase file, Array filters, int maxSize, string dirPath) { Dictionary <string, string> result = new Dictionary <string, string>(); dirPath = Server.MapPath(dirPath); //FileCommon.ExistsCreate(dirPath); if (file == null) { result.Add("code", "1"); result.Add("msg", "没有文件"); return(result); } if (file.InputStream == null || file.InputStream.Length > maxSize) { result.Add("code", "1"); result.Add("msg", "上传文件大小超过限制"); return(result); } //file.InputStream可以获取到System.io.Stream对象,由此可以对文件进行hash加密运算 string fileName = file.FileName, fileExt = Path.GetExtension(fileName).ToLower();//扩展名 if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(filters, fileExt.Substring(1).ToLower()) == -1) { result.Add("code", "1"); result.Add("msg", "上传文件扩展名是不允许的扩展名"); return(result); } string newFileName = string.Empty, //新生成的文件名 filePath = string.Empty; if (string.IsNullOrEmpty(dirPath)) { result.Add("code", "1"); result.Add("msg", "服务器异常"); return(result); } newFileName = FileCommon.CreateFileNameByTime() + fileExt; filePath = dirPath + newFileName; file.SaveAs(filePath); //TODO 这里需要检测有没有什么异常 MusicInfo music = MusicCommon.GetFileInfo(filePath); music.Size = file.ContentLength; music.Type = fileExt.Substring(1); //if (string.IsNullOrEmpty(music.Length)) //{ // //测试了10个左右的文件,发现木有音乐文件长度不存在的情况 // if (music.BitRate == 0) // { // result.Add("code", "2"); // result.Add("msg", "文件异常(无法读取到正确的文件信息)"); // return result; // } // else // { // //尝试转换 // //这里得到的是字节么? // music.Length = (music.Size * 1024 / music.BitRate * 8).ToString(); //歌曲时长 = 文件大小(mb)/ 位率(KBPS) * 8 // System.Diagnostics.Debug.WriteLine(music.Length); // } //} result.Add("code", "0"); result.Add("path", filePath); result.Add("dir", dirPath); result.Add("name", newFileName); result.Add("data", JavaScriptCommon.Serialize(music)); return(result); }
public async Task <ActionResult> FaceContrast([FromBody] FaceContrastViewModel model) { RequestFaceModel request = new RequestFaceModel() { Status = 500, Message = null, FaceList = null }; #region 基本验证 // 模型验证 if (!ModelState.IsValid) { request.Message = ModelState.Where(e => e.Value.Errors.Count > 0).FirstOrDefault().Value.Errors[0].ErrorMessage; request.Status = (int)RequestEnum.必要参数未传入; return(Ok(request)); } // 根据 secretkey 获取set var userFaceSet = await Service_user_face_set.GetUser_Face_Set_By_Secret_key_Async(model.secret_id); if (userFaceSet == null) { // 您未开通此项目 request.Enum = RequestEnum.没有接口权限; return(Ok(request)); } // 签名判断 var dic = ModleToDictionary.GetProperties(model); var key = userFaceSet.secret_key; dic.Remove("face_field"); dic.Remove("sign"); var sign = Signature.GetSign(dic, key); if (sign != model.sign) { // 签名错误 request.Enum = RequestEnum.签名错误; return(Ok(request)); } #endregion #region 余额逻辑判断 // 根据 userFaceSet.user_id 获取用户信息 var user = await Service_user.GetUser_By_User_id_Async(userFaceSet.user_id); //Service_user_face_compare_prepayment // 根据user_id 先查预付款,未查询到则扣费使用 var prepayment = await Service_user_face_compare_prepayment.GetUser_Face_Compare_Prepayment_By_User_id(user.id); user_face_compare_price comparePrice = null; if (prepayment == null || prepayment.number <= 0) { prepayment = null; comparePrice = await Service_user_face_compare_price.GetUser_Face_Compare_Price_By_User_idAsync(user.id); if (comparePrice == null) { // 用户没有预付费次数 request.Enum = RequestEnum.没有预付费次数未开通余额付款; } else { // 查询用户余额 derectPrice.price>余额 if (user.use_amount < comparePrice.price) { // 预付费次数为0,余额不足 request.Enum = RequestEnum.预付费次数为0且余额不足; } } } // 用户余额验证 if (!string.IsNullOrEmpty(request.Message)) { return(Ok(request)); } #endregion #region 获取图片 List <(Bitmap, string)> bitmaps = new List <(Bitmap, string)>(); List <ContrastData> face_tokens = new List <ContrastData>(); for (int i = 0; i < model.images.Count; i++) { var image = model.images[i]; var face_token = string.Empty; (Bitmap, string)bitmap = (null, null); // 获取图片文件 bitmap = image.image_type.ToUpper() == "BASE64" ? FileCommon.Base64ToBitmap(image.image) : FileCommon.UrlToBitmap(image.image); if (bitmap.Item1 == null && !string.IsNullOrEmpty(bitmap.Item2)) { if (image.image_type.ToUpper() == "BASE64") { request.Enum = RequestEnum.Base64图片格式错误; } else { request.Enum = RequestEnum.从图片的url下载图片失败; } return(Ok(request)); } #endregion #region 图片以及数据处理 // 查找是否有相同的图片 face_token = image.image_type.ToUpper() == "BASE64" ? EncryptProvider.Md5(image.image) : EncryptProvider.Md5(FileCommon.BitmapToBase64(bitmap.Item1)); // 查询 图片库 // 判断是否还需要进行图片存储 var user_images = await Service_user_face_image_data.Getuser_face_image_data_By_Token_Async(face_token); if (user_images != null) { bitmap.Item2 = user_images.url; } else { try { // 存入本地 bitmap.Item2 = FileCommon.SaveBitmap(bitmap.Item1, bitmap.Item2); } catch (Exception ex) { LogHelperNLog.Error(ex); request.Enum = RequestEnum.数据存储处理失败; return(Ok(request)); } } bitmaps.Add(bitmap); face_tokens.Add(new ContrastData() { face_token = face_token }); } #region old Code //foreach (var image in model.images) //{ // // 变量初始化 // var face_token = string.Empty; // (Bitmap, string) bitmap = (null, null); // bitmap = image.image_type.ToUpper() == "BASE64" ? FileCommon.Base64ToBitmap(image.image) : FileCommon.UrlToBitmap(image.image); // if (bitmap.Item1 == null && !string.IsNullOrEmpty(bitmap.Item2)) // { // if (image.image_type.ToUpper() == "BASE64") // { // request.Enum = RequestEnum.Base64图片格式错误; // request.Message = string.IsNullOrEmpty(request.Message) ? num + RequestEnum.Base64图片格式错误.ToString() : request.Message + num + RequestEnum.Base64图片格式错误.ToString(); // } // else // { // request.Enum = RequestEnum.从图片的url下载图片失败; // request.Message = string.IsNullOrEmpty(request.Message) ? num + RequestEnum.从图片的url下载图片失败.ToString() : request.Message + num + RequestEnum.从图片的url下载图片失败.ToString(); // } // return Ok(request); // } // bitmaps.Add(bitmap); //} //#endregion //#region 图片以及数据处理 //List<ContrastData> face_tokens = new List<ContrastData>(); //for (int i = 0; i < bitmaps.Count; i++) //{ // var face_token = string.Empty; // face_token = EncryptProvider.Md5(FileCommon.BitmapToBase64(bitmaps[i].Item1)); // //此处后期要更改为人脸库的图片 // var detect_Log = await Service_user_face_detect_log.GetUser_Face_Detect_Log_By_Face_token(face_token); // if (detect_Log != null) // { // bitmaps[i] = (bitmaps[i].Item1, detect_Log.image); // //bitmaps[i].Item2 = detect_Log.image; // } // else // { // try // { // // 存入本地 // bitmaps[i] = (bitmaps[i].Item1, FileCommon.SaveBitmap(bitmaps[i].Item1, bitmaps[i].Item2)); // } // catch (Exception ex) // { // LogHelperNLog.Error(ex); // request.Enum = RequestEnum.数据存储处理失败; // return Ok(request); // } // } // face_tokens.Add(new ContrastData() { // face_token=face_token // }); //} #endregion var difference = DnnFaceRecognitionCompute.GetOutputLabels(bitmaps[0].Item1, bitmaps[1].Item1); // 是否需要人脸位置 if (model.face_field.ToUpper().Contains("FACE_SHAPE")) { for (int i = 0; i < face_tokens.Count; i++) { face_tokens[i].rectangles = difference.Item2[i]; } } request.FaceList = face_tokens; request.Score = difference.Item1; #endregion request.Enum = RequestEnum.Succeed; //日志&&扣费 try { // 预付费/扣费 log // 时间戳 DateTime dateTime = DateTime.Now; user_face_compare_prepayment_detail prepayment_Detail = null; user_amount_detail amount_Detail = null; if (prepayment != null) { prepayment_Detail = new user_face_compare_prepayment_detail() { user_id = user.id, change_number = -1, now_number = prepayment.number - 1, info = string.Format("应用ID:{0}", model.secret_id), created_at = dateTime, updated_at = dateTime }; } else { amount_Detail = new user_amount_detail() { user_id = user.id, change_amount = -comparePrice.price, now_amount = user.use_amount - comparePrice.price, info = string.Format("应用ID:{0},费用:{1}", model.secret_id, comparePrice.price), created_at = dateTime, updated_at = dateTime, type = 0 }; } // 操作信息 user_face_compare_log face_Compare_Log = new user_face_compare_log() { user_id = user.id, secret_id = model.secret_id, image = JsonConvert.SerializeObject(bitmaps.Select(x => x.Item2)), is_deductions_success = true, api_respone = JsonConvert.SerializeObject(request, UnderlineSplitContractResolver.GetSettings()), timestamp = Timelong.GetUnixDateTime(model.timestamp), sign = sign, created_at = dateTime, updated_at = dateTime, }; // 修改余额,存库,日志 using (var tra = SqlDapperHelper.GetOpenConnection().BeginTransaction()) { if (prepayment != null) { await SqlDapperHelper.ExecuteInsertAsync(prepayment_Detail, tra); await Service_user_face_compare_prepayment.MinusUsernumber_By_User_id(prepayment_Detail, tra); } else { await SqlDapperHelper.ExecuteInsertAsync(amount_Detail, tra); await Service_user.MinusUserAmount_By_User_id(amount_Detail); } await SqlDapperHelper.ExecuteInsertAsync(face_Compare_Log, tra); tra.Commit(); } } catch (Exception ex) { LogHelperNLog.Error(ex, "操作日志添加失败"); request.Enum = RequestEnum.操作日志添加失败; return(Ok(request)); } return(Ok(request)); }
public static void UpdateMetadata(this FileCommon file, FileCommon fileToCopy) { file.InRecycleBin = fileToCopy.InRecycleBin; file.IsPublic = fileToCopy.IsPublic; }