Пример #1
0
        public async Task <IActionResult> UpImage([FromForm] UploadModel model)
        {
            if (!ModelState.IsValid)
            {
                ApiData.code = 1005;
                ApiData.msg  = ModelState.GetErrMsg();
                return(Ok(ApiData));
            }
            if (Request.Form.Files == null || Request.Form.Files.Count == 0 || !Request.Form.Files[0].IsImage())
            {
                ApiData.code = 1006;
                ApiData.msg  = "请上传图片文件";
                return(Ok(ApiData));
            }
            var bucket = _bucketService.GetBucketBayName(model.bucket);

            if (bucket == null)
            {
                ApiData.code = 2001;
                ApiData.msg  = "bucket错误";
                return(Ok(ApiData));
            }

            IFormFile file = Request.Form.Files[0];
            string    sha1 = file.GetSHA1();
            var       item = _bucketImageService.GetSHA1(sha1);

            if (item != null)
            {
                ApiData.code = 0;
                ApiData.msg  = "上传成功";
                ApiData.data = new { url = $"/oss/imagecn{item.visiturl}" };
                return(Ok(ApiData));
            }

            if (!ValidSignature(model.signature, file.GetMD5(), model.VERB))
            {
                ApiData.code = 1005;
                ApiData.msg  = "签名验证失败";
                return(Ok(ApiData));
            }

            uint   crc32 = Crc32Algorithm.Compute(EncryptorHelper.GetMD5Byte(Guid.NewGuid().ToString()));
            var    dir   = Math.Abs(crc32) % 256;//256个子目录
            string f_dir = Math.Abs(crc32).ToString();


            string path = System.IO.Path.Combine(MediaItemConfig.RootDir, bucket.name, dir.ToString(), f_dir);
            //保存文件并且获取文件的相对存储路径
            var image = file.CreateImagePathFromStream(_mediaItemStorage, path);


            string visiturl = $"/oss/imagecn/{bucket.name}/{dir}/{f_dir}/{image.NewFileName}";

            _bucketImageService.AddImage(new Entities.bucket_image()
            {
                id            = CombGuid.NewGuidAsString(),
                bucket_id     = bucket.id,
                creation_time = DateTime.Now,
                ext_name      = image.ExtName,
                sha1          = sha1,
                visiturl      = visiturl,
                io_path       = image.IOPath,
                width         = image.Width,
                height        = image.Height,
                length        = file.Length
            });

            ApiData.code = 0;
            ApiData.msg  = "上传成功";
            ApiData.data = new { url = visiturl };
            await Task.FromResult(0);

            return(Ok(ApiData));
        }
Пример #2
0
        public async Task <IActionResult> UpImage([FromForm] UploadModel model)
        {
            #region 数据验证

            if (!ModelState.IsValid)
            {
                return(NoValid());
            }
            if (Request.Form.Files == null || Request.Form.Files.Count == 0 || !Request.Form.Files[0].IsImage())
            {
                ApiData.code = 1006;
                ApiData.msg  = "请上传文件";
                return(Ok(ApiData));
            }
            var bucket = _bucketService.GetBucketBayName(model.bucket);
            if (bucket == null)
            {
                ApiData.code = 2001;
                ApiData.msg  = "bucket错误";
                return(Ok(ApiData));
            }
            #endregion

            IFormFile file = Request.Form.Files[0];

            string sha1 = file.GetSHA1();

            var item = _bucketImageService.GetSHA1(sha1);
            if (item != null)
            {
                ApiData.code = 0;
                ApiData.msg  = "上传成功";
                ApiData.data = new { url = $"/oss/imagecn{item.VisitUrl}" };
                return(Ok(ApiData));
            }

            if (!ValidSignature(model.signature, file.GetMD5(), model.VERB))
            {
                ApiData.code = 1005;
                ApiData.msg  = "签名验证失败";
                return(Ok(ApiData));
            }

            string fileName = Guid.NewGuid().ToString(); //文件名
            string visitUrl = $"/{bucket.Name}/{fileName}";
            Guid   id       = CombGuid.NewGuid();        //entityid
                                                         //存储目录
            string path     = System.IO.Path.Combine(MediaItemConfig.RootDir, bucket.Name, DateTime.Today.ToString("yyyy-MM-dd"), id.ToString());
            bool   compress = false;
            if (bucket.IsCompress)
            {
                if (file.Length > 1024 * 800)
                {
                    compress = true;
                }
            }
            //保存文件并且获取文件的相对存储路径
            var image = file.CreateImagePathFromStream(_mediaItemStorage, path, compress, 70);

            _bucketImageService.AddImage(new Entities.BucketImage()
            {
                Id           = id,
                BucketId     = bucket.Id,
                CreationTime = DateTime.Now,
                FileName     = file.Name,
                IOPath       = image.IOPath,
                SHA1         = sha1,
                VisitUrl     = visitUrl,
                Width        = image.Width,
                Height       = image.Height,
                ExtName      = image.ExtName
            });
            ApiData.code = 0;
            ApiData.msg  = "上传成功";
            ApiData.data = new { url = $"/oss/imagecn{visitUrl}" };
            await Task.FromResult(0);

            return(Ok(ApiData));
        }