Exemplo n.º 1
0
        public IResult Add(IFormFile image, CarImage carImage)
        {
            var imageCount = _carImageDal.GetAll(c => c.CarId == carImage.CarId).Count;

            if (imageCount >= 5)
            {                                                                              //5 den fazla fotoğraf olmasın diye yazdığımız kural
                return(new ErrorResult(Messages.ImageLimitExceded));
            }

            var imageResult = ImageUploadHelper.Upload(image);

            carImage.ImagePath = imageResult.Message;                       //Upload dan gelen mesaj bir dosya yoludur.
            _carImageDal.Add(carImage);
            return(new SuccessResult(Messages.ImageAded));
        }
Exemplo n.º 2
0
        private IResult CarImageFileControl(IFormFile image, CarImage carImage)
        {
            if (image == null)
            {
                return(new ErrorResult(Messages.ImageNotFound));
            }
            if (carImage.CarId == 0)
            {
                return(new ErrorResult(Messages.ImageIdNotFound));
            }

            var imageResult = ImageUploadHelper.Upload(image);

            carImage.ImagePath = imageResult.Message;//Upload dan gelen mesaj bir dosya yoludur.
            carImage.Date      = DateTime.Now;

            return(new SuccessResult());
        }
 public void Execute(CommandContext context)
 {
     try
     {
         if (!File.Exists(FilePath))
         {
             ConsoleHelper.PrintError($"文件不存在:{FilePath}");
         }
         else
         {
             var imgUrl = ImageUploadHelper.Upload(FilePath);
             ConsoleHelper.PrintMsg($"{FilePath} 上传成功. {imgUrl}");
         }
     }
     catch (Exception e)
     {
         ConsoleHelper.PrintError(e.Message);
     }
 }
Exemplo n.º 4
0
 public void Execute(CommandContext context)
 {
     try
     {
         var tasks = new List <Task <string> >();
         foreach (var filePath in FilePaths)
         {
             // 使用多线程上传
             tasks.Add(Task.Run(() =>
             {
                 try
                 {
                     if (!File.Exists(filePath))
                     {
                         ConsoleHelper.PrintError($"文件不存在:{filePath}");
                         return(null);
                     }
                     else
                     {
                         var imgUrl = ImageUploadHelper.Upload(filePath);
                         return(imgUrl);
                     }
                 }
                 catch
                 {
                     return("");
                 }
             }));
         }
         Task.WaitAll(tasks.ToArray());
         foreach (var task in tasks)
         {
             ConsoleHelper.PrintMsg($"{task.Result}");
         }
     }
     catch (Exception e)
     {
         ConsoleHelper.PrintError(e.Message);
     }
 }
        public void Execute(CommandContext context)
        {
            try
            {
                if (!File.Exists(FilePath))
                {
                    ConsoleHelper.PrintError($"文件不存在:{FilePath}");
                }
                else
                {
                    var fileDir     = new FileInfo(FilePath).DirectoryName;
                    var fileContent = File.ReadAllText(FilePath);
                    var imgHandler  = new ImageHandler();
                    var imgList     = imgHandler.Process(fileContent);

                    ConsoleHelper.PrintMsg($"提取图片成功,共 {imgList.Count} 个。");

                    //循环上传图片
                    foreach (var img in imgList)
                    {
                        if (img.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                        {
                            ConsoleHelper.PrintMsg($"图片跳过:{img} ");
                            continue;
                        }

                        try
                        {
                            var imgPhyPath = Path.Combine(fileDir !, img);
                            if (File.Exists(imgPhyPath))
                            {
                                var imgUrl = ImageUploadHelper.Upload(imgPhyPath);
                                if (!ReplaceDic.ContainsKey(img))
                                {
                                    ReplaceDic.Add(img, imgUrl);
                                }
                                ConsoleHelper.PrintMsg($"{img} 上传成功. {imgUrl}");
                            }
                            else
                            {
                                ConsoleHelper.PrintMsg($"{img} 未发现文件.");
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }

                    //替换
                    fileContent = ReplaceDic.Keys.Aggregate(fileContent, (current, key) => current.Replace(key, ReplaceDic[key]));

                    var newFileName = FilePath.Substring(0, FilePath.LastIndexOf('.')) + "-cnblog" + Path.GetExtension(FilePath);
                    File.WriteAllText(newFileName, fileContent, FileEncodingType.GetType(FilePath));

                    ConsoleHelper.PrintMsg($"处理完成!文件保存在:{newFileName}");
                }
            }
            catch (Exception e)
            {
                ConsoleHelper.PrintError(e.Message);
            }
        }