Esempio n. 1
0
        public static bool HandleRequest(string localPath, IRequestInfo info, CollectionSettings currentCollectionSettings)
        {
            var lastSep     = localPath.IndexOf("/", System.StringComparison.Ordinal);
            var lastSegment = (lastSep > -1) ? localPath.Substring(lastSep + 1) : localPath;

            switch (lastSegment)
            {
            case "loadStrings":

                while (_localizing)
                {
                    Thread.Sleep(0);
                }

                try
                {
                    _localizing = true;

                    var d    = new Dictionary <string, string>();
                    var post = info.GetPostData();

                    foreach (string key in post.Keys)
                    {
                        var translation = LocalizationManager.GetDynamicString("Bloom", key, post[key]);
                        if (!d.ContainsKey(key))
                        {
                            d.Add(key, translation);
                        }
                    }

                    info.ContentType = "application/json";
                    info.WriteCompleteOutput(JsonConvert.SerializeObject(d));
                    return(true);
                }
                finally
                {
                    _localizing = false;
                }
            }

            return(false);
        }
        public static bool HandleRequest(string localPath, IRequestInfo info, CollectionSettings currentCollectionSettings)
        {
            var lastSep     = localPath.IndexOf("/", StringComparison.Ordinal);
            var lastSegment = (lastSep > -1) ? localPath.Substring(lastSep + 1) : localPath;

            switch (lastSegment)
            {
            case "loadReaderToolSettings":
                info.ContentType = "application/json";
                info.WriteCompleteOutput(GetDefaultReaderSettings(currentCollectionSettings));
                return(true);

            case "saveReaderToolSettings":
                var path    = currentCollectionSettings.DecodableLevelPathName;
                var content = info.GetPostData()["data"];
                File.WriteAllText(path, content, Encoding.UTF8);
                info.ContentType = "text/plain";
                info.WriteCompleteOutput("OK");
                return(true);

            case "getDefaultFont":
                var bookFontName = currentCollectionSettings.DefaultLanguage1FontName;
                if (string.IsNullOrEmpty(bookFontName))
                {
                    bookFontName = "sans-serif";
                }
                info.ContentType = "text/plain";
                info.WriteCompleteOutput(bookFontName);
                return(true);

            case "getSampleTextsList":
                info.ContentType = "text/plain";
                info.WriteCompleteOutput(GetSampleTextsList(currentCollectionSettings.SettingsFilePath));
                return(true);

            case "getSampleFileContents":
                var fileName = info.GetQueryString()["data"];
                info.ContentType = "text/plain";
                info.WriteCompleteOutput(GetSampleFileContents(fileName, currentCollectionSettings.SettingsFilePath));
                return(true);

            case "getTextOfPages":
                info.ContentType = "text/plain";
                info.WriteCompleteOutput(GetTextOfPages());
                return(true);

            case "saveReaderToolsWords":
                info.ContentType = "text/plain";
                info.WriteCompleteOutput(SaveReaderToolsWordsFile(info.GetPostData()["data"]));
                return(true);

            case "makeLetterAndWordList":
                MakeLetterAndWordList(info.GetPostData()["settings"], info.GetPostData()["allWords"]);
                info.ContentType = "text/plain";
                info.WriteCompleteOutput("OK");
                return(true);

            case "openTextsFolder":
                OpenTextsFolder();
                info.ContentType = "text/plain";
                info.WriteCompleteOutput("OK");
                return(true);
            }

            return(false);
        }
		// Every path should return false or send a response.
		// Otherwise we can get a timeout error as the browser waits for a response.
		protected override bool ProcessRequest(IRequestInfo info)
		{
			if (base.ProcessRequest(info))
				return true;

			var localPath = GetLocalPathWithoutQuery(info);

			// routing
			if (localPath.StartsWith("readers/"))
			{
				if (ReadersHandler.HandleRequest(localPath, info, CurrentCollectionSettings))
					return true;
			}
			else if (localPath.StartsWith("i18n/"))
			{
				if (I18NHandler.HandleRequest(localPath, info, CurrentCollectionSettings))
					return true;
			}
			else if (localPath.StartsWith("directoryWatcher/"))
			{
				var dirName = info.GetPostData()["dir"];

				if (dirName == "Sample Texts")
				{
					if (CheckForSampleTextChanges(info))
						return true;
				}

				return false;
			}
			else if (localPath.StartsWith("leveledRTInfo/"))
			{
				var queryPart = string.Empty;
				if (info.RawUrl.Contains("?"))
					queryPart = "#" + info.RawUrl.Split('?')[1];
				var langCode = LocalizationManager.UILanguageId;
				var completeEnglishPath = FileLocator.GetFileDistributedWithApplication(localPath);
				var completeUiLangPath = GetUiLanguageFileVersion(completeEnglishPath, langCode);
				string url;
				if (langCode != "en" && File.Exists(completeUiLangPath))
					url = completeUiLangPath;
				else
					url = completeEnglishPath;
				var cleanUrl = url.Replace("\\", "/"); // allows jump to file to work

				string browser = string.Empty;
				if (Palaso.PlatformUtilities.Platform.IsLinux)
				{
					// REVIEW: This opens HTML files in the browser. Do we have any non-html
					// files that this code needs to open in the browser? Currently they get
					// opened in whatever application the user has selected for that file type
					// which might well be an editor.
					browser = "xdg-open";
				}
				else
				{
					// If we don't provide the path of the browser, i.e. Process.Start(url + queryPart), we get file not found exception.
					// If we prepend "file:///", the anchor part of the link (#xxx) is not sent unless we provide the browser path too.
					// This is the same behavior when simply typing a url into the Run command on Windows.
					// If we fail to get the browser path for some reason, we still load the page, just without navigating to the anchor.
					string defaultBrowserPath;
					if (TryGetDefaultBrowserPath(out defaultBrowserPath))
					{
						browser = defaultBrowserPath;
					}
				}

				if (!string.IsNullOrEmpty(browser))
				{
					try
					{
						Process.Start(browser, "\"file:///" + cleanUrl + queryPart + "\"");
						return false;
					}
					catch (Exception)
					{
						Debug.Fail("Jumping to browser with anchor failed.");
						// Don't crash Bloom because we can't open an external file.
					}
				}

				// If the above failed, either for lack of default browser or exception, try this:
				Process.Start("\"" + cleanUrl + "\"");
				return false;
			}

			switch (localPath)
			{
				case "currentPageContent":
					info.ContentType = "text/html";
					info.WriteCompleteOutput(CurrentPageContent ?? "");
					return true;

				case "accordionContent":
					info.ContentType = "text/html";
					info.WriteCompleteOutput(AccordionContent ?? "");
					return true;

				case "availableFontNames":
					InstalledFontCollection installedFontCollection = new InstalledFontCollection();
					info.WriteCompleteOutput(string.Join(",", installedFontCollection.Families.Select(f => f.Name)));
					return true;

				case "help":
					var post = info.GetPostData();
					HelpLauncher.Show(null, post["data"]);
					return true;

				case "getNextBookStyle":
					info.ContentType = "text/html";
					info.WriteCompleteOutput(CurrentBook.NextStyleNumber.ToString(CultureInfo.InvariantCulture));
					return true;
			}

			string path = null;
			try
			{
				path = FileLocator.GetFileDistributedWithApplication("BloomBrowserUI", localPath);
			}
			catch (ApplicationException)
			{
				// ignore
			}

			if (!File.Exists(path)) return false;

			info.ContentType = GetContentType(Path.GetExtension(localPath));
			info.ReplyWithFileContent(path);
			return true;
		}