Inheritance: NMaier.SimpleDlna.Utilities.Logging, IMediaServer, IVolatileMediaServer, IDisposable
Beispiel #1
0
        protected BaseFile(FileServer server, FileInfo aFile, DlnaMime aType, DlnaMediaTypes aMediaType)
        {
            if (server == null) {
            throw new ArgumentNullException("server");
              }
              this.server = server;
              Item = aFile;

              length = Item.Length;
              lastModified = Item.LastWriteTimeUtc;

              Type = aType;
              MediaType = aMediaType;

              title = System.IO.Path.GetFileNameWithoutExtension(Item.Name);
              if (string.IsNullOrEmpty(title)) {
            title = Item.Name;
              }
              if (!string.IsNullOrWhiteSpace(title)) {
            try {
              title = Uri.UnescapeDataString(title);
            }
            catch (UriFormatException) {
              // no op
            }
              }
              title = title.StemNameBase();
        }
Beispiel #2
0
        private PlainFolder(FileServer server, DlnaMediaTypes types, VirtualFolder parent, DirectoryInfo dir, IEnumerable<string> exts)
            : base(parent, dir.Name)
        {
            Server = server;
              this.dir = dir;
              folders = (from d in dir.GetDirectories()
                 let m = TryGetFolder(server, types, d)
                 where m != null && m.ChildCount > 0
                 select m as IMediaFolder).ToList();

              var rawfiles = from f in dir.GetFiles("*.*")
                     select f;
              var files = new List<BaseFile>();
              foreach (var f in rawfiles) {
            var ext = f.Extension;
            if (string.IsNullOrEmpty(ext) ||
              !exts.Contains(ext.Substring(1), StringComparer.InvariantCultureIgnoreCase)) {
              continue;
            }
            try {
              files.Add(server.GetFile(this, f));
            }
            catch (Exception ex) {
              server.Warn(f, ex);
            }
              }
              resources.AddRange(files);
        }
 public void Dispose()
 {
     if (fileServer != null) {
     fileServer.Dispose();
     fileServer = null;
       }
 }
Beispiel #4
0
        internal PlainFolder(FileServer server, VirtualFolder parent, DirectoryInfo dir)
            : base(parent, dir.Name)
        {
            Server = server;
              this.dir = dir;
              var rawfiles = from f in dir.GetFiles("*.*")
                     select f;
              var files = new List<BaseFile>();
              foreach (var f in rawfiles) {
            var ext = f.Extension;
            if (string.IsNullOrEmpty(ext) ||
            !server.Filter.Filtered(ext.Substring(1))) {
              continue;
            }
            try {
              var file = server.GetFile(this, f);
              if (server.Allowed(file)) {
            files.Add(file);
              }
            }
            catch (Exception ex) {
              server.Warn(f, ex);
            }
              }
              Resources.AddRange(files);

              Folders = (from d in dir.GetDirectories()
                 let m = TryGetFolder(server, d)
                 where m != null && m.ChildCount > 0
                 select m as IMediaFolder).ToList();
        }
Beispiel #5
0
 private PlainFolder TryGetFolder(FileServer server, DirectoryInfo d)
 {
     try {
     return new PlainFolder(server, this, d);
       }
       catch (Exception ex) {
     if (!d.Name.Equals("System Volume Information")) {
       server.Warn("Failed to access folder", ex);
     }
     return null;
       }
 }
 public void FileStoreReaderWriter_File_Roundtrip_Test()
 {
     var store = new FileStoreMock();
       var fi = new FileInfo(@"img\Patern_test.jpg");
       var reader = new FileStoreReader(store);
       var writer = new FileStoreWriter(store);
       var server = new FileServer(DlnaMediaTypes.All, null, new DirectoryInfo[] { new DirectoryInfo(".") });
       var item = new BaseFileMock(server,fi,DlnaMime.AudioAAC,DlnaMediaTypes.Image);
       writer.StoreFile(item);
       var result = reader.GetFile(fi, null, NMaier.SimpleDlna.Server.DlnaMime.ImageJPEG);
       Assert.IsNotNull(result);
       Assert.AreEqual(item.Path, result.Path);
 }
 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;
       }
 }
Beispiel #8
0
 internal AudioFile(FileServer server, FileInfo aFile, DlnaMime aType)
     : base(server, aFile, aType, DlnaMediaTypes.Audio)
 {
 }
 public DeserializeInfo(FileServer server, FileInfo info, DlnaMime type)
 {
   Server = server;
   Info = info;
   Type = type;
 }
Beispiel #10
0
 internal ImageFile(FileServer server, FileInfo aFile, DlnaMime aType)
     : base(server, aFile, aType, DlnaMediaTypes.Image)
 {
 }
Beispiel #11
0
 internal AudioFile(FileServer server, FileInfo aFile, DlnaMime aType)
     : base(server, aFile, aType, DlnaMediaTypes.Audio)
 {
 }
    private void StopFileServer()
    {
      if (!Description.Active || fileServer == null) {
        return;
      }
      server.UnregisterMediaServer(fileServer);
      fileServer.Dispose();
      fileServer = null;

      state = State.Stopped;
    }
Beispiel #13
0
 protected PlainFolder(FileServer server, DlnaMediaTypes types,
                       VirtualFolder parent, DirectoryInfo dir)
     : this(server, types, parent, dir, types.GetExtensions())
 {
 }
 internal PlainRootFolder(FileServer server, DlnaMediaTypes types,
                          DirectoryInfo di)
   : base(server, types, null, di)
 {
   Id = Identifiers.GeneralRoot;
 }
Beispiel #15
0
 internal BaseFile MaybeGetFile(FileServer server, FileInfo info, DlnaMime type)
 {
     if (connection == null) {
     return null;
       }
       byte[] data;
       lock (connection) {
     selectKey.Value = info.FullName;
     selectSize.Value = info.Length;
     selectTime.Value = info.LastWriteTimeUtc.Ticks;
     try {
       data = select.ExecuteScalar() as byte[];
     }
     catch (DbException ex) {
       Error("Failed to lookup file from store", ex);
       return null;
     }
       }
       if (data == null) {
     return null;
       }
       try {
     using (var s = new MemoryStream(data)) {
       var ctx = new StreamingContext(StreamingContextStates.Persistence, new DeserializeInfo(server, info, type));
       var formatter = new BinaryFormatter(null, ctx) { TypeFormat = FormatterTypeStyle.TypesWhenNeeded, AssemblyFormat = FormatterAssemblyStyle.Simple };
       var rv = formatter.Deserialize(s) as BaseFile;
       rv.Item = info;
       return rv;
     }
       }
       catch (Exception ex) {
     if (ex is TargetInvocationException || ex is SerializationException) {
       Debug("Failed to deserialize an item", ex);
       return null;
     }
     throw;
       }
 }
 public BaseFileMock(FileServer server, FileInfo file, DlnaMime type,
            DlnaMediaTypes mediaType)
     : base(server,file,type,mediaType)
 {
 }
 public PlainRootFolder(FileServer server, DlnaMediaTypes types, DirectoryInfo di)
     : base(server, types, null, di)
 {
     Id = Identifiers.ROOT;
 }
Beispiel #18
0
        private static FileServer ConfiguraServidorDeArquivos(Opcoes opcoes, DlnaMediaTypes tipos, DirectoryInfo[] d)
        {
            var ids = new Identifiers(ComparerRepository.Lookup(opcoes.Ordem), opcoes.Ascendente);

            var fs = new FileServer(tipos, ids, d);
            try
            {
                if (opcoes.ArquivoCache != null)
                {
                    fs.SetCacheFile(opcoes.ArquivoCache);
                }
                fs.Load();
            }
            catch (Exception)
            {
                fs.Dispose();
                throw;
            }
            return fs;
        }
Beispiel #19
0
 internal VideoFile(FileServer server, FileInfo aFile, DlnaMime aType)
     : base(server, aFile, aType, DlnaMediaTypes.Video)
 {
 }
Beispiel #20
0
 internal VideoFile(FileServer server, FileInfo aFile, DlnaMime aType)
     : base(server, aFile, aType, DlnaMediaTypes.Video)
 {
 }
Beispiel #21
0
 private PlainFolder TryGetFolder(FileServer server, DlnaMediaTypes types, DirectoryInfo d)
 {
     try {
     return new PlainFolder(server, types, this, d);
       }
       catch (Exception ex) {
     server.Warn("Failed to access folder", ex);
     return null;
       }
 }
Beispiel #22
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;
   }
   try {
     if (options.CacheFile != null) {
       fs.SetCacheFile(options.CacheFile);
     }
     fs.Load();
     if (!options.Rescanning) {
       fs.Rescanning = false;
     }
   }
   catch (Exception) {
     fs.Dispose();
     throw;
   }
   return fs;
 }
Beispiel #23
0
 protected PlainFolder(FileServer server, DlnaMediaTypes types, VirtualFolder parent, DirectoryInfo dir)
     : this(server, types, parent, dir, GetExtensions(types))
 {
 }
    private void StartFileServer()
    {
      if (!Description.Active) {
        state = State.Stopped;
        return;
      }
      var start = DateTime.Now;
      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();
        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);
        state = State.Running;
        var elapsed = DateTime.Now - start;
        LogManager.GetLogger("State").Logger.Log(
          GetType(),
          Level.Notice,
          string.Format(
            "{0} loaded in {1:F2} seconds",
            fileServer.FriendlyName,
            elapsed.TotalSeconds),
          null
        );
      }
      catch (Exception ex) {
        server.ErrorFormat("Failed to start {0}, {1}", Description.Name, ex);
        Description.ToggleActive();
        state = State.Stopped;
      }
    }
Beispiel #25
0
 internal ImageFile(FileServer server, FileInfo aFile, DlnaMime aType)
     : base(server, aFile, aType, DlnaMediaTypes.Image)
 {
 }