Пример #1
0
        public async Task <ActionResult <object> > PostImg()
        {
            string root = "pcx/";

            if (!Directory.Exists(root))
            {
                Directory.CreateDirectory(root);
            }
            PCXcontainer containerTemp = new PCXcontainer();

            containerTemp.IPaddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();
            ImgUpload temp = new ImgUpload();

            Request.Headers["filename"] = Regex.Replace(Request.Headers["filename"], "[^a-zA-Z0-9% ._]", string.Empty);
            containerTemp.filename      = Request.Headers["filename"] + ".pcx";
            containerTemp.originalImg   = root + DateTime.Now.ToString("dd-MM-yyyy-hh_mm-ss-ffff") + "_" + Request.Headers["filename"] + ".pcx";
            temp.FileName = containerTemp.filename;
            using (var fs = new FileStream("wwwroot/" + containerTemp.originalImg, FileMode.Create))
            {
                Request.Body.CopyTo(fs);
            }

            try
            {
                var      extension       = ImageExtensions.GetImageFormat(Request.Headers["extension"]);
                var      stringExtension = Request.Headers["extension"];
                PCXImage image           = new PCXImage("wwwroot/" + containerTemp.originalImg);
                containerTemp.version = image.FileVersion;
                Bitmap convertedImage = new Bitmap(image.Image);
                image.Dispose();

                using (var bitmap = new Bitmap(convertedImage))
                {
                    string filename = root + DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss-ffff") + "_" + Request.Headers["filename"] + stringExtension;
                    bitmap.Save("wwwroot/" + filename, extension);
                    containerTemp.convertedImg = filename;
                }
                containerTemp.dateTime       = DateTime.Now;
                containerTemp.downloadFormat = stringExtension;
                containerTemp.height         = convertedImage.Height;
                containerTemp.width          = convertedImage.Width;
                convertedImage.Dispose();
                _context.PCXcontainers.RemoveRange(_context.PCXcontainers.OrderBy(x => x.dateTime).Take(_context.PCXcontainers.Count() - 4));
                _context.PCXcontainers.Add(containerTemp);
                await _context.SaveChangesAsync();

                temp.id = _context.PCXcontainers.Last().id;
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
                return(NotFound());
            }
            return(temp);
        }