Exemple #1
0
        private static FileServer SetupFileServer(Options options,
                                                  DlnaMediaTypes types,
                                                  DirectoryInfo[] d)
        {
            var ids = new Identifiers(
                ComparerRepository.Lookup(options.Order), options.DescendingOrder);

            foreach (var v in options.Views)
            {
                try {
                    ids.AddView(v);
                }
                catch (RepositoryLookupException) {
                    throw new GetOptException("Invalid view " + v);
                }
            }
            var fs = new FileServer(types, ids, d);

            try {
                if (options.CacheFile != null)
                {
                    fs.SetCacheFile(options.CacheFile);
                }
                fs.Load();
            }
            catch (Exception) {
                fs.Dispose();
                throw;
            }
            return(fs);
        }
Exemple #2
0
        private static FileServer SetupFileServer(DlnaMediaTypes types, string[] Tags)
        {
            var ids = new Identifiers(
                ComparerRepository.Lookup("title"), true);

            string[] Views = new string[0];
            foreach (var v in Views)
            {
                try
                {
                    ids.AddView(v);
                }
                catch (RepositoryLookupException)
                {
                    throw new Exception("Invalid view " + v);
                }
            }

            var fs = new FileServer(types, ids, Tags.ToList());

            fs.FriendlyName     = Tags[0];
            Program.previews_db = new SQLiteConnection("data source=" + "C:\\utils\\data\\Previews.sqlite");
            Program.previews_db.Open();
            fs.PreviewsDB           = Program.previews_db;
            fs.ErzaConnectionString = "data source=C:\\utils\\data\\erza.sqlite";
            fs.Load();
            return(fs);
        }
Exemple #3
0
        public void ByTitleView_RegisterFolder_Test()
        {
            var ids = new Identifiers(ComparerRepository.Lookup("title"), false);

            ids.AddView("bytitle");
            var f      = new MediaFolder();
            var addRes = new[] {
                new MediaResource()
                {
                    Path = @"C:\somepath\resrouceZ.mkv", Title = "Z"
                }
                , new MediaResource()
                {
                    Path = @"C:\somepath\resrouceY.mkv", Title = "Y"
                }
                , new MediaResource()
                {
                    Path = @"C:\somepath\resrouceV.mkv", Title = "V"
                }
                , new MediaResource()
                {
                    Path = @"C:\somepath\resrouceO.mkv", Title = "O"
                }
                , new MediaResource()
                {
                    Path = @"C:\somepath\resrouceP.mkv", Title = "P"
                }
                , new MediaResource()
                {
                    Path = @"C:\somepath\resrouceQ.mkv", Title = "Q"
                }
                , new MediaResource()
                {
                    Path = @"C:\somepath\resrouceM.mkv", Title = "M"
                }
                , new MediaResource()
                {
                    Path = @"C:\somepath\resrouceE.mkv", Title = "E"
                }
            };

            f.AccessorChildItems.AddRange(addRes);
            f.AccessorChildFolders.Add(new MediaFolder()
            {
                Path = @"C:\somepath"
            });
            ids.RegisterFolder("tempid", f);
            var res = ids.Resources;

            Assert.IsTrue(res.Select(r => r.Target).ToList().Contains(addRes[2]), "Not contains added resource");
            Assert.IsNotNull(ids.GetItemByPath("/:/:Q"), "GetItemByPath(\"/:/:Q\") failed");
            Assert.AreEqual(ids.GetItemById(addRes[2].Id), addRes[2]);
            //target.Load();
        }
Exemple #4
0
        private void StartFileServer()
        {
            if (!Description.Active)
            {
                state = State.Stopped;
                return;
            }
            try {
                state = State.Loading;
                var ids = new Identifiers(ComparerRepository.Lookup(Description.Order), Description.OrderDescending);
                foreach (var v in Description.Views)
                {
                    ids.AddView(v);
                }
                var dirs = (from i in Description.Directories
                            let d = new DirectoryInfo(i)
                                    where d.Exists
                                    select d).ToArray();
                if (dirs.Length == 0)
                {
                    throw new InvalidOperationException("No remaining directories");
                }
                fileServer = new FileServer(Description.Types, ids, dirs)
                {
                    FriendlyName = Description.Name
                };
#if !DEBUG
                if (cacheFile != null)
                {
                    fileServer.SetCacheFile(cacheFile);
                }
#endif
                fileServer.Changing += (o, e) =>
                {
                    state = State.Refreshing;
                };
                fileServer.Changed += (o, e) =>
                {
                    state = Description.Active ? State.Running : State.Stopped;
                };
                fileServer.Load();
                server.RegisterMediaServer(fileServer);
                state = State.Running;
            }
            catch (Exception ex) {
                server.ErrorFormat("Failed to start {0}, {1}", Description.Name, ex);
                Description.ToggleActive();
                state = State.Stopped;
            }
        }
        public string Get(int id)
        {
            using (var comparerRepository = new ComparerRepository())
            {
                var comparer = comparerRepository.Find(id);

                if (comparer is null)
                {
                    throw new ArgumentNullException("Comparer not found");
                }

                if (comparer.Left.Equals(comparer.Right))
                {
                    return("Contents are the same.");
                }

                if (!comparer.Left.Length.Equals(comparer.Right.Length))
                {
                    return("Contents are not of the same size.");
                }

                string jsonLeft  = Encoding.UTF8.GetString(Convert.FromBase64String(comparer.Left));
                string jsonRight = Encoding.UTF8.GetString(Convert.FromBase64String(comparer.Right));

                int length = 0;
                int?offset = null;
                List <DiffEntry> diffEntries = new List <DiffEntry>();

                for (int i = 0; i < jsonLeft.Length; i++)
                {
                    if (jsonLeft[i] != jsonRight[i])
                    {
                        if (length == 0)
                        {
                            offset = i;
                        }

                        length++;
                    }
                    else if (length > 0)
                    {
                        diffEntries.Add(new DiffEntry((int)offset, length));
                        length = 0;
                        offset = 0;
                    }
                }

                return(Newtonsoft.Json.JsonConvert.SerializeObject(diffEntries));
            }
        }
Exemple #6
0
        private static void ListOrders()
        {
            var items = from v in ComparerRepository.ListItems()
                        orderby v.Key
                        select v.Value;

            Console.WriteLine("Available orders:");
            Console.WriteLine("----------------");
            Console.WriteLine();
            foreach (var i in items)
            {
                Console.WriteLine("  - " + i);
                Console.WriteLine();
            }
        }
Exemple #7
0
        private void AddOrderItems()
        {
            var items = from v in ComparerRepository.ListItems()
                        orderby v.Key
                        select v.Value;

            foreach (var v in items)
            {
                var i = comboOrder.Items.Add(v);
                if (v.Name == "title")
                {
                    comboOrder.SelectedIndex = i;
                }
            }
        }
Exemple #8
0
        private static FileServer SetupFileServer(Options options,
                                                  DlnaMediaTypes types,
                                                  DirectoryInfo[] d)
        {
            var ids = new Identifiers(
                ComparerRepository.Lookup(options.Order), options.DescendingOrder);

            foreach (var v in options.Views)
            {
                try
                {
                    ids.AddView(v);
                }
                catch (RepositoryLookupException)
                {
                    throw new GetOptException("Invalid view " + v);
                }
            }
            var fs = new FileServer(types, ids, d);

            if (!string.IsNullOrEmpty(options.FriendlyName))
            {
                fs.FriendlyName = options.FriendlyName;
            }
            fs.ShowHidden = options.ShowHidden;
            fs.ShowSample = options.ShowSample;
            try
            {
                if (options.CacheFile != null)
                {
                    fs.SetCacheFile(options.CacheFile);
                }
                fs.Load();
                if (!options.Rescanning)
                {
                    fs.Rescanning = false;
                }
            }
            catch (Exception)
            {
                fs.Dispose();
                throw;
            }
            return(fs);
        }
        public string SetContent(int id, string content, Side side = Side.Left)
        {
            if (string.IsNullOrWhiteSpace(content))
            {
                throw new ArgumentException("null content");
            }

            if (!IsBase64String(content))
            {
                throw new ArgumentException("invalid content");
            }

            using (var comparerRepository = new ComparerRepository())
            {
                var comparer = comparerRepository.Find(id);

                if (comparer is null)
                {
                    comparer = new Comparer()
                    {
                        Id = id
                    };

                    comparerRepository.Add(comparer);
                }

                if (side == Side.Left)
                {
                    comparer.Left = content;
                }
                else
                {
                    comparer.Right = content;
                }

                comparerRepository.Update(comparer);

                return($"{side}-side Content was set");
            }
        }
        private void StartFileServer()
        {
            if (!Description.Active)
            {
                InternalState = State.Stopped;
                return;
            }
            var start = DateTime.Now;

            try
            {
                InternalState = State.Loading;
                var ids = new Identifiers(ComparerRepository.Lookup(Description.Order), Description.OrderDescending);
                foreach (var v in Description.Views)
                {
                    ids.AddView(v);
                }
                var dirs = (from i in Description.Directories
                            let d = new DirectoryInfo(i)
                                    where d.Exists
                                    select d).ToArray();
                if (dirs.Length == 0)
                {
                    throw new InvalidOperationException("No remaining directories");
                }
                fileServer = new FileServer(Description.Types, ids, dirs)
                {
                    FriendlyName = Description.Name
                };
#if !DEBUG
                if (cacheFile != null)
                {
                    fileServer.SetCacheFile(cacheFile);
                }
#endif
                fileServer.Changing += (o, e) => { InternalState = State.Refreshing; };
                fileServer.Changed  += (o, e) => { InternalState = Description.Active ? State.Running : State.Stopped; };
                fileServer.Load();
                var authorizer = new HttpAuthorizer();
                if (Description.Ips.Length != 0)
                {
                    authorizer.AddMethod(new IPAddressAuthorizer(Description.Ips));
                }
                if (Description.Macs.Length != 0)
                {
                    authorizer.AddMethod(new MacAuthorizer(Description.Macs));
                }
                if (Description.UserAgents.Length != 0)
                {
                    authorizer.AddMethod(new UserAgentAuthorizer(Description.UserAgents));
                }
                fileServer.Authorizer = authorizer;
                server.RegisterMediaServer(fileServer);
                InternalState = State.Running;
                var elapsed = DateTime.Now - start;
                LogManager.GetLogger("State").Logger.Log(
                    GetType(),
                    Level.Notice,
                    $"{fileServer.FriendlyName} loaded in {elapsed.TotalSeconds:F2} seconds",
                    null
                    );
            }
            catch (Exception ex)
            {
                server.ErrorFormat("Failed to start {0}, {1}", Description.Name, ex);
                Description.ToggleActive();
                InternalState = State.Stopped;
            }
        }