public void SaveAsync(AssignmentStream stream, Workspace workspace, EventHandler onSaveComplete) { // Create another thread to do this ParameterizedThreadStart pts = new ParameterizedThreadStart(this.SaveAsyncThreadProc); Thread saveThread = new Thread(pts); saveThread.Start(new object[] { stream, workspace, onSaveComplete }); }
/// <summary> /// Thread procedure for saving an assignment. Compresses the workspace save data and then starts /// the save process by re-authenticating. /// </summary> private void SaveAsyncThreadProc(object o) { AssignmentStream stream = (o as object[])[0] as AssignmentStream; Workspace workspace = (o as object[])[1] as Workspace; EventHandler onSaveComplete = (o as object[])[2] as EventHandler; // Build the zip file here. Start by saving the workspace to a memory stream. MemoryStream ms = new MemoryStream(); workspace.Save(ms); // Determine a file name and compress the file data. The contract (at the time of this writing) // is that the file name in the uploaded assignments must ALWAYS match the deliverable file // name. This is true for both reviews and basic assignments. string fileName = OSBLEState.GetDeliverableFileName(this); byte[] zipData = CreateZipFile(ms.ToArray(), fileName); // Dispose the memory stream, as we are done with it ms.Dispose(); ms = null; // If it's null then we have a problem if (null == zipData) { onSaveComplete(this, new SaveEventArgs(false)); return; } #if DEBUG IList <string> fileNamesCheck = GetZipFileNames(zipData); if (1 != fileNamesCheck.Count) { throw new Exception("Workspace save data could not be correctly compressed"); } #endif //AuthenticationServiceClient auth = new AuthenticationServiceClient( // m_bind, new System.ServiceModel.EndpointAddress(OSBLEState.AuthServiceLink)); AuthenticationServiceClient auth = new AuthenticationServiceClient(); auth.ValidateUserCompleted += new EventHandler <ValidateUserCompletedEventArgs>(AuthForSaveCompleted); // Build an array of parameters object[] args = new object[] { auth, onSaveComplete, zipData, (null == stream) ? -1 : stream.OriginalAuthorID }; // Authenticate so that we can save to OSBLE auth.ValidateUserAsync(m_userName, m_password, args); }
private bool AddFromZip(byte[] zipFileData) { // Make a memory stream for the byte array MemoryStream ms = new MemoryStream(zipFileData); // Create the zip file ICSharpCode.SharpZipLib.Zip.ZipFile zf; try { zf = new ICSharpCode.SharpZipLib.Zip.ZipFile(ms); } catch (Exception) { return(false); } // Go through the files within the zip foreach (ZipEntry ze in zf) { string name = ZipEntry.CleanName(ze.Name); string nameLwr = name.ToLower(); int placeholder = 0; // The file name structure tells us whether or not this file is one of the "originals" // for a review assignment. An original in this case means the file that was submitted // for review. A single review assignment will have a collection of files, some of // which are originals and the rest are submitted reviews. bool isOriginal = false; if (nameLwr.StartsWith("originals/")) { name = name.Substring(10); isOriginal = true; } else if (nameLwr.StartsWith("reviews/")) { name = name.Substring(8); isOriginal = false; } else if (Int32.TryParse(nameLwr[0].ToString(), out placeholder)) { //document is a merged document for review discussions string[] pieces = name.Split(';'); name = pieces[1]; } else { name = System.IO.Path.GetFileName(name); } // Read the whole thing into a memory stream AssignmentStream msUncompressed; using (Stream tempStream = zf.GetInputStream(ze)) { msUncompressed = new AssignmentStream(name, this, true, isOriginal); tempStream.CopyTo(msUncompressed); } // Add the file to the collection m_files.Add(msUncompressed); } ms.Dispose(); return(true); }