public bool savePicture(ImagesEntity model)
        {
            var db = new HmsContext();

            db.Images.Add(model);
            return(db.SaveChanges() > 0);
        }
Exemplo n.º 2
0
        public JsonResult Images()
        {
            JsonResult json = new JsonResult();

            var files     = Request.Files;
            var ImageList = new List <ImagesEntity>();

            for (int i = 0; i < files.Count; i++)
            {
                var image = files[i];

                var path     = Server.MapPath("~/Content/images/Site/");
                var fileName = Guid.NewGuid() + Path.GetExtension(image.FileName);
                var filePath = path + fileName;
                image.SaveAs(filePath);
                var dbImages = new ImagesEntity();
                dbImages.Url = fileName;
                if (DashboardServices.Instance.savePicture(dbImages))
                {
                    ImageList.Add(dbImages);
                }
            }
            json.Data = ImageList;
            return(json);
        }
Exemplo n.º 3
0
        public async Task <IActionResult> PostImage(Guid restaurantId, CancellationToken cancellationToken)
        {
            _logger.LogInformation("[RestaurantId: {restaurantId}] Storing a new image", restaurantId);
            Agent.Tracer.ActiveSpan?.SetTag("CUSTOM_KEY", "CUSTOM_VALUE");

            string contentType;
            Stream fileStream;

            if (Request.ContentType.StartsWith("image/"))
            {
                contentType = Request.ContentType;
                fileStream  = Request.Body;
            }
            else
            {
                _logger.LogCritical("No images were found in the request. Invalid ContentType = " + Request.ContentType);
                return(BadRequest("No images were found in the request. Invalid ContentType = " + Request.ContentType));
            }

            _logger.LogInformation("[RestaurantId: {restaurantId}] ContentType: {contentType}", restaurantId, contentType);

            await using var imagesCtx = new ImagesContext();

            var bytesData = await Utils.GetBytesFromStreamAsync(fileStream, cancellationToken).ConfigureAwait(false);

            if (bytesData.Length == 0)
            {
                throw new Exception("Image data length must be greater than 0");
            }

            var imgData = new ImagesEntity
            {
                RestaurantId = restaurantId,
                ContentType  = contentType,
                ContentData  = bytesData
            };

            imagesCtx.ImagesData.Add(imgData);

            _logger.LogInformation("Writing image data to database...");
            await imagesCtx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

            _logger.LogInformation("[RestaurantId: {restaurantId}] Images saved with Id: {Id}", restaurantId, imgData.Id);
            return(Ok(imgData.Id));
        }