Esempio n. 1
0
 public UserOfferingsController(IAuthorizationService authorizationService, CTDbContext context,
                                UserManager <ApplicationUser> userManager, UserUtils userUtils, ILogger <UserOfferingsController> logger) : base(context, logger)
 {
     _authorizationService = authorizationService;
     _userManager          = userManager;
     _userUtils            = userUtils;
 }
Esempio n. 2
0
        protected async override Task OnConsume(string example, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, "ExampleTask"); // may throw AlreadyInProgress exception
            _logger.LogInformation("Example Task Starting");
            int captionCount       = 0;
            int transcriptionCount = 0;

            using (var _context = CTDbContext.CreateDbContext())
            {
                CaptionQueries captionQueries = new CaptionQueries(_context);

                var transcriptions = await _context.Transcriptions.Take(30).ToListAsync();

                foreach (var transcription in transcriptions)
                {
                    var transcriptionId = transcription.Id;
                    var videoID         = transcription.VideoId;
                    var captions        = await captionQueries.GetCaptionsAsync(transcriptionId);

                    _logger.LogInformation($"{transcription.Id}: Caption count= {captions.Count}");
                    transcriptionCount++;
                }
            }

            _logger.LogInformation($"Example Task Done.  transcriptionCount={transcriptionCount} captionCount={captionCount}");
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var configuration = CTDbContext.GetConfigurations();
            //setup our DI
            var serviceProvider = new ServiceCollection()
                                  .AddLogging(builder =>
            {
                builder.AddConsole();
                builder.AddFilter <Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>
                    ("", LogLevel.Warning);
                builder.AddApplicationInsights(configuration.GetValue <string>("APPLICATION_INSIGHTS_KEY"));
            })
                                  .AddOptions()
                                  .Configure <AppSettings>(configuration)
                                  .AddDbContext <CTDbContext>(options => options.UseLazyLoadingProxies().UseNpgsql(CTDbContext.ConnectionStringBuilder()))
                                  .AddScoped <SlackLogger>()
                                  .AddSingleton <MSTranscriptionService>()
                                  .AddSingleton <TempCode>()
                                  .AddSingleton <RpcClient>()
                                  .BuildServiceProvider();

            Globals.appSettings = serviceProvider.GetService <IOptions <AppSettings> >().Value;

            TempCode tempCode = serviceProvider.GetService <TempCode>();

            tempCode.Temp();
        }
Esempio n. 4
0
        public async Task DeleteVideoAsync(CTDbContext context)
        {
            if (Video1 != null)
            {
                await Video1.DeleteFileRecordAsync(context);
            }
            if (Video2 != null)
            {
                await Video2.DeleteFileRecordAsync(context);
            }
            if (ProcessedVideo1 != null)
            {
                await ProcessedVideo1.DeleteFileRecordAsync(context);
            }
            if (ProcessedVideo2 != null)
            {
                await ProcessedVideo2.DeleteFileRecordAsync(context);
            }
            if (Audio != null)
            {
                await Audio.DeleteFileRecordAsync(context);
            }
            var dbVideoRow = await context.Videos.FindAsync(Id);

            if (dbVideoRow != null)
            {
                context.Videos.Remove(dbVideoRow);
                await context.SaveChangesAsync();
            }
        }
Esempio n. 5
0
 public RolesController(RoleManager<IdentityRole> roleManager, UserManager<ApplicationUser> userManager, 
     CTDbContext context, ILogger<RolesController> logger, UserUtils userUtils) : base(context, logger)
 {
     _roleManager = roleManager;
     _userManager = userManager;
     _userutils = userUtils;
 }
Esempio n. 6
0
        /// <summary>Extracts scene information for a video.
        /// Beware: It is possible to start another scene task while the first one is still running</summary>
        protected async override Task OnConsume(string videoId, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, videoId); // may throw AlreadyInProgress exception
            using (var _context = CTDbContext.CreateDbContext())
            {
                Video video = await _context.Videos.FindAsync(videoId);

                if (video.SceneData == null || taskParameters.Force)
                {
                    var jsonString = await _rpcClient.PythonServerClient.GetScenesRPCAsync(new CTGrpc.File
                    {
                        FilePath = video.Video1.VMPath
                    });

                    JArray scenes = JArray.Parse(jsonString.Json);

                    video.SceneData = new JObject
                    {
                        { "Scenes", scenes }
                    };

                    await _context.SaveChangesAsync();
                }
            }
        }
Esempio n. 7
0
        public async Task <Video> DownloadLocalPlaylist(Media media)
        {
            try
            {
                Video video = new Video();
                if (media.JsonMetadata.ContainsKey("video1Path"))
                {
                    var video1Path = media.JsonMetadata["video1Path"].ToString();
                    video.Video1 = await FileRecord.GetNewFileRecordAsync(video1Path, Path.GetExtension(video1Path));
                }
                if (media.JsonMetadata.ContainsKey("video2Path"))
                {
                    var video2Path = media.JsonMetadata["video2Path"].ToString();
                    video.Video2 = await FileRecord.GetNewFileRecordAsync(video2Path, Path.GetExtension(video2Path));
                }

                return(video);
            }
            catch (Exception e)
            {
                GetLogger().LogError(e, "DownloadLocalPlaylist failed. mediaId {0}", media.Id);
                using (var context = CTDbContext.CreateDbContext())
                {
                    context.Medias.Remove(media);
                    context.SaveChanges();
                }
                return(null);
            }
        }
Esempio n. 8
0
        public async Task <Video> DownloadYoutubeVideo(Media media)
        {
            var mediaResponse = await _rpcClient.PythonServerClient.DownloadYoutubeVideoRPCAsync(new CTGrpc.MediaRequest
            {
                VideoUrl = media.JsonMetadata["videoUrl"].ToString()
            });

            if (FileRecord.IsValidFile(mediaResponse.FilePath))
            {
                Video video = new Video
                {
                    Video1 = await FileRecord.GetNewFileRecordAsync(mediaResponse.FilePath, mediaResponse.Ext)
                };
                return(video);
            }
            else
            {
                // Deleting media is fine if download failed as we can get it back from the youtube playlist.
                GetLogger().LogError("DownloadYoutubeVideo failed. mediaId {0}, removing Media record", media.Id);
                using (var context = CTDbContext.CreateDbContext())
                {
                    context.Medias.Remove(media);
                    context.SaveChanges();
                }

                return(null);
            }
        }
Esempio n. 9
0
        /// <summary>
        ///  Updates the accessToken and refreshToken. These keys must already exist in the Dictionary table.
        /// </summary>
        public async Task RefreshAccessTokenAsync()
        {
            try
            {
                using (var _context = CTDbContext.CreateDbContext())
                {
                    var accessToken = await _context.Dictionaries.Where(d => d.Key == CommonUtils.BOX_ACCESS_TOKEN).FirstAsync();

                    var refreshToken = await _context.Dictionaries.Where(d => d.Key == CommonUtils.BOX_REFRESH_TOKEN).FirstAsync();

                    var config = new BoxConfig(Globals.appSettings.BOX_CLIENT_ID, Globals.appSettings.BOX_CLIENT_SECRET, new Uri("http://locahost"));
                    var auth   = new OAuthSession(accessToken.Value, refreshToken.Value, 3600, "bearer");
                    var client = new BoxClient(config, auth);
                    /// Try to refresh the access token
                    auth = await client.Auth.RefreshAccessTokenAsync(auth.AccessToken);

                    /// Create the client again
                    client = new BoxClient(config, auth);
                    _logger.LogInformation("Refreshed Tokens");
                    accessToken.Value  = auth.AccessToken;
                    refreshToken.Value = auth.RefreshToken;
                    await _context.SaveChangesAsync();
                }
            }
            catch (Box.V2.Exceptions.BoxSessionInvalidatedException e)
            {
                _logger.LogError(e, "Box Token Failure.");
                await _slack.PostErrorAsync(e, "Box Token Failure.");

                throw;
            }
        }
Esempio n. 10
0
        // To generate authCode on a browser open,
        // https://account.box.com/api/oauth2/authorize?client_id=[CLIENT_ID]&response_type=code
        /// <summary>Updates Box accessToken and refreshToken values in the Dictionary table.
        /// Optionally creates these keys if they do not exist.
        /// </summary>
        public async Task CreateAccessTokenAsync(string authCode)
        {
            // This implementation is overly chatty with the database, but we rarely create access tokens so it is not a problem
            using (var _context = CTDbContext.CreateDbContext())
            {
                if (!await _context.Dictionaries.Where(d => d.Key == CommonUtils.BOX_ACCESS_TOKEN).AnyAsync())
                {
                    _context.Dictionaries.Add(new Dictionary
                    {
                        Key = CommonUtils.BOX_ACCESS_TOKEN
                    });
                    await _context.SaveChangesAsync();
                }
                if (!await _context.Dictionaries.Where(d => d.Key == CommonUtils.BOX_REFRESH_TOKEN).AnyAsync())
                {
                    _context.Dictionaries.Add(new Dictionary
                    {
                        Key = CommonUtils.BOX_REFRESH_TOKEN
                    });
                    await _context.SaveChangesAsync();
                }


                var accessToken  = _context.Dictionaries.Where(d => d.Key == CommonUtils.BOX_ACCESS_TOKEN).First();
                var refreshToken = _context.Dictionaries.Where(d => d.Key == CommonUtils.BOX_REFRESH_TOKEN).First();
                var config       = new BoxConfig(Globals.appSettings.BOX_CLIENT_ID, Globals.appSettings.BOX_CLIENT_SECRET, new Uri("http://locahost"));
                var client       = new Box.V2.BoxClient(config);
                var auth         = await client.Auth.AuthenticateAsync(authCode);

                _logger.LogInformation("Created Box Tokens");
                accessToken.Value  = auth.AccessToken;
                refreshToken.Value = auth.RefreshToken;
                await _context.SaveChangesAsync();
            }
        }
Esempio n. 11
0
        protected async override Task OnConsume(string example, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, "BuildElasticIndexTask"); // may throw AlreadyInProgress exception
            GetLogger().LogInformation("BuildElasticIndexTask Starting");

            using (var _context = CTDbContext.CreateDbContext())
            {
                CaptionQueries captionQueries = new CaptionQueries(_context);

                var all_transcriptions = await _context.Transcriptions.Where(t => t.Language == Languages.ENGLISH_AMERICAN).ToListAsync();

                foreach (var transcription in all_transcriptions)
                {
                    var all_captions = transcription.Captions;

                    // each index has the unique name "index_string_unique", the current in use one has the alias "index_string_alias"
                    var index_string_base   = transcription.Id + "_" + Languages.ENGLISH_AMERICAN.ToLower(System.Globalization.CultureInfo.CurrentCulture);
                    var index_string_unique = index_string_base + "_" + $"{DateTime.Now:yyyyMMddHHmmss}";
                    var index_string_alias  = index_string_base + "_" + "primary";

                    var asyncBulkIndexResponse = await _client.BulkAsync(b => b
                                                                         .Index(index_string_unique)
                                                                         .IndexMany(all_captions)
                                                                         );

                    var alias_exist = await _client.Indices.ExistsAsync(index_string_alias);

                    if (alias_exist.Exists)
                    {
                        var oldIndices = await _client.GetIndicesPointingToAliasAsync(index_string_alias);

                        var oldIndexName = oldIndices.First().ToString();

                        var indexResponse = await _client.Indices.BulkAliasAsync(new BulkAliasRequest
                        {
                            Actions = new List <IAliasAction>
                            {
                                new AliasRemoveAction {
                                    Remove = new AliasRemoveOperation {
                                        Index = oldIndexName, Alias = index_string_alias
                                    }
                                },
                                new AliasAddAction {
                                    Add = new AliasAddOperation {
                                        Index = index_string_unique, Alias = index_string_alias
                                    }
                                }
                            }
                        });
                    }
                    else
                    {
                        var putAliasResponse = await _client.Indices.PutAliasAsync(new PutAliasRequest(index_string_unique, index_string_alias));
                    }
                }
            }

            GetLogger().LogInformation("BuildElasticIndexTask Done");
        }
Esempio n. 12
0
 public UserUtils(
     UserManager <ApplicationUser> userManager,
     CTDbContext context
     )
 {
     _userManager = userManager;
     _context     = context;
 }
Esempio n. 13
0
 public CaptionsController(WakeDownloader wakeDownloader,
                           CTDbContext context,
                           CaptionQueries captionQueries,
                           ILogger <CaptionsController> logger) : base(context, logger)
 {
     _captionQueries = captionQueries;
     _wakeDownloader = wakeDownloader;
 }
Esempio n. 14
0
        public async Task <Video> DownloadEchoVideo(Media media)
        {
            Video video = new Video();
            bool  video1Success = false, video2Success = false;

            var mediaResponse = await _rpcClient.PythonServerClient.DownloadEchoVideoRPCAsync(new CTGrpc.MediaRequest
            {
                VideoUrl       = media.JsonMetadata["videoUrl"].ToString(),
                AdditionalInfo = media.Playlist.JsonMetadata["downloadHeader"].ToString()
            });

            video1Success = FileRecord.IsValidFile(mediaResponse.FilePath);
            if (video1Success)
            {
                video.Video1 = await FileRecord.GetNewFileRecordAsync(mediaResponse.FilePath, mediaResponse.Ext);
            }


            if (!string.IsNullOrEmpty(media.JsonMetadata["altVideoUrl"].ToString()))
            {
                var mediaResponse2 = await _rpcClient.PythonServerClient.DownloadEchoVideoRPCAsync(new CTGrpc.MediaRequest
                {
                    VideoUrl       = media.JsonMetadata["altVideoUrl"].ToString(),
                    AdditionalInfo = media.Playlist.JsonMetadata["downloadHeader"].ToString()
                });

                video2Success = FileRecord.IsValidFile(mediaResponse2.FilePath);
                if (video2Success)
                {
                    video.Video2 = await FileRecord.GetNewFileRecordAsync(mediaResponse2.FilePath, mediaResponse.Ext);
                }
            }
            else
            {
                // As there is no file to download, it's "successfull"
                video2Success = true;
            }



            if (video1Success && video2Success)
            {
                return(video);
            }
            else
            {
                // Deleting media is fine if download failed as we can get it back from the echo playlist.
                GetLogger().LogError("DownloadEchoVideo failed. mediaId {0}, removing Media record", media.Id);
                using (var context = CTDbContext.CreateDbContext())
                {
                    context.Medias.Remove(media);
                    context.SaveChanges();
                }

                return(null);
            }
        }
Esempio n. 15
0
 public StaticFileController(IAuthorizationService authorizationService,
                             CTDbContext context,
                             UserUtils userUtils,
                             ILogger <StaticFileController> logger) : base(context, logger)
 {
     _authorizationService = authorizationService;
     _userUtils            = userUtils;
     _provider             = new PhysicalFileProvider(Globals.appSettings.DATA_DIRECTORY);
 }
Esempio n. 16
0
        /// For recently directly/manually uploaded video files,
        /// only the media entry for each uploaded video is created
        /// But we process them here AND in the DownloadMedia
        /// (which ultimately sets the name)
        /// i.e. to better understand this code first also read the DownloadMedia code
        /// When run as part of the periodic update all playlists, this
        /// code should be a NOOP
        public async Task <List <Media> > GetLocalPlaylist(Playlist playlist, CTDbContext _context)
        {
            var medias = await _context.Medias.Where(m => m.Video == null && m.PlaylistId == playlist.Id).ToListAsync();

            medias.ForEach(m => m.Name = GetMediaName(m));
            await _context.SaveChangesAsync();

            return(medias);
        }
Esempio n. 17
0
        protected async override Task OnConsume(string transcriptionId, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, transcriptionId); // may throw AlreadyInProgress exception

            GetLogger().LogInformation($"Creating VTT & SRT files for ({transcriptionId})");

            using (var _context = CTDbContext.CreateDbContext())
            {
                var transcription = await _context.Transcriptions.FindAsync(transcriptionId);

                CaptionQueries captionQueries = new CaptionQueries(_context);
                var            captions       = await captionQueries.GetCaptionsAsync(transcription.Id);

                var vttfile = await FileRecord.GetNewFileRecordAsync(Caption.GenerateWebVTTFile(captions, transcription.Language), ".vtt");

                FileRecord?existingVtt = await _context.FileRecords.FindAsync(transcription.FileId);

                if (existingVtt is null)
                {
                    GetLogger().LogInformation($"{transcriptionId}: Creating new vtt file {vttfile.FileName}");
                    await _context.FileRecords.AddAsync(vttfile);

                    transcription.File = vttfile;
                    _context.Entry(transcription).State = EntityState.Modified;
                }
                else
                {
                    GetLogger().LogInformation($"{transcriptionId}: replacing existing vtt file contents {existingVtt.FileName}");
                    existingVtt.ReplaceWith(vttfile);
                    _context.Entry(existingVtt).State = EntityState.Modified;
                }

                var srtfile = await FileRecord.GetNewFileRecordAsync(Caption.GenerateSrtFile(captions), ".srt");

                FileRecord?existingSrt = await _context.FileRecords.FindAsync(transcription.SrtFileId);

                if (existingSrt is null)
                {
                    GetLogger().LogInformation($"{transcriptionId}: Creating new srt file {srtfile.FileName}");

                    await _context.FileRecords.AddAsync(srtfile);

                    transcription.SrtFile = srtfile;
                    _context.Entry(transcription).State = EntityState.Modified;
                }
                else
                {
                    GetLogger().LogInformation($"{transcriptionId}: replacing existing srt file contents {existingSrt.FileName}");
                    existingSrt.ReplaceWith(srtfile);
                    _context.Entry(existingSrt).State = EntityState.Modified;
                }

                await _context.SaveChangesAsync();

                GetLogger().LogInformation($"{transcriptionId}: Database updated");
            }
        }
Esempio n. 18
0
 public MediaController(IAuthorizationService authorizationService,
                        WakeDownloader wakeDownloader,
                        CTDbContext context,
                        UserUtils userUtils,
                        ILogger <MediaController> logger) : base(context, logger)
 {
     _authorizationService = authorizationService;
     _wakeDownloader       = wakeDownloader;
     _userUtils            = userUtils;
 }
Esempio n. 19
0
 public AccountController(
     UserManager <ApplicationUser> userManager,
     SignInManager <ApplicationUser> signInManager,
     CTDbContext context, UserUtils userUtils,
     ILogger <AccountController> logger) : base(context, logger)
 {
     _userManager   = userManager;
     _signInManager = signInManager;
     _userUtils     = userUtils;
 }
Esempio n. 20
0
        public async Task <List <Media> > GetYoutubePlaylist(Playlist playlist, CTDbContext _context)
        {
            List <Media> newMedia = new List <Media>();

            CTGrpc.JsonString jsonString = null;
            CTGrpc.JsonString metadata   = new CTGrpc.JsonString
            {
                Json = playlist.JsonMetadata != null?playlist.JsonMetadata.ToString() : ""
            };
            try
            {
                jsonString = await _rpcClient.PythonServerClient.GetYoutubePlaylistRPCAsync(new CTGrpc.PlaylistRequest
                {
                    Url      = playlist.PlaylistIdentifier,
                    Metadata = metadata
                });
            }
            catch (RpcException e)
            {
                if (e.Status.StatusCode == StatusCode.InvalidArgument)
                {
                    if (e.Status.Detail == "INVALID_PLAYLIST_IDENTIFIER")
                    {
                        // Notification to Instructor.
                    }
                    GetLogger().LogError(e.Message);
                }
                return(newMedia);
            }
            JArray jArray = JArray.Parse(jsonString.Json);

            foreach (JObject jObject in jArray)
            {
                // Check if there is a valid videoId, and for the same playlist the same media does not exist.
                if (jObject["videoId"].ToString().Length > 0 &&
                    !await _context.Medias.Where(m => m.UniqueMediaIdentifier == jObject["videoId"].ToString() &&
                                                 m.SourceType == playlist.SourceType &&
                                                 m.PlaylistId == playlist.Id).AnyAsync())
                {
                    newMedia.Add(new Media
                    {
                        JsonMetadata          = jObject,
                        SourceType            = playlist.SourceType,
                        PlaylistId            = playlist.Id,
                        UniqueMediaIdentifier = jObject["videoId"].ToString()
                    });
                }
            }
            newMedia.ForEach(m => m.Name = GetMediaName(m));
            await _context.Medias.AddRangeAsync(newMedia);

            await _context.SaveChangesAsync();

            return(newMedia);
        }
Esempio n. 21
0
        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            var v = WebHost.CreateDefaultBuilder(args)
                    .ConfigureServices(c => c.AddOptions().Configure <AppSettings>(CTDbContext.GetConfigurations()));

            // TODO: This filter could be a environment variable setting
            // However we are still building the configuration at this point (is AppSettings configured here?)
            v.ConfigureLogging((context, logging) => {
                logging.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Warning);
            });
            return(v.UseStartup <Startup>());
        }
Esempio n. 22
0
        // This constructor is run before every test, ensuring a new context and in-memory DB for each test case
        // https://xunit.net/docs/shared-context
        public BaseControllerTest(GlobalFixture fixture)
        {
            var optionsBuilder = new DbContextOptionsBuilder <CTDbContext>()
                                 .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                 .UseInternalServiceProvider(fixture._serviceProvider);

            _context   = new CTDbContext(optionsBuilder.Options, null);
            _userUtils = new UserUtils(
                (MockUserManager)fixture._serviceProvider.GetService(typeof(MockUserManager)),
                _context
                );
        }
        public async Task <List <Media> > GetKalturaPlaylist(Playlist playlist, CTDbContext _context)
        {
            List <Media> newMedia = new List <Media>();

            CTGrpc.JsonString jsonString = null;
            try
            {
                jsonString = await _rpcClient.PythonServerClient.GetKalturaChannelEntriesRPCAsync(new CTGrpc.PlaylistRequest
                {
                    Url = playlist.PlaylistIdentifier
                });
            }
            catch (RpcException e)
            {
                if (e.Status.StatusCode == StatusCode.InvalidArgument)
                {
                    if (e.Status.Detail == "INVALID_PLAYLIST_IDENTIFIER")
                    {
                        // Notification to Instructor.
                    }
                    _logger.LogError(e.Message);
                }
                return(newMedia);
            }
            JArray jArray = JArray.Parse(jsonString.Json);

            foreach (JObject jObject in jArray)
            {
                // Check if there is a valid Id, and for the same playlist the same media does not exist.
                if (jObject["id"].ToString().Length > 0 &&
                    !await _context.Medias.Where(m => m.UniqueMediaIdentifier == jObject["id"].ToString() &&
                                                 m.SourceType == playlist.SourceType &&
                                                 m.PlaylistId == playlist.Id).AnyAsync())
                {
                    newMedia.Add(new Media
                    {
                        JsonMetadata          = jObject,
                        SourceType            = playlist.SourceType,
                        PlaylistId            = playlist.Id,
                        UniqueMediaIdentifier = jObject["id"].ToString(),
                        CreatedAt             = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
                                                .AddSeconds(jObject["createdAt"].ToObject <int>())
                    });
                }
            }
            newMedia.ForEach(m => m.Name = GetMediaName(m));
            await _context.Medias.AddRangeAsync(newMedia);

            await _context.SaveChangesAsync();

            return(newMedia);
        }
Esempio n. 24
0
        private async Task <List <Media> > GetBoxPlaylist(Playlist playlist, CTDbContext _context)
        {
            try
            {
                var client = await _box.GetBoxClientAsync();

                /// Try to refresh the access token
                var folderInfo = await client.FoldersManager.GetInformationAsync(playlist.PlaylistIdentifier);

                playlist.JsonMetadata = JObject.FromObject(folderInfo);

                var items = (await client.FoldersManager.GetFolderItemsAsync(playlist.PlaylistIdentifier, 500)).Entries.OfType <BoxFile>();
                // Process only files with an mp4 extension.
                items = items.Where(i => i.Name.Substring(i.Name.LastIndexOf(".") + 1) == "mp4").ToList();
                List <Media> newMedia = new List <Media>();

                foreach (var item in items)
                {
                    var file = await client.FilesManager.GetInformationAsync(item.Id);

                    // Check if there is a valid file.Id, and for the same playlist the same media does not exist.
                    if (file.Id.Length > 0 &&
                        !await _context.Medias.Where(m => m.UniqueMediaIdentifier == file.Id &&
                                                     m.SourceType == playlist.SourceType &&
                                                     m.PlaylistId == playlist.Id).AnyAsync())
                    {
                        newMedia.Add(new Media
                        {
                            SourceType            = playlist.SourceType,
                            PlaylistId            = playlist.Id,
                            UniqueMediaIdentifier = file.Id,
                            JsonMetadata          = JObject.FromObject(file),
                            CreatedAt             = file.CreatedAt ?? DateTime.Now
                        });
                    }
                }
                newMedia.ForEach(m => m.Name = GetMediaName(m));
                await _context.Medias.AddRangeAsync(newMedia);

                await _context.SaveChangesAsync();

                return(newMedia);
            }
            catch (Box.V2.Exceptions.BoxSessionInvalidatedException e)
            {
                GetLogger().LogError(e, "Box Token Failure.");
                await _slack.PostErrorAsync(e, "Box Token Failure.");

                throw;
            }
        }
Esempio n. 25
0
        // The following is impossible because rpcClient is in the CTCommons project
        // which depends on this project, not the other way around

        //bool useRPCForFileDigest = false;
        //if(useRPCForFileDigest)
        //{
        //    var request = new CTGrpc.FileHashRequest
        //    {
        //        File = filePath,
        //        Algorithms = "sha256"
        //    };
        //    CTGrpc.FileHashResponse rpcresponse = await _rpcClient.PythonServerClient(request);
        //    return rpcresponse.Result;
        // }
        // ComputeHashAsync is not yet available (only in 5.0 RC1)
        //
        // The concern is that ComputeHash is hogging the thread for too long
        // So async tasks (e.g. SpeechToText might timeout)



        /// <summary>
        /// Delete a file record, and its corresponding file.
        /// </summary>
        public async Task DeleteFileRecordAsync(CTDbContext context)
        {
            if (File.Exists(Path))
            {
                File.Delete(Path);
            }
            var dbFileRecord = await context.FileRecords.FindAsync(Id);

            if (dbFileRecord != null)
            {
                context.FileRecords.Remove(dbFileRecord);
                await context.SaveChangesAsync();
            }
        }
Esempio n. 26
0
        public BuildElasticIndexTask(RabbitMQConnection rabbitMQ,
                                     ILogger <BuildElasticIndexTask> logger)
            : base(rabbitMQ, TaskType.BuildElasticIndex, logger)
        {
            var configuration = CTDbContext.GetConfigurations();

            // initialize elastic client
            var node = new Uri(configuration.GetValue <string>("ES_CONNECTION_ADDR"));

            using (var settings = new ConnectionSettings(node))
            {
                //settings.DefaultIndex("classTranscribe");
                _client = new ElasticClient(settings);
            }
        }
Esempio n. 27
0
        protected async override Task OnConsume(string transcriptionId, TaskParameters taskParameters, ClientActiveTasks cleanup)
        {
            registerTask(cleanup, transcriptionId); // may throw AlreadyInProgress exception
            using (var _context = CTDbContext.CreateDbContext())
            {
                var transcription = await _context.Transcriptions.FindAsync(transcriptionId);

                FileRecord existingVtt = await _context.FileRecords.FindAsync(transcription.FileId);

                FileRecord existingSrt = await _context.FileRecords.FindAsync(transcription.SrtFileId);


                CaptionQueries captionQueries = new CaptionQueries(_context);
                var            captions       = await captionQueries.GetCaptionsAsync(transcription.Id);

                var vttfile = await FileRecord.GetNewFileRecordAsync(Caption.GenerateWebVTTFile(captions, transcription.Language), ".vtt");

                if (string.IsNullOrEmpty(transcription.FileId))
                {
                    await _context.FileRecords.AddAsync(vttfile);

                    transcription.File = vttfile;
                    _context.Entry(transcription).State = EntityState.Modified;
                }
                else
                {
                    existingVtt.ReplaceWith(vttfile);
                    _context.Entry(existingVtt).State = EntityState.Modified;
                }

                var srtfile = await FileRecord.GetNewFileRecordAsync(Caption.GenerateSrtFile(captions), ".srt");

                if (string.IsNullOrEmpty(transcription.SrtFileId))
                {
                    await _context.FileRecords.AddAsync(srtfile);

                    transcription.SrtFile = srtfile;
                    _context.Entry(transcription).State = EntityState.Modified;
                }
                else
                {
                    existingSrt.ReplaceWith(srtfile);
                    _context.Entry(existingSrt).State = EntityState.Modified;
                }

                await _context.SaveChangesAsync();
            }
        }
        // This constructor is run before every test, ensuring a new context and in-memory DB for each test case
        // https://xunit.net/docs/shared-context
        public BaseControllerTest()
        {
            // Create a fresh service provider, and therefore a fresh InMemory database instance
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkInMemoryDatabase()
                                  .BuildServiceProvider();

            // Set up CTDbContext with an in-memory database
            var optionsBuilder = new DbContextOptionsBuilder <CTDbContext>()
                                 .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                 .UseInternalServiceProvider(serviceProvider);

            _context = new CTDbContext(optionsBuilder.Options, null);
            _context.Database.EnsureDeleted();
            _context.Database.EnsureCreated();
        }
        /// <summary>
        /// Original implementation of OnConsume. This code may be deleted if it is no longer useful. It is left as available for now as a template
        /// </summary>
        /// <param name="videoId"></param>
        /// <param name="taskParameters"></param>
        /// <returns></returns>
        private async Task OldOnConsumeNotUsed(string videoId, TaskParameters taskParameters)
        {
            using (var _context = CTDbContext.CreateDbContext())
            {
                // Get the video object
                var video = await _context.Videos.FindAsync(videoId);

                _logger.LogInformation("Consuming" + video);
                // Make RPC call to produce audio file.
                var file = await _rpcClient.PythonServerClient.ConvertVideoToWavRPCWithOffsetAsync(new CTGrpc.FileForConversion
                {
                    File = new CTGrpc.File {
                        FilePath = video.Video1.VMPath
                    }
                });


                // Check if a valid file was returned.
                if (FileRecord.IsValidFile(file.FilePath))
                {
                    var fileRecord = await FileRecord.GetNewFileRecordAsync(file.FilePath, file.Ext);

                    // Get the latest video object, in case it has changed
                    var videoLatest = await _context.Videos.FindAsync(video.Id);

                    // If there is no Audio file present, then update.
                    if (videoLatest.Audio == null)
                    {
                        await _context.FileRecords.AddAsync(fileRecord);

                        videoLatest.Audio = fileRecord;
                        await _context.SaveChangesAsync();


                        // If no transcriptions present, produce transcriptions.
                        if (!videoLatest.Transcriptions.Any())
                        {
                            _transcriptionTask.Publish(videoLatest.Id);
                        }
                    }
                }
                else
                {
                    throw new Exception("ConvertVideoToWavTask Failed + " + video.Id);
                }
            }
        }
Esempio n. 30
0
        private async void buildMockCaptions(string videoId)
        {
            GetLogger().LogInformation($"Building Mock Captions for video {videoId}");

            using (var _context = CTDbContext.CreateDbContext())
            {
                Video video = await _context.Videos.Include(v => v.Transcriptions).SingleAsync(v => v.Id == videoId);

                string[] languages = new string[] { Languages.ENGLISH_AMERICAN, Languages.SPANISH };
                foreach (var language in languages)
                {
                    var transcription = video.Transcriptions.SingleOrDefault(t => t.Language == language);
                    // Did we get the default or an existing Transcription entity?
                    if (transcription == null)
                    {
                        transcription = new Transcription()
                        {
                            Language = language, VideoId = video.Id
                        };
                        _context.Add(transcription);
                    }
                    ;

                    TimeSpan time = new TimeSpan();

                    TimeSpan duration = new TimeSpan(0, 0, 3); // seconds

                    for (int index = 1; index <= 3; index++)
                    {
                        TimeSpan end = time.Add(duration);

                        Caption c = new Caption
                        {
                            Index           = index,
                            Text            = $"The Caption in {language} is {index + 100} on {DateTime.Now}",
                            Begin           = time,
                            End             = end,
                            TranscriptionId = transcription.Id
                        };
                        _context.Add(c);
                        time = end;
                    } // for
                }     // for language
                await _context.SaveChangesAsync();
            }
        }