コード例 #1
0
        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="saveModel"></param>
        public void SaveModel(InusekeyinfoModel saveModel)
        {
            if (saveModel == null)
            {
                return;
            }
            using (var conn = SqlConnectionHelper.GetOpenConnection())
            {
                saveModel.UseYear  = saveModel.UseDate.Year;
                saveModel.UseMonth = saveModel.UseDate.Month;
                saveModel.UseDay   = saveModel.UseDate.Day;
                if (saveModel.Id < 1)
                {
                    //新增
                    var newId = conn.Insert(saveModel);
                    if (newId != null)
                    {
                        saveModel.Id = newId.Value;
                    }
                }

                //修改
                conn.Update(saveModel);
            }
        }
コード例 #2
0
        /// <summary>
        /// 处理新上传的图片资源
        /// </summary>
        /// <param name="idList"></param>
        public async Task DealNewResourceAsync(List <long> idList)
        {
            if (idList.Count < 1)
            {
                return;
            }
            var resourceList = _dataAccess.GetDailyStoryResourceModelsById(idList);

            if (resourceList.Count < 1)
            {
                return;
            }
            var dictServer    = new SysDicService();
            var inUserService = new InuseKeyInfoService();
            var dictList      = dictServer.GetAllDict("FtpConfig");

            if (dictList == null || dictList.Count < 1)
            {
                return;
            }
            var ftpUrl         = dictList.FirstOrDefault(f => f.Lable == "FtpUrl")?.Value;
            var ftpUserName    = dictList.FirstOrDefault(f => f.Lable == "FtpUserName")?.Value;
            var ftpPassword    = dictList.FirstOrDefault(f => f.Lable == "FtpPassword")?.Value;
            var ftpViewPre     = dictList.FirstOrDefault(f => f.Lable == "FtpViewPre")?.Value;
            var tinifyCodeList = dictServer.GetAllDict("TinifyKey")?.OrderBy(f => f.Sort).ToList();

            if (tinifyCodeList == null || tinifyCodeList.Count < 1)
            {
                return;
            }
            var newTime          = DateTime.Now;
            var tiniCodeUseLimit = 500;
            var inUseModel       = inUserService.GetModels("TinifyKey", null, newTime.Year, newTime.Month)?.FirstOrDefault(f => f.UseCount < tiniCodeUseLimit);

            if (inUseModel == null || tinifyCodeList.All(f => f.Value != inUseModel.KeyInfo))
            {
                inUseModel = new InusekeyinfoModel {
                    CreateTime = newTime, IsDel = FlagEnum.HadZore, KeyInfo = tinifyCodeList[0].Value, KeyType = "TinifyKey", UseCount = 0, UseDate = newTime
                };
                inUserService.SaveModel(inUseModel);
                if (inUseModel.Id < 1)
                {
                    Console.WriteLine("保存失败");
                    return;
                }
            }
            foreach (var model in tinifyCodeList)
            {
                if (model.Value == inUseModel.KeyInfo)
                {
                    model.IsDel = FlagEnum.HadOne.GetHashCode();
                }
            }
            Tinify.Key = inUseModel.KeyInfo;
            var ftpServer       = new FtpUpLoadFiles(ftpUrl, ftpUserName, ftpPassword);
            var allowExtensions = new List <string> {
                ".jpeg", ".png", ".jpg"
            };

            foreach (var f in resourceList)
            {
                //压缩图片
                if (string.IsNullOrEmpty(f.Url))
                {
                    continue;
                }
                var tempPath = HostingEnvironment.MapPath(f.Url);
                if (string.IsNullOrEmpty(tempPath))
                {
                    continue;
                }
                var fileExtension = Path.GetExtension(tempPath)?.ToLower();
                if (fileExtension == null)
                {
                    continue;
                }
                var    sourceData = File.ReadAllBytes(tempPath);
                byte[] resultData = null;
                //判断文件扩展名是否能够进行压缩
                var couldCompress = allowExtensions.Select(x => x.ToLower()).Contains(fileExtension);
                if (couldCompress)
                {
                    try
                    {
                        resultData = await Tinify.FromBuffer(sourceData).ToBuffer();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        continue;
                    }
                    //该code已经用完了
                    if (Tinify.CompressionCount >= tiniCodeUseLimit)
                    {
                        //变更原有的状态
                        inUseModel.IsDel    = FlagEnum.HadOne;
                        inUseModel.UseCount = (int)Tinify.CompressionCount;
                        inUserService.SaveModel(inUseModel);

                        //重新生成
                        var tempKey = tinifyCodeList.FirstOrDefault(r => r.IsDel == FlagEnum.HadZore.GetHashCode());
                        if (tempKey == null)
                        {
                            continue;
                        }
                        tempKey.IsDel = FlagEnum.HadOne.GetHashCode();
                        inUseModel    = new InusekeyinfoModel {
                            CreateTime = newTime, IsDel = FlagEnum.HadZore, KeyInfo = tempKey.Value, KeyType = "TinifyKey", UseCount = 0, UseDate = newTime
                        };
                        inUserService.SaveModel(inUseModel);
                    }
                    else
                    {
                        inUseModel.UseCount = (int)(Tinify.CompressionCount ?? 0);
                        inUserService.SaveModel(inUseModel);
                    }
                }
                //上传
                var indexOfName = f.Url.LastIndexOf("/", StringComparison.Ordinal);
                if (indexOfName < 0)
                {
                    continue;
                }
                var fileName       = f.Url.Substring(indexOfName);
                var transferFinish = ftpServer.UploadFile("", couldCompress ? resultData : sourceData, fileName);
                if (transferFinish)
                {
                    //上传完成
                    f.FullUrl = ftpViewPre + fileName;
                    SaveModel(f);
                }
            }
        }
コード例 #3
0
 /// <summary>
 /// 保存信息
 /// </summary>
 /// <param name="saveModel"></param>
 public void SaveModel(InusekeyinfoModel saveModel)
 {
     _dataAccess.SaveModel(saveModel);
 }