protected virtual void GetBase64(SocialContact.Domain.Core.UserFileInfo obj) { if (Request.Form.Files.Count == 1) { string suffix = Request.Form.Files[0].FileName.Split('.').LastOrDefault(); obj.Src = $"{RandomHelper.OrderId.Sha1()}.{suffix}"; obj.FileId = RandomHelper.OrderId.Sha1(); using System.IO.Stream stream = Request.Form.Files[0].OpenReadStream(); byte[] buffer = new Byte[stream.Length]; stream.Read(buffer); obj.Base64 = Base64Helper.Base64String(buffer); } else { var src = obj.FileId == null?null: this.RedisCache.GetHashValue("file", obj.FileId); if (string.IsNullOrEmpty(src)) { var temp = this.Cache.Get <List <UserFileEntry> >(Core.FileChannel).Find(it => it.FileName == obj.FileId); src = temp?.FileSrc ?? string.Empty; } else { var strs = src.Split('_'); obj.Src = strs[0]; src = obj.Src; obj.Type = strs.Length == 2 ? strs[1] : null; } //byte[] buffer = this._httpClientFactory.CreateClient().GetAsync(this._fileAddress).Result.Content.ReadAsByteArrayAsync().Result; //obj.Base64 = Base64Utils.Base64String(System.IO.File.ReadAllBytes(buffer)); if (System.IO.File.Exists($"{AddressConfig.UploadImgDirectory}//{ src}")) { obj.Base64 = Base64Helper.Base64String(System.IO.File.ReadAllBytes($"{AddressConfig.UploadImgDirectory}//{ src}")); } } }
public IActionResult RsaEncrypt(string hexKey, string val, string sign) { string publicXml = "<RSAKeyValue><Modulus>{0}</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"; publicXml = string.Format(publicXml, Base64Helper.Base64String(HexHelper.ToByte(hexKey))); var res = SecurityHelper.RsaEncrypt(128, val, publicXml, sign); //var res = SecurityUtils.RsaEncryptToByte(val, RsaSecurity.RSAPublicKeyJava2DotNet(HexUtils.ToByte(hexKey))); return(new JsonResult(new { en = res })); }
public IActionResult HexToBase64(string key) { return(new JsonResult(new { key = Base64Helper.Base64String(HexHelper.ToByte(key)) })); }
UserFileInfo Set(UserFileInfo fileInfo, UserInfo user = null, bool add = true) { fileInfo.User = add?null: user; string str = base.RedisCache.GetHashValue("file", fileInfo.FileId); if (add) { str = str ?? throw new Exception("用户文件不存在"); } UserFileInfo parent = null; UserFileInfo pic = null; if (str != null) { string[] strs = str.Split('_'); int index = str.IndexOf("_"); fileInfo.Src = str.Substring(0, index); fileInfo.Type = str.Substring(index + 1); fileInfo.Base64 = Base64Helper.Base64String(System.IO.File.ReadAllBytes($"{AddressConfig.UploadImgDirectory}\\{fileInfo.Src}")); parent = base.UnitWork.FindSingle <UserFileInfo>(it => it.Base64 == fileInfo.Base64); if (!add) { //pic = base.UnitWork.FindSingle<UserFileInfo>(it => it.User.Id == user.Id && it.FileId == fileInfo.FileId && it.Type == fileInfo.Type); pic = base.UnitWork.FindSingle <UserFileInfo>(it => it.FileId == fileInfo.FileId && it.Type == fileInfo.Type); if (pic == null) { throw new Exception("用户文件不存在"); } } } else { // pic = base.UnitWork.FindSingle<UserFileInfo>(it => it.User.Id == user.Id && it.FileId == fileInfo.FileId); pic = base.UnitWork.FindSingle <UserFileInfo>(it => it.FileId == fileInfo.FileId); if (pic == null) { throw new Exception("用户文件不存在"); } parent = pic.Parent; } if (parent == null) { parent = fileInfo; parent.Id = (int)base.UnitWork.Insert(parent); if (add) { pic = (UserFileInfo)parent.Clone(); pic.Id = (int)base.UnitWork.Insert(pic); } } else { if (add) { pic = fileInfo; pic.Parent = parent; pic.Base64 = null; pic.Id = (int)base.UnitWork.Insert(pic); } } if (add) { var userFileEntry1 = this._userFileService.ToUserFileEntry(pic); this._userFileService.Publish(userFileEntry1); return(pic); } else { if (parent != null) { if (pic.Parent.Base64 != parent.Base64) { pic.Parent = parent; } } } pic.FileId = RandomHelper.OrderId.Sha1(); pic.Src = $"{RandomHelper.OrderId.Sha1()}.{pic.Parent.Src.Split('.').LastOrDefault()}"; base.UnitWork.Update <UserFileInfo>(it => it.Id == pic.Id, it => new UserFileInfo() { Parent = pic.Parent, FileId = pic.FileId, Src = pic.Src, UpdateDate = DateTime.Now }); var userFileEntry = this._userFileService.ToUserFileEntry(pic); this._userFileService.Publish(userFileEntry); return(pic); }