public DTOBackFileSection SectionDownloadFile([FromBody] DTODownLoadFile dto) { string fileFullName = Environment.CurrentDirectory + "/LocalFile/" + dto.FileRelativePath; string requestIp = HttpContext.Connection.RemoteIpAddress.ToString(); if (_cache.Get(requestIp) != null) { throw new Exception("请求频率过高!"); } _cache.GetOrCreate <string>(requestIp, entry => { entry.SetAbsoluteExpiration(TimeSpan.FromSeconds(DownloadInterval)); return(string.Empty); }); if (System.IO.File.Exists(fileFullName)) { using (FileStream fs = System.IO.File.OpenRead(fileFullName)) { DTOBackFileSection result = new DTOBackFileSection(); fs.Position = dto.CurrPosition; result.CurrPosition = dto.CurrPosition; result.FileName = System.IO.Path.GetFileName(fs.Name); result.FileSize = fs.Length; result.FileBuffer = new byte[dto.CurrSize]; result.CurrSize = fs.Read(result.FileBuffer, 0, dto.CurrSize); return(result); } } else { throw new Exception("未找到文件"); } }
public DTOBackFileSection SectionDownloadUpgradeExe([FromBody] DTOUpgradeFileSectDownload dto) { string fileFullName = Environment.CurrentDirectory + "/LocalFile/UpgradeExe/UpgradeApp.zip"; if (System.IO.File.Exists(fileFullName)) { using (FileStream fs = System.IO.File.OpenRead(fileFullName)) { DTOBackFileSection result = new DTOBackFileSection(); fs.Position = dto.CurrPosition; result.CurrPosition = dto.CurrPosition; result.FileName = System.IO.Path.GetFileName(fs.Name); result.FileSize = fs.Length; result.FileBuffer = new byte[dto.CurrSize]; result.CurrSize = fs.Read(result.FileBuffer, 0, dto.CurrSize); return(result); } } else { throw new Exception("未找到升级程序"); } }
public DTOBackFileSection SectionDownloadUpgradeBag([FromBody] DTOUpgradeFileSectDownload dto) { //缓存 string bytesKey = "UpgradeBag_" + dto.VersionId.ToString().ToUpper(); string nameKey = "UpgradeBagName_" + dto.VersionId.ToString().ToUpper(); List <byte> bytes = _cache.Get <List <byte> >(bytesKey); if (bytes == null) { Prc_VersionInfo versionInfo; Repository <Prc_VersionInfo> repository = new Repository <Prc_VersionInfo>(DbContext); versionInfo = repository.Get(a => a.ID == dto.VersionId); if (versionInfo == null) { throw new Exception("未找到版本信息"); } string fileFullName = Environment.CurrentDirectory + "/LocalFile/UpgradeBag/" + versionInfo.UpgradeBagName; if (!System.IO.File.Exists(fileFullName)) { throw new Exception("未找到文件"); } //using () using (FileStream fs = System.IO.File.OpenRead(fileFullName)) { byte[] bs = new byte[fs.Length]; fs.Read(bs, 0, bs.Length); bytes = bs.ToList(); //写入字节数组 _cache.GetOrCreate(bytesKey, entry => { entry.SetSlidingExpiration(TimeSpan.FromHours(1)); return(bytes); }); } } string fileName = _cache.Get <string>(nameKey); if (fileName == null) { Prc_VersionInfo versionInfo; Repository <Prc_VersionInfo> repository = new Repository <Prc_VersionInfo>(DbContext); versionInfo = repository.Get(a => a.ID == dto.VersionId); if (versionInfo == null) { throw new Exception("未找到版本信息"); } fileName = versionInfo.UpgradeBagName; _cache.GetOrCreate(nameKey, entry => { entry.SetSlidingExpiration(TimeSpan.FromHours(1)); return(fileName); }); } DTOBackFileSection result = new DTOBackFileSection(); //fs.Position = dto.CurrPosition; result.CurrPosition = dto.CurrPosition; result.FileName = fileName; result.FileSize = bytes.Count; if (bytes.Count <= dto.CurrPosition) { result.CurrSize = 0; result.FileBuffer = new byte[] { }; } else { if (bytes.Count <= dto.CurrPosition + dto.CurrSize) { result.FileBuffer = bytes.GetRange((int)dto.CurrPosition, (int)(bytes.Count - dto.CurrPosition)).ToArray(); result.CurrSize = (int)(bytes.Count - dto.CurrPosition); } else { result.FileBuffer = bytes.GetRange((int)dto.CurrPosition, dto.CurrSize).ToArray(); result.CurrSize = dto.CurrSize; } } return(result); }