示例#1
0
        public async Task <EntryProcessResult> PreProcessEntry(ApplicationUser user, PodcastEntry entry)
        {
            var quota     = user.DiskQuota ?? _storageSettings.DefaultUserQuota;
            var totalUsed = (await _repository.GetAllForUserAsync(user.Id))
                            .Select(x => x.AudioFileSize)
                            .Sum();

            if (totalUsed >= quota)
            {
                return(EntryProcessResult.QuotaExceeded);
            }

            if (string.IsNullOrEmpty(entry.ImageUrl))
            {
                entry.ImageUrl = $"{_storageSettings.CdnUrl}/static/images/default-entry.png";
            }

            entry.Processed = false;
            _repository.AddOrUpdate(entry);
            try {
                var succeeded = await _unitOfWork.CompleteAsync();

                if (succeeded)
                {
                    BackgroundJob.Enqueue <ProcessNewEntryJob>(e => e.ProcessEntry(entry.Id, null));
                    return(EntryProcessResult.Succeeded);
                }
            } catch (DbUpdateException e) {
                _logger.LogError(e.Message);
            }
            return(EntryProcessResult.GeneralFailure);
        }
        public async Task <ActionResult <PodcastEntryViewModel> > UploadEntryImage(string id, IFormFile image)
        {
            _logger.LogDebug("Uploading new entry image");

            var entry = await _entryRepository.GetAsync(_applicationUser.Id, id);

            if (entry is null)
            {
                return(NotFound());
            }
            try {
                var imageFile = await _commitImage(id, image, "entry");

                entry.ImageUrl = $"{_imageFileStorageSettings.ContainerName}/{imageFile}";
                _entryRepository.AddOrUpdate(entry);
                await _unitOfWork.CompleteAsync();

                return(Ok(_mapper.Map <PodcastEntry, PodcastEntryViewModel>(entry)));
            } catch (InvalidOperationException ex) {
                return(BadRequest(ex.Message));
            }
        }
        public async Task <IActionResult> Upload(string slug, IFormFile file)
        {
            _logger.LogDebug($"Uploading file for: {slug}");
            if (file is null || file.Length == 0)
            {
                return(BadRequest("No file found in stream"));
            }
            if (file.Length > _audioFileStorageSettings.MaxUploadFileSize)
            {
                return(BadRequest("Maximum file size exceeded"));
            }
            if (!_audioFileStorageSettings.IsSupported(file.FileName))
            {
                return(BadRequest("Invalid file type"));
            }

            var podcast = await _podcastRepository.GetForUserAndSlugAsync(_applicationUser.Slug, slug);

            if (podcast is null)
            {
                _logger.LogError($"Unable to find podcast");
                return(NotFound());
            }

            var entry = new PodcastEntry {
                Title            = Path.GetFileName(Path.GetFileNameWithoutExtension(file.FileName)),
                ImageUrl         = $"{_storageSettings.CdnUrl}/static/images/default-entry.png",
                Processed        = false,
                ProcessingStatus = ProcessingStatus.Processing,
                Podcast          = podcast
            };

            var localFile = await CachedFormFileStorage.CacheItem(_hostingEnvironment.WebRootPath, file);

            _logger.LogDebug($"Local file is: {localFile}");

            _entryRepository.AddOrUpdate(entry);

            _logger.LogDebug("Completing uow");
            await _unitOfWork.CompleteAsync();

            var authToken = _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"].ToString();

            if (string.IsNullOrEmpty(authToken))
            {
                return(Unauthorized("Auth token is empty"));
            }

            //convert uploaded file to extension
            var audioUrl = localFile
                           .Replace(_hostingEnvironment.WebRootPath, string.Empty)
                           .Replace(@"\", "/");

            _logger.LogDebug($"Starting processing jobs for url: {audioUrl}");

            BackgroundJob.Enqueue <ProcessNewEntryJob>(e =>
                                                       e.ProcessEntryFromUploadFile(entry.Id, audioUrl, authToken, null));

            var ret = _mapper.Map <PodcastEntry, PodcastEntryViewModel>(entry);

            return(Ok(ret));
        }