/// <summary> /// Creates media objects /// </summary> /// <param name="contentModels"></param> public void CreateMedia(List<ContentModel> contentModels) { using (var db = new MediaManagerDataContext()) { var movies = contentModels.Where(a => a.VideoType == VideoType.Movie).Select(a => new { a.Title, a.SortName, a.RemoteSourceId }).Distinct(); foreach (var movie in movies) { EnsureMovie(db, new ContentModel() { Title = movie.Title, SortName = movie.SortName, RemoteSourceId = movie.RemoteSourceId }); } db.SubmitChanges(); } using (var db = new MediaManagerDataContext()) { var shows = contentModels.Where(a => a.VideoType == VideoType.TvShowEpisode).Select(a => new { Title = a.ParentTitle, SortName = a.ParentSortTitle, RemoteSourceId = a.ParentRemoteSourceId }).Distinct(); foreach (var show in shows) { EnsureShow(db, show.Title, show.SortName, show.RemoteSourceId); } db.SubmitChanges(); } using (var db = new MediaManagerDataContext()) { var episodes = contentModels.Where(a => a.VideoType == VideoType.TvShowEpisode).Select(a => new { a.Title, a.SortName, a.Season, a.Episode, a.ParentRemoteSourceId, a.ParentTitle }).Distinct(); foreach (var episode in episodes) { EnsureEpisode(db, new ContentModel() { Title = episode.Title, SortName = episode.SortName, Season = episode.Season, Episode = episode.Episode, ParentRemoteSourceId = episode.ParentRemoteSourceId, ParentTitle = episode.ParentTitle }, loadShowOnly: true); } db.SubmitChanges(); } }
// ============================================= // Remote Commands Functions // ============================================= public RemoteCommand CheckForCommand(string serverKey) { using (var db = new MediaManagerDataContext()) { var server = Loader.LoadServer(db, serverKey); if (server == null) return RemoteCommand.NoCommand; var commandQueue = server.ServerCommandQueues.FirstOrDefault(a => !a.InProgress); if (commandQueue != null) { commandQueue.InProgress = true; commandQueue.Started = DateTime.UtcNow; if (commandQueue.Command != RemoteCommand.Transcode) { server.CurrentCommand = commandQueue.Command; } else { server.CurrentlyTranscoding = true; } db.SubmitChanges(); return commandQueue.Command; } } return RemoteCommand.NoCommand; }
/// <summary> /// Creates and ensures Library Names exist /// </summary> /// <param name="libraries"></param> /// <param name="serverKey"></param> public void EnsureLibraries(List<string> libraries, string serverKey) { using (var db = new MediaManagerDataContext()) { var server = Loader.LoadServer(db, serverKey); if (server != null) { foreach (var lib in libraries) { if (!db.Libraries.Any(a => a.Server == server && a.Name == lib)) { db.Libraries.InsertOnSubmit(new Library() { Name = lib, Server = server }); } } } db.SubmitChanges(); } }
public void CommandComplete(string serverKey, RemoteCommand remoteCommand, List<string> keys = null ) { using (var db = new MediaManagerDataContext()) { var server = Loader.LoadServer(db, serverKey); if (server == null) return; var commandQueue = server.ServerCommandQueues.FirstOrDefault(a => a.InProgress && a.Command == remoteCommand); // update the server's current status if (remoteCommand != RemoteCommand.Transcode) { server.CurrentCommand = RemoteCommand.NoCommand; if (remoteCommand == RemoteCommand.CopyFromDrive) server.CopyFromDrive = null; if (remoteCommand == RemoteCommand.CopyToDrive) { if (keys == null) { db.WishListFiles.DeleteAllOnSubmit(db.WishListFiles.Where(a => a.ServerMedia.ServerId == server.Id && a.Approved)); } else { db.WishListFiles.DeleteAllOnSubmit(db.WishListFiles.Where(a => a.ServerMedia.ServerId == server.Id && a.Approved && keys.Contains(a.ServerMedia.FilePath))); } server.CopyToDrive = null; } } else { server.CurrentlyTranscoding = false; } if (commandQueue != null) { db.ServerCommandQueues.DeleteOnSubmit(commandQueue); } db.SubmitChanges(); } }
// ============================================= // Transcoding Functions // ============================================= public QueueModel TopQueue(string serverKey) { using (var db = new MediaManagerDataContext()) { // exit if currently running job if (db.TranscodingQueues.Any(a => a.Server.Key == new Guid(serverKey) && !a.Complete && a.DateTimeStarted.HasValue)) { return null; } var queue = db.TranscodingQueues.FirstOrDefault(a => a.Server.Key == new Guid(serverKey) && a.Pending); // no job if (queue == null) return null; var qm = new QueueModel() { QueueId = queue.Id, FilePath = queue.ServerMedia.FilePath, TargetFormat = queue.TargetFormat.Name, VideoType = queue.ServerMedia.Movie != null ? VideoType.Movie : VideoType.TvShowEpisode }; queue.Pending = false; queue.DateTimeStarted = DateTime.UtcNow; db.SubmitChanges(); return qm; } }
// ============================================= // File Import / Copy Functions // ============================================= public void ReportDrives(string serverKey, IList<DriveModel> driveModels) { using (var db = new MediaManagerDataContext()) { var server = db.Servers.FirstOrDefault(a => a.Key == new Guid(serverKey) && a.IsActive); if (server != null) { var existingDrives = db.ServerDrives.Where(a => a.Server.Key == new Guid(serverKey)); db.ServerDrives.DeleteAllOnSubmit(existingDrives); var newDrives = driveModels.Select(a => new ServerDrive() { Name = a.Name, DriveType = a.DriveType.ToString(), VolumeLabel = a.VolumeLabel, AvailableSpace = a.AvailableSpace, ServerId = server.Id, Id = Guid.NewGuid() }); db.ServerDrives.InsertAllOnSubmit(newDrives); } db.SubmitChanges(); } }
public void PopQueue(string serverKey, int queueId) { using (var db = new MediaManagerDataContext()) { var queue = db.TranscodingQueues.FirstOrDefault(a => a.Server.Key == new Guid(serverKey) && a.Id == queueId); if (queue == null) { throw new ArgumentException("Queue does not match server."); } queue.DateTimecompleted = DateTime.UtcNow; queue.Complete = true; db.SubmitChanges(); } }
public CopyModel CopyFiles(string serverKey) { using (var db = new MediaManagerDataContext()) { var server = db.Servers.FirstOrDefault(a => a.Key == new Guid(serverKey) && a.IsActive); if (server != null && server.CurrentCommand == RemoteCommand.CopyToDrive) { //var targetDrive = server.ServerDrives.FirstOrDefault(a => a.Selected); var targetDrive = server.CopyToDrive; if (targetDrive != null) { var wlfiles = db.WishListFiles.Where(a => a.ServerMedia.Server == server && a.Approved); var results = new List<KeyValuePair<string, VideoType>>(); results.AddRange(wlfiles.Select(a => new KeyValuePair<string, VideoType>(a.ServerMedia.FilePath, a.ServerMedia.Movie != null ? VideoType.Movie : VideoType.TvShowEpisode))); db.SubmitChanges(); return new CopyModel() { Drive = server.CopyToDrive, Files = results }; } } } return null; }
/// <summary> /// Assigns media objects to server /// </summary> /// <param name="contentModels"></param> /// <param name="serverKey"></param> public IEnumerable<CacheModel> UpdateContent(IEnumerable<ContentModel> contentModels, string serverKey) { // assign the media to servers using (var db = new MediaManagerDataContext()) { var server = Loader.LoadServer(db, serverKey); if (server != null) { foreach (var cm in contentModels) { if (cm.VideoType == VideoType.Movie) { var movie = EnsureMovie(db, cm, true); AssignMovieToServer(db, server, cm, movie); } if (cm.VideoType == VideoType.TvShowEpisode) { var episode = EnsureEpisode(db, cm, true, true); AssignEpisodeToServer(db, server, cm, episode); } } } db.SubmitChanges(); } // get the ids using (var db = new MediaManagerDataContext()) { var server = Loader.LoadServer(db, serverKey); var mediaIds = contentModels.SelectMany(a => a.MediaModels.Select(b => b.MediaId)); var cache = db.ServerMedias.Where(a => a.Server == server && mediaIds.Contains(a.MediaId)).Select(a => new CacheModel() { VideoType = a.Movie != null ? VideoType.Movie : VideoType.TvShowEpisode, ServerMediaId = a.Id, VideoId = a.VideoId, MediaId = a.MediaId, Hash = a.Hash }); return cache.ToList(); } }
/// <summary> /// Removes a server media object from a server /// </summary> /// <param name="mediaIds"></param> /// <param name="serverKey"></param> public void RemoveContent(IEnumerable<int> mediaIds, string serverKey) { using (var db = new MediaManagerDataContext()) { var server = Loader.LoadServer(db, serverKey); if (server != null) { //foreach (var id in mediaIds) //{ // var sm = db.ServerMedias.FirstOrDefault(a => a.Id == id && a.Server == server); // if (sm != null) // { // db.ServerMedias.DeleteOnSubmit(sm); // } //} var medias = db.ServerMedias.Where(a => mediaIds.Contains(a.Id) && a.Server == server); db.WishListFiles.DeleteAllOnSubmit(medias.SelectMany(a => a.WishListFiles)); db.ServerMedias.DeleteAllOnSubmit(medias); db.SubmitChanges(); } } }