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 impersonation = 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; } }
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 }; }
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; } }
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; }
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(); }