Beispiel #1
0
        public IActionResult StoryAdd([FromForm] List <IFormFile> model)
        {
            try
            {
                var currentUserId = User.FindFirst(ClaimTypes.NameIdentifier).Value;

                if (model.Any(f => f.Length == 0))
                {
                    return(StatusCode(301, "Resim veya Video Yok"));
                }

                var stories = new List <Story>();

                foreach (var file in model)
                {
                    var extention = Path.GetExtension(file.FileName);
                    if (extention == ".jpg" || extention == ".png")
                    {
                        var uploadResult = new ImageUploadResult();

                        using (var stream = file.OpenReadStream())
                        {
                            var uploadParams = new ImageUploadParams
                            {
                                File = new FileDescription(file.Name, stream),
                            };
                            uploadResult = _cloudinary.Upload(uploadParams);
                        }


                        stories.Add(new Story()
                        {
                            Id          = Guid.NewGuid(),
                            StoryUrl    = uploadResult.Uri.ToString(),
                            UserId      = Guid.Parse(currentUserId),
                            PublicId    = uploadResult.PublicId,
                            CreatedDate = DateTime.Now,
                            IsActive    = true
                        });
                    }

                    if (extention == ".mp4")
                    {
                        var uploadvideoResult = new VideoUploadResult();
                        using (var stream = file.OpenReadStream())
                        {
                            var uploadParams = new VideoUploadParams()
                            {
                                File = new FileDescription(file.Name, stream)
                            };
                            uploadvideoResult = _cloudinary.Upload(uploadParams);
                        }

                        stories.Add(new Story()
                        {
                            Id          = Guid.NewGuid(),
                            StoryUrl    = uploadvideoResult.Uri.ToString(),
                            UserId      = Guid.Parse(currentUserId),
                            PublicId    = uploadvideoResult.PublicId,
                            CreatedDate = DateTime.Now,
                            IsActive    = true
                        });
                    }
                }

                _storyService.AddRange(stories);

                return(StatusCode(200, "Başarılı"));
            }
            catch (Exception e)
            {
                return(StatusCode(302, "Hata oluştu"));
            }
        }