Beispiel #1
0
        public async Task <IActionResult> Pic(int id, IFormFile file)
        {
            var user = await uow.User.Get(id);

            if (user == null)
            {
                return(NotFound(new { respose = "error", message = $"user id = {id} does not exist" }));
            }

            using (var memStream = new MemoryStream())
            {
                await file.CopyToAsync(memStream);

                var image = memStream.ToArray();

                var mimeType = imageProcessor.GetImageMimeType(image);
                if (!imageProcessor.SupportedMimeTypes.Contains(mimeType))
                {
                    return(BadRequest(new { Result = "error", Message = "unsupported format. the only supported format is JPEG" }));
                }

                var blobName = await blobStorage.SaveAsync(image);

                var media = await uow.Media.Add(new UserMedia
                {
                    BlobPath  = blobName,
                    Extension = imageProcessor.GetImageExtension(image),
                    Uploaded  = DateTime.UtcNow,
                    Rel       = MediaRelationType.UserPic,
                    UserId    = id
                });

                await uow.CommitAsync();

                return(Ok(new { PicUrl = Url.ActionUserPic(blobName) }));
            }
        }