예제 #1
0
        protected override async Task Process()
        {
            var file = await GetFileAsync();

            var fileOutput = await API.File.PutAsync(new FileModel
            {
                Identifier = new FileIdentifier(file.Identifier as FolderIdentifier, Guid.NewGuid().ToString()),
                MimeType   = "text/plain",
                Name       = "Voicebase output"
            }
                                                     .InitializeEmptyMetadata()
                                                     .Write(MetadataKeyConstants.HIDDEN, true)
                                                     .Write(MetadataKeyConstants.CHILDOF, file.Identifier));

            var callbackUrl = await API.Queue.CreateCallbackAsync(new CallbackModel
            {
                FileIdentifier = fileOutput.Identifier,
                Queue          = "Voicebase.Callback",
                Token          = API.Token
            });

            var alternativeViews = file.Read <List <AlternativeView> >(MetadataKeyConstants.ALTERNATIVE_VIEWS);
            var mp3 = alternativeViews?.FirstOrDefault(a => a.MimeType == "audio/mp3");

            var targetFileIdentifier = mp3 == null
                ? file.Identifier
                : mp3.FileIdentifier;

            using (var voicebase = new VoiceBaseClient(new Uri(Configuration.VoicebaseURL), Configuration.VoicebaseToken))
                await API.File.DownloadAsync(targetFileIdentifier, async (stream, cancel) =>
                {
                    var mediaID = await voicebase.UploadMedia(stream, callbackUrl);

                    await API.ConcurrencyRetryBlock(async() =>
                    {
                        file = await GetFileAsync();
                        file.Write("attributes.voicebase.status", "processing");
                        file.Write("attributes.voicebase.mediaid", mediaID);
                        file.Write("attributes.requestedBy", CurrentMessage.RequestedBy);
                        await API.File.PutAsync(file);
                    });
                });
        }
예제 #2
0
        protected override async Task Process()
        {
            // get the callback response file
            var file = await API.File.GetAsync(CurrentMessage.FileIdentifier);

            var originalFileIdentifier = file.Read <FileIdentifier>(MetadataKeyConstants.CHILDOF);
            var originalFile           = await API.File.GetAsync(originalFileIdentifier);

            var maxDepth = 10;
            var parent   = originalFile.Read <FileIdentifier>(MetadataKeyConstants.CHILDOF);

            while (parent != null && maxDepth-- > 0)
            {
                originalFileIdentifier = parent;
                originalFile           = await API.File.GetAsync(parent);

                parent = originalFile.Read <FileIdentifier>(MetadataKeyConstants.CHILDOF);
            }

            // download the callback response file and deserialize it
            var obj = await API.File.DownloadAsAsync <MediaResponse>(CurrentMessage.FileIdentifier);

            var message = $"Transcription of file {originalFile.Name} completed";

            await API.Log.PostAsync(new AuditLogEntryModel
            {
                Identifier     = new AuditLogEntryIdentifier(originalFileIdentifier),
                FileIdentifier = originalFileIdentifier,
                ActionType     = "Transcription",
                Description    = message,
                Details        = JsonConvert.SerializeObject(new
                {
                    obj.MediaID,
                    obj.Status,
                    obj.Length
                }),
                InitiatorUserIdentifier = originalFile.Read <UserIdentifier>("attribute.requestedBy") ?? API.UserIdentifier,
                Generated = DateTime.UtcNow,
                UserAgent = API.UserAgent
            });

            // grab the VTT formatted transcript
            var vttContents = Convert.FromBase64String(obj.Transcript.AlternateFormats.First(a => a.Format == "webvtt").Data);
            // upload the vtt transcript
            var vttFile = new FileModel
            {
                Identifier = new FileIdentifier(
                    originalFileIdentifier as FolderIdentifier,
                    Guid.NewGuid().ToString()
                    ),
                Name     = Path.GetFileNameWithoutExtension(originalFile.Name) + ".vtt",
                MimeType = "text/vtt"
            };

            vttFile
            .InitializeEmptyMetadata()
            .Write(MetadataKeyConstants.CHILDOF, originalFileIdentifier)
            .Write(MetadataKeyConstants.HIDDEN, true);

            vttFile = await API.File.UploadAsync(vttFile, Encoding.UTF8.GetString(vttContents));

            await API.ConcurrencyRetryBlock(async() =>
            {
                // tag the original
                originalFile = await API.File.GetAsync(originalFileIdentifier);

                var views = originalFile.Read(MetadataKeyConstants.ALTERNATIVE_VIEWS, defaultValue: new List <AlternativeView>());
                views.Add(new AlternativeView
                {
                    FileIdentifier = vttFile.Identifier,
                    MimeType       = "text/vtt",
                    Name           = "Voicebase WebVTT",
                });
                originalFile.Write(MetadataKeyConstants.ALTERNATIVE_VIEWS, views);
                originalFile.Write("attributes.voicebase.status", "complete");

                await API.File.PutAsync(originalFile);
            });

            using (var voicebase = new VoiceBaseClient(new Uri(Configuration.VoicebaseURL), Configuration.VoicebaseToken))
                await voicebase.DeleteMediaAsync(obj.MediaID);
        }