Пример #1
0
        public async Task <IActionResult> DeleteAttachmentAsync(int id)
        {
            var attachment = _rewardService.GetAttachment(id);

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

            var reward     = _rewardService.GetReward(attachment.RewardId);
            var authResult = await _authorizationService.AuthorizeAsync(User, reward, Policy.CanEditReward);

            if (!authResult.Succeeded)
            {
                return(Forbid());
            }

            reward.RewardAttachments.RemoveAll(a => a.Id == id);
            _rewardService.SaveChanges();
            _logger.LogInformation("{user} removed file {file} from reward {reward}",
                                   User.Identity.Name, attachment.FileId, reward.Id);

            if (!_rewardService.IsAttachedToReward(attachment.FileId))
            {
                _fileService.DeleteFile(attachment.FileId);
                _logger.LogInformation("{user} deleted file {file}", User.Identity.Name, attachment.FileId);
            }

            return(RedirectToAction("Attachments", new { id = reward.Id }));
        }
Пример #2
0
        public IActionResult Download(string type, int id, bool inline = false)
        {
            int?fileId;

            switch (type.ToLower())
            {
            case "event":
                var eventAttachment = _eventService.GetAttachment(id);
                fileId = eventAttachment?.FileId;
                break;

            case "reward":
                var rewardAttachment = _rewardService.GetAttachment(id);
                fileId = rewardAttachment?.FileId;
                break;

            default:
                _logger.LogWarning("Unsupported attachment type {type}", type);
                return(BadRequest());
            }

            return(fileId != null?DownloadFile((int)fileId, inline) : NotFound());
        }