public void InstallPlugin(string sourcePath, string targetName, MediaBrowser.Library.Network.WebDownload.PluginInstallUpdateCB updateCB, MediaBrowser.Library.Network.WebDownload.PluginInstallFinishCB doneCB, MediaBrowser.Library.Network.WebDownload.PluginInstallErrorCB errorCB) { string target = Path.Combine(ApplicationPaths.AppPluginPath, targetName); if (sourcePath.ToLower().StartsWith("http")) { // Initialise Async Web Request int BUFFER_SIZE = 1024; Uri fileURI = new Uri(sourcePath); WebRequest request = WebRequest.Create(fileURI); Network.WebDownload.State requestState = new Network.WebDownload.State(BUFFER_SIZE, target); requestState.request = request; requestState.fileURI = fileURI; requestState.progCB = updateCB; requestState.doneCB = doneCB; requestState.errorCB = errorCB; IAsyncResult result = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(ResponseCallback), requestState); } else { File.Copy(sourcePath, target, true); InitialisePlugin(target); } // Moved code to InitialisePlugin() //Function needs to be called at end of Async dl process as well }
/// <summary> /// Main callback invoked in response to the Stream.BeginRead method, when we have some data. /// </summary> private void ReadCallback(IAsyncResult asyncResult) { Network.WebDownload.State requestState = ((Network.WebDownload.State)(asyncResult.AsyncState)); try { Stream responseStream = requestState.streamResponse; // Get results of read operation int bytesRead = responseStream.EndRead(asyncResult); // Got some data, need to read more if (bytesRead > 0) { // Save Data requestState.downloadDest.Write(requestState.bufferRead, 0, bytesRead); // Report some progress, including total # bytes read, % complete, and transfer rate requestState.bytesRead += bytesRead; double percentComplete = ((double)requestState.bytesRead / (double)requestState.totalBytes) * 100.0f; //Callback to GUI to update progress if (requestState.progCB != null) { requestState.progCB(percentComplete); } // Kick off another read IAsyncResult ar = responseStream.BeginRead(requestState.bufferRead, 0, requestState.bufferRead.Length, new AsyncCallback(ReadCallback), requestState); return; } // EndRead returned 0, so no more data to be read else { responseStream.Close(); requestState.response.Close(); requestState.downloadDest.Flush(); requestState.downloadDest.Close(); //Callback to GUI to report download has completed if (requestState.doneCB != null) { requestState.doneCB(); } } } catch (PluginAlreadyLoadedException) { Logger.ReportWarning("Attempting to install a plugin that is already loaded: " + requestState.fileURI); } catch (WebException ex) { //Callback to GUI to report an error has occured. if (requestState.errorCB != null) { requestState.errorCB(ex); } } }
public void InstallPlugin(string sourcePath, string targetName, bool globalTarget, MediaBrowser.Library.Network.WebDownload.PluginInstallUpdateCB updateCB, MediaBrowser.Library.Network.WebDownload.PluginInstallFinishCB doneCB, MediaBrowser.Library.Network.WebDownload.PluginInstallErrorCB errorCB) { string target; if (globalTarget) { //install to ehome for now - can change this to GAC if figure out how... target = Path.Combine(System.Environment.GetEnvironmentVariable("windir"), Path.Combine("ehome", targetName)); //and put our pointer file in "plugins" File.Create(Path.Combine(ApplicationPaths.AppPluginPath, Path.ChangeExtension(Path.GetFileName(targetName), ".pgn"))); } else { target = Path.Combine(ApplicationPaths.AppPluginPath, targetName); } if (sourcePath.ToLower().StartsWith("http")) { // Initialise Async Web Request int BUFFER_SIZE = 1024; Uri fileURI = new Uri(sourcePath); WebRequest request = WebRequest.Create(fileURI); Network.WebDownload.State requestState = new Network.WebDownload.State(BUFFER_SIZE, target); requestState.request = request; requestState.fileURI = fileURI; requestState.progCB = updateCB; requestState.doneCB = doneCB; requestState.errorCB = errorCB; IAsyncResult result = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(ResponseCallback), requestState); } else { File.Copy(sourcePath, target, true); InitialisePlugin(target); } // Moved code to InitialisePlugin() //Function needs to be called at end of Async dl process as well }
/// <summary> /// Main response callback, invoked once we have first Response packet from /// server. This is where we initiate the actual file transfer, reading from /// a stream. /// </summary> /// <param name="asyncResult"></param> private void ResponseCallback(IAsyncResult asyncResult) { Network.WebDownload.State requestState = ((Network.WebDownload.State)(asyncResult.AsyncState)); try { WebRequest req = requestState.request; // HTTP if (requestState.fileURI.Scheme == Uri.UriSchemeHttp) { HttpWebResponse resp = ((HttpWebResponse)(req.EndGetResponse(asyncResult))); requestState.response = resp; requestState.totalBytes = requestState.response.ContentLength; } else { throw new ApplicationException("Unexpected URI"); } // Set up a stream, for reading response data into it Stream responseStream = requestState.response.GetResponseStream(); requestState.streamResponse = responseStream; // Begin reading contents of the response data IAsyncResult ar = responseStream.BeginRead(requestState.bufferRead, 0, requestState.bufferRead.Length, new AsyncCallback(ReadCallback), requestState); return; } catch (WebException ex) { //Callback to GUI to report an error has occured. if (requestState.errorCB != null) { requestState.errorCB(ex); } } }