Exemplo n.º 1
0
        public async Task <IActionResult> Upload(UploadViewModel model)
        {
            if (ModelState.IsValid)
            {
                var duty = await _dutyManager.GetDutyAsync(model.Id);

                if (duty == null)
                {
                    return(NotFound());
                }

                var user = await GetCurrentUser();

                if (duty.UserId != user.Id && !User.IsSuperUser())
                {
                    return(Unauthorized());
                }

                foreach (var item in model.SharedFiles)
                {
                    var content = new byte[item.Length];
                    var sharing = new DutySharedFile()
                    {
                        DutyId   = duty.Id,
                        MineType = item.ContentType,
                        FileName = item.FileName,
                        Lenght   = item.Length,
                        UserId   = user.Id,
                    };

                    using (var stream = item.OpenReadStream())
                        await stream.ReadAsync(content);

                    sharing.Content = content;

                    await _shareManager.CreateAsync(sharing);
                }

                return(RedirectToAction("Index"));
            }

            return(View(model));
        }