예제 #1
0
        public static Stream ExtractImage(MediaSource source, long position, int? maxWidth, int? maxHeight, string borders, string format)
        {
            if (!source.Exists)
            {
                Log.Warn("ExtractImage: Source {0} (resolved to path {1}) doesn't exists", source.GetDebugName(), source.GetPath());
                WCFUtil.SetResponseCode(System.Net.HttpStatusCode.NotFound);
                return Stream.Null;
            }

            if (!source.SupportsDirectAccess)
            {
                Log.Warn("ExtractImage: Extracting images from remote sources isn't supported yet", source.MediaType, source.Id);
                WCFUtil.SetResponseCode(System.Net.HttpStatusCode.NotImplemented);
                return Stream.Null;
            }

            // get temporary filename
            string filename = String.Format("extract_{0}_{1}.jpg", source.GetUniqueIdentifier(), position);
            string tempFile = cache.GetPath(filename);

            // maybe it exists in cache, return that then
            if (cache.Contains(filename))
            {
                return StreamPostprocessedImage(new ImageMediaSource(tempFile), maxWidth, maxHeight, borders, format);
            }

            // execute it
            ProcessStartInfo info = new ProcessStartInfo();
            using (NetworkShareImpersonator impersonator = new NetworkShareImpersonator(source.NeedsImpersonation))
            {
                info.Arguments = String.Format("-ss {0} -i \"{1}\" -vframes 1 -f image2 {2}", position, source.GetPath(), tempFile);
                info.FileName = Configuration.StreamingProfiles.FFMpegPath;
                info.CreateNoWindow = true;
                info.UseShellExecute = false;
                Process proc = new Process();
                proc.StartInfo = info;
                proc.Start();
                proc.WaitForExit();
            }

            // log when failed
            if (!File.Exists(tempFile))
            {
                Log.Warn("Failed to extract image to temporary file {0} with command {1}", tempFile, info.Arguments);
                WCFUtil.SetResponseCode(System.Net.HttpStatusCode.InternalServerError);
                return Stream.Null;
            }

            return StreamPostprocessedImage(new ImageMediaSource(tempFile), maxWidth, maxHeight, borders, format);
        }
예제 #2
0
        public Stream RetrieveFile(int? provider, WebMediaType mediatype, WebFileType filetype, string id, int offset)
        {
            try
            {
                string path = GetPathList(provider, mediatype, filetype, id).ElementAt(offset);
                WebFileInfo info = GetFileInfo(provider, mediatype, filetype, id, offset);

                // first try to read the file
                if (info.IsLocalFile && File.Exists(path))
                {
                    return new FileStream(path, FileMode.Open, FileAccess.Read);
                }

                // maybe the plugin has some magic
                if (!info.IsLocalFile && info.Exists && !info.OnNetworkDrive)
                {
                    return GetLibrary(provider, mediatype).GetFile(path);
                }

                // try to load it from a network drive
                if (info.OnNetworkDrive && info.Exists)
                {
                    using (NetworkShareImpersonator impersonator = new NetworkShareImpersonator())
                    {
                        return new FileStream(path, FileMode.Open, FileAccess.Read);
                    }
                }

                // fail
                Log.Warn("Requested non-existing or non-accessible file mediatype={0} filetype={1} id={2} offset={3}", mediatype, filetype, id, offset);
                WCFUtil.SetResponseCode(System.Net.HttpStatusCode.NotFound);
                return Stream.Null;
            }
            catch (Exception ex)
            {
                Log.Info("Failed to retrieve file for mediatype=" + mediatype + ", filetype=" + filetype + ", id=" + id + " and offset=" + offset, ex);
                WCFUtil.SetResponseCode(System.Net.HttpStatusCode.InternalServerError);
                return Stream.Null;
            }
        }
예제 #3
0
        public WebFileInfo GetFileInfo(int? provider, WebMediaType mediatype, WebFileType filetype, string id, int offset)
        {
            string path = "";
            try
            {
                path = GetPathList(provider, mediatype, filetype, id).ElementAt(offset);
                WebFileInfo retVal = null;

                bool tryImpersonation = false;
                try
                {
                    // first try it the usual way
                    retVal = GetLibrary(provider, mediatype).GetFileInfo(path).Finalize(provider, mediatype);
                    tryImpersonation = retVal == null || !retVal.Exists;
                }
                catch (UnauthorizedAccessException)
                {
                    // access denied, try impersonation
                    tryImpersonation = true;
                }

                if (tryImpersonation && new Uri(path).IsUnc && !FileUtil.IsAccessible(path))
                {
                    using (var impersonator = new NetworkShareImpersonator())
                    {
                        retVal = new WebFileInfo(path);
                        retVal.IsLocalFile = Configuration.Services.NetworkImpersonation.ReadInStreamingService;
                        retVal.OnNetworkDrive = true;
                    }
                }

                // Make sure to always the path property, even if the file doesn't exist. This makes debugging a lot easier, as you get actual paths in your logs now.
                retVal.Path = path;
                return retVal;
            }
            catch (ArgumentOutOfRangeException)
            {
                Log.Info("Cannot resolve mediatype={0}, filetype={1}, provider={2}, id={3}, offset={4}", mediatype, filetype, provider, id, offset);
            }
            catch (FileNotFoundException)
            {
                Log.Info("Failed to load fileinfo for non-existing file mediatype={0}, filetype={1}, provider={5}, id={2}, offset={3} (resulting in path={4})", mediatype, filetype, id, offset, path, provider);
            }
            catch (Exception ex)
            {
                Log.Info(String.Format("Failed to load fileinfo for mediatype={0}, filetype={1}, provider={5}, id={2}, offset={3} (resulting in path={4})", mediatype, filetype, id, offset, path, provider), ex);
            }

            return new WebFileInfo()
            {
                Exists = false,
                Path = String.IsNullOrWhiteSpace(path) ? null : path
            };
        }
예제 #4
0
 public bool Setup()
 {
     try
     {
         using(var impersonator = new NetworkShareImpersonator())
         {
             DataOutputStream = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
         }
     }
     catch (Exception e)
     {
         Log.Error("Failed to setup ImpersonationInputUnit", e);
         return false;
     }
     return true;
 }
예제 #5
0
        public static WebMediaInfo GetMediaInfo(MediaSource source)
        {
            // we can't use our persistent cache for TV unfortunately, but we do cache them in memory for 60 seconds
            if (source.MediaType == WebMediaType.TV)
            {
                if (tvCache.ContainsKey(source.Id) && DateTime.Now - tvCache[source.Id].Item1 > TimeSpan.FromSeconds(60))
                {
                    return tvCache[source.Id].Item2;
                }

                // save it to our memory cache for a while
                TsBuffer buf = new TsBuffer(source.Id);
                string path = buf.GetCurrentFilePath();
                Log.Debug("Using path {0} from TS buffer {1} as source for {2}", path, source.Id, source.GetDebugName());
                WebMediaInfo info = DoLoadMediaInfo(buf.GetCurrentFilePath(), true);
                tvCache[source.Id] = new Tuple<DateTime, WebMediaInfo>(DateTime.Now, info);
                return info;
            }

            // load this item from persistent disk cache, if possible
            if (persistentCache.HasForSource(source))
            {
                return persistentCache.GetForSource(source);
            }

            // some checks that only matter when we are actually going to load it from disk
            if (!source.Exists)
            {
                Log.Warn("Trying to load mediainfo for {0}, which doesn't seem to exist", source.GetDebugName());
                return null;
            }
            else if (!source.SupportsDirectAccess)
            {
                // not (yet?) supported
                Log.Warn("Loading mediainfo for non-direct access source {0} isn't supported yet", source.GetDebugName());
                return null;
            }

            // actually load it
            WebMediaInfo outInfo;
            using (NetworkShareImpersonator impersonator = new NetworkShareImpersonator(source.NeedsImpersonation))
            {
                outInfo = DoLoadMediaInfo(source.GetPath(), false);
            }
            if (outInfo != null)
            {
                persistentCache.Save(source, outInfo);
            }
            return outInfo;
        }
예제 #6
0
        public Stream ReadRecordingFile(int id)
        {
            try
            {
                WebRecordingFileInfo info = GetRecordingFileInfo(id);

                // return it as a simple file
                if (info.IsLocalFile && File.Exists(info.Path))
                {
                    return new FileStream(info.Path, FileMode.Open, FileAccess.Read);
                }

                // try to load it from a network drive
                if (info.OnNetworkDrive && info.Exists)
                {
                    using (NetworkShareImpersonator impersonation = new NetworkShareImpersonator())
                    {
                        return new FileStream(info.Path, FileMode.Open, FileAccess.Read);
                    }
                }

                // failed
                Log.Warn("No method to read file for recording {0}", id);
                WCFUtil.SetResponseCode(System.Net.HttpStatusCode.NotFound);
                return Stream.Null;
            }
            catch (Exception ex)
            {
                Log.Warn(String.Format("Failed to read file for recording {0}", id), ex);
                WCFUtil.SetResponseCode(System.Net.HttpStatusCode.InternalServerError);
                return Stream.Null;
            }
        }
예제 #7
0
        public WebRecordingFileInfo GetRecordingFileInfo(int id)
        {
            try
            {
                string filename = GetRecordingById(id).FileName;
                try
                {
                    return new WebRecordingFileInfo(new FileInfo(filename));
                }
                catch (UnauthorizedAccessException)
                {
                    // access denied, try impersonation when on a network share
                    if (new Uri(filename).IsUnc)
                    {
                        using (NetworkShareImpersonator impersonation = new NetworkShareImpersonator())
                        {
                            var ret = new WebRecordingFileInfo(filename);
                            ret.IsLocalFile = Configuration.Services.NetworkImpersonation.ReadInStreamingService;
                            ret.OnNetworkDrive = true;
                            return ret;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Warn("Failed to load fileinfo for recording", ex);
            }

            return new WebRecordingFileInfo();
        }
예제 #8
0
        public WebFileInfo GetFileInfo(int? provider, WebMediaType mediatype, WebFileType filetype, string id, int offset)
        {
            string path = "";
            try
            {
                path = GetPathList(provider, mediatype, filetype, id).ElementAt(offset);

                try
                {
                    // first try it the usual way
                    return GetLibrary(provider, mediatype).GetFileInfo(path).Finalize(provider, mediatype);
                }
                catch (UnauthorizedAccessException)
                {
                    // access denied, try impersonation
                    if (new Uri(path).IsUnc)
                    {
                        using (NetworkShareImpersonator impersonation = new NetworkShareImpersonator())
                        {
                            var ret = new WebFileInfo(path);
                            ret.IsLocalFile = Configuration.Services.NetworkImpersonation.ReadInStreamingService;
                            ret.OnNetworkDrive = true;
                            return ret;
                        }
                    }
                }
            }
            catch (ArgumentOutOfRangeException)
            {
                Log.Info("Cannot resolve mediatype={0}, filetype={1}, provider={2}, id={3}, offset={4}", mediatype, filetype, provider, id, offset);
            }
            catch (FileNotFoundException)
            {
                Log.Info("Failed to load fileinfo for non-existing file mediatype={0}, filetype={1}, provider={5}, id={2}, offset={3} (resulting in path={4})", mediatype, filetype, id, offset, path, provider);
            }
            catch (Exception ex)
            {
                Log.Info(String.Format("Failed to load fileinfo for mediatype={0}, filetype={1}, provider={5}, id={2}, offset={3} (resulting in path={4})", mediatype, filetype, id, offset, path, provider), ex);
            }

            return new WebFileInfo()
            {
                Exists = false
            };
        }
예제 #9
0
 public Stream Retrieve()
 {
     if (MediaType == WebMediaType.TV && FileType == WebFileType.Content)
     {
         return new TsBuffer(Id);
     }
     else if (SupportsDirectAccess)
     {
         using (NetworkShareImpersonator impersonator = new NetworkShareImpersonator(NeedsImpersonation))
         {
             return new FileStream(GetPath(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
         }
     }
     else if (MediaType == WebMediaType.Recording)
     {
         return Connections.TAS.ReadRecordingFile(Int32.Parse(Id));
     }
     else
     {
         return Connections.MAS.RetrieveFile(Provider, MediaType, FileType, Id, Offset);
     }
 }