/** * Saves the dictionary as an XML file to the local system (both formatted/easy to read and non-formatted) */ public void SubmitToFile() { if (TabManager == null) { BG = GameObject.Find("GaudyBG"); TabManager = BG.GetComponentInChildren <TabManager>(); ds = BG.GetComponentInChildren <DataScript>(); fileName = GlobalData.fileName; path = GlobalData.filePath; } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } TabManager.sameTab = true; TabManager.AddToDictionary(); TabManager.GetSectionImages(); fileName = GlobalData.fileName; string tempFileName = fileName.Remove(fileName.Length - 3); string data = "<body>" + ds.GetData() + "</body>"; Debug.Log(data); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(data); string textDataExtension = "ced"; if (autosaving) { textDataExtension = "auto"; } //Outputting regular file StreamWriter sw = new StreamWriter(path + tempFileName + textDataExtension, false); //sw.WriteLine(data); sw.Close(); sw.Dispose(); File.WriteAllBytes(path + tempFileName + textDataExtension, EncryptStringToBytes_Aes(data)); if (!autosaving) //Don't want to save images when autosaving, just text for now. //Formatted, easy to read version { sw = new StreamWriter(path + tempFileName + "xml", false); xmlDoc.Save(sw); sw.Close(); sw.Dispose(); //Easy to read images (For testing) sw = new StreamWriter(path + "ImageTest" + tempFileName + "xml", false); xmlDoc.LoadXml(ds.GetImagesXML()); xmlDoc.Save(sw); sw.Close(); sw.Dispose(); File.Delete(path + tempFileName + "auto"); File.Delete(path + tempFileName + "iauto"); ds.RestartAutosave(); } //Images string imgDataExtension = "cei"; if (autosaving) { textDataExtension = "iauto"; // Autosaving should have the most recent save GlobalData.caseObj.dateModified = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; } sw = new StreamWriter(path + tempFileName + imgDataExtension, false); //sw.WriteLine (ds.GetImagesXML ()); sw.Close(); sw.Dispose(); File.WriteAllBytes(path + tempFileName + imgDataExtension, EncryptStringToBytes_Aes(ds.GetImagesXML())); //Update the caseObj to create the menu file Transform content = transform.Find("SaveCasePanel/Content"); GlobalData.caseObj.description = content.Find("Row3/TMPInputField/DescriptionValue").GetComponent <TMP_InputField>().text; GlobalData.caseObj.summary = content.Find("Row5/TMPInputField/SummaryValue").GetComponent <TMP_InputField>().text; GlobalData.caseObj.tags = GetComponent <AutofillTMP>().enteredTags.ToArray(); GlobalData.caseObj.audience = content.Find("Row7/TargetAudienceValue").GetComponent <TMP_Dropdown>().captionText.text; GlobalData.caseObj.difficulty = content.Find("Row7/DifficultyValue").GetComponent <TMP_Dropdown>().captionText.text; DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime localFileModified = File.GetLastWriteTime(GlobalData.filePath + GlobalData.fileName); //GlobalData.caseObj.dateModified = (long) DateTime.UtcNow.Subtract (unixEpoch).TotalSeconds;// Old method GlobalData.caseObj.dateModified = (long)localFileModified.ToUniversalTime().Subtract(unixEpoch).TotalSeconds; File.WriteAllText(GlobalData.filePath + GlobalData.fileName.Remove(GlobalData.fileName.Length - 4) + " menu.txt", ds.ServerUploader.GetMenuText()); //Old demo menu appending system /*if (GlobalData.demo) { * bool append = true; * * StreamReader reader = new StreamReader(Application.streamingAssetsPath + GlobalData.filePath + GlobalData.fileName); * string fileText = reader.ReadToEnd(); * string[] cases = fileText.Split(new string[] { "::" }, System.StringSplitOptions.RemoveEmptyEntries); * foreach (string s in cases) { * string[] caseSplit = s.Split(new string[] { "--" }, System.StringSplitOptions.None); * if (caseSplit[1].Equals(GlobalData.fileName)) { * caseSplit[3] = GlobalData.firstName + "_" + GlobalData.lastName; * fileText = fileText.Replace(s, string.Join("--", caseSplit)); * append = false; * break; * } * } * reader.Close(); * StreamWriter writer = new StreamWriter(Application.streamingAssetsPath + "/DemoCases/Cases/MenuCases/MenuCases.txt", append); * if (append) { * writer.Write("0--{0}--ECGC Guest--{1}--000000--Beginner--User Custom Case--This case was created by one of our guests!--N/A--0--ECGC Guests--1.0--N/A::", tempFileName + "ced", GlobalData.firstName + "_" + GlobalData.lastName); * } else { * writer.Write(fileText); //Write all of the cases including the edited one * } * writer.Close(); * }*/ Debug.Log("Saved: " + path + tempFileName); //b.interactable = false; if (autosaving) { autosaving = false; Debug.Log("Data Autosaved!"); ds.ShowMessage("Data Autosaved!", false); } else { Debug.Log("Data successfully submitted!"); ds.ShowMessage("Data saved successfully!", false); } }
/** * Handles uploading all data to the server. * Currently it's a mess because I'm not sure if we'll need to store more than the max_allowed_packet variable * but it can do that if needed. Just remove most of the commented things/format them and it should work for that. */ private IEnumerator Upload() { print("Uploading to server..."); string fileName = GlobalData.fileName.Replace(" ", "_"); string serverURL = GlobalData.serverAddress + "Test.php"; string urlParams = "?webfilename=" + fileName + "&webusername=clinical&webpassword=encounters&mode=download"; string wwwText = ""; string imagesXML = ds.GetImagesXML(); int i = -1; int MAP = 0; do { //For uploading WWWForm form = new WWWForm(); form.AddField("mode", "upload"); form.AddField("fileN", fileName); form.AddField("account_id", GlobalData.accountId); form.AddField("index", i); //Used for segmenting image uploads //form.AddField ("column", "xmlData"); byte[] fileBytes = GetFileAsByteArray("<body>" + ds.GetData() + "</body>"); Debug.Log("Case file size (in bytes): " + fileBytes.Length); form.AddBinaryData("xmlData", fileBytes, fileName, "text/xml"); if (MAP == 0) { wwwText = imagesXML; //use "Placeholder" if trying to segment data; } else if (MAP * i + MAP > imagesXML.Length) { wwwText = imagesXML.Substring(MAP * i); } else { wwwText = imagesXML.Substring(MAP * i, MAP); } byte[] fileBytesImg = GetFileAsByteArray(wwwText); Debug.Log("Image file size (in bytes): " + fileBytesImg.Length); if (imagesXML.Length > 10000000) //If the xml length is greater than max_allowed_packet { print("Error: Images exceed upload size limit"); } else { form.AddBinaryData("imgData", fileBytesImg, fileName, "text/xml"); } using (UnityWebRequest webRequest = UnityWebRequest.Post(serverURL, form)) { yield return(webRequest.SendWebRequest()); while (!webRequest.isDone) { Debug.Log(webRequest.uploadProgress); yield return(new WaitForSeconds(0.5f)); } if (webRequest.error != null) { print("Error: " + webRequest.error); } else { if (webRequest.uploadProgress == 1 && webRequest.isDone) { Debug.Log("Returned text from PHP: \n" + webRequest.downloadHandler.text); } } try { MAP = int.Parse(webRequest.downloadHandler.text); } catch (Exception) { //print ("Number was not returned in www.text"); } i++; } } while (false);//MAP * i < imagesXML.Length); serverURL = GlobalData.serverAddress + "Test.php"; //Upload the truncated case preview for the main menu yield return(StartCoroutine(UploadMenuEntry())); //ShowConfirmation ("Upload finished!"); ds.ShowMessage("Upload finished!"); }