Beispiel #1
0
        public async Task <IActionResult> PostPhoto(int productId, IList <IFormFile> photos)
        {
            // todo validate file
            // todo create list and use addRange
            foreach (var photo in photos)
            {
                var uploadResult = await _photoStorage.Save(photo);

                var photoEntry = new Photo()
                {
                    ProductId   = productId,
                    OriginalUrl = uploadResult.Uri.ToString()
                };
                _context.Photos.Add(photoEntry);


                await _context.SaveChangesAsync();

                await _bus.Publish(new PhotoAddedEvent()
                {
                    OriginalUrl = photoEntry.OriginalUrl,
                    PhotoId     = photoEntry.PhotoId,
                    ProductId   = photoEntry.ProductId
                });
            }

            // todo send events to resizer

            return(Accepted());
        }
        public async Task <IHttpActionResult> UploadFile()
        {
            var    ctx                 = HttpContext.Current;
            string description         = ctx.Request.Headers["Description"].ToString();
            string fileNameFromRequest = ctx.Request.Headers["Name"].ToString();
            string userName            = ctx.Request.Headers["UserName"].ToString();

            var root     = ctx.Server.MapPath("~/Images");
            var provider =
                new MultipartFormDataStreamProvider(root);
            Photo photo = new Photo()
            {
                Path = "", Description = description, UserName = userName, FileName = fileNameFromRequest
            };

            try
            {
                await Request.Content
                .ReadAsMultipartAsync(provider);

                foreach (var file in provider.FileData)
                {
                    if (file.Headers.ContentDisposition.FileName.Contains(".jpg"))
                    {
                        file.Headers.ContentDisposition.FileName = fileNameFromRequest + ".jpg";
                    }
                    else
                    {
                        file.Headers.ContentDisposition.FileName = fileNameFromRequest + ".png";
                    }
                    var name = file.Headers
                               .ContentDisposition
                               .FileName;

                    // remove double quotes from string.
                    name = name.Trim('"');

                    var localFileName = file.LocalFileName;
                    var filePath      = Path.Combine(root, name);
                    photo.Path = "http://localhost:1801/Images/" + name;
                    File.Move(localFileName, filePath);
                }
            }
            catch (Exception e)
            {
                return(Ok(new { status = false }));
            }

            db.Photos.Add(photo);
            await db.SaveChangesAsync();

            return(Ok(new { status = true, answerData = photo }));
        }
        protected static async Task <TResult> PerformDatabaseFunction <TResult>(Func <PhotosContext, Task <TResult> > databaseFunction)
        {
            using var connection = new PhotosContext();

            try
            {
                var result = await databaseFunction(connection).ConfigureAwait(false);

                await connection.SaveChangesAsync().ConfigureAwait(false);

                return(result);
            }
            catch (Exception e)
            {
                Debug.WriteLine("");
                Debug.WriteLine(e.Message);
                Debug.WriteLine(e.ToString());
                Debug.WriteLine("");

                throw;
            }
        }