Esempio n. 1
0
        public async Task <bool> ExecuteForEntry(Guid entryId, string localFile, PerformContext context)
        {
            _setContext(context);
            Log($"Creating waveform for: {entryId} using {localFile}");

            if (!string.IsNullOrEmpty(localFile) && !File.Exists(localFile))
            {
                LogError($"FileNotFound: {localFile}");
                return(false);
            }
            var entry = await _entryRepository.GetAsync(entryId);

            if (entry != null)
            {
                Log($"Generating waveform for: {entry.Id}");

                var(dat, json, png) = !string.IsNullOrEmpty(localFile) ?
                                      await _waveFormGenerator.GenerateWaveformLocalFile(localFile) :
                                      await _waveFormGenerator.GenerateWaveformRemoteFile(
                    entry.GetRawAudioUrl(_storageSettings.CdnUrl, _audioFileStorageSettings.ContainerName, "mp3")
                    );

                Log($"Dat: {dat}\nJSON: {json}\nPNG: {png}");
                if (!string.IsNullOrEmpty(dat))
                {
                    Log("Uploading .dat");
                    await _fileUploader.UploadFile(
                        dat,
                        _waveformStorageSettings.ContainerName,
                        $"{entry.Id}.dat",
                        "application/x-binary", null);
                }
                if (!string.IsNullOrEmpty(json))
                {
                    Log("Uploading .json");
                    await _fileUploader.UploadFile(
                        json,
                        _waveformStorageSettings.ContainerName,
                        $"{entry.Id}.json",
                        "application/json", null);
                }
                if (!string.IsNullOrEmpty(png))
                {
                    Log("Uploading .png");
                    await _fileUploader.UploadFile(
                        png,
                        _waveformStorageSettings.ContainerName,
                        $"{entry.Id}.png",
                        "image/png", null);
                }
                entry.WaveformGenerated = true;
                Log("Updating context");
                await _unitOfWork.CompleteAsync();

                return(true);
            }
            return(false);
        }
        private async Task _process(Guid entryId, string audioUrl, bool forceReprocess = false)
        {
            var audioExists = !string.IsNullOrEmpty(audioUrl) &&
                              await _fileUtils.CheckFileExists(audioUrl.Split('/')[0], audioUrl.Split('/')[1]);

            if (!audioExists || forceReprocess)
            {
                //TODO: This is all largely a duplicate of ProcessEntryJob, should call into that...
                Log($"_process: Missing audio for: {entryId}");
                var localFile = Path.Combine(Path.GetTempPath(), $"{System.Guid.NewGuid()}.mp3");
                var processed = await _processor.DownloadAudio(entryId, localFile);

                if (processed)
                {
                    Log($"_process: Processed: {entryId}");
                    Log($"_process: Uploading audio for: {entryId}");
                    var uploaded = await _uploadService.UploadAudio(entryId, localFile);

                    if (!uploaded)
                    {
                        LogError($"Error uploading audio from {entryId}");
                    }

                    Log($"_process: Generating waveform for: {entryId}");
                    var(dat, json, png) = await _waveFormGenerator.GenerateWaveformLocalFile(localFile);

                    if (!File.Exists(json))
                    {
                        LogError($"_process: Error json does not exist {json}");
                    }
                    else
                    {
                        Log($"_process: Uploading waveform for: {entryId}");
                        var result = await _fileUploader.UploadFile(
                            json,
                            _waveformStorageSettings.ContainerName,
                            $"{entryId}.json",
                            "application/x-binary",
                            null);

                        Log($"_process: Uploaded waveform for: {entryId}\n\tResult: {result}");
                    }

                    Log($"_process: Completed processing of: {entryId}");
                }
                else
                {
                    LogError($"_process: Unable to process podcast entry: {entryId}");
                }
            }
            else
            {
                Log($"_process: Audio exists, not processing");
            }
        }