private static string GetThemeLocalizationPath(Path siteRoot, string themeName) { return(System.IO.Path.Combine( "Themes", themeName, "App_Data", "Localization", "{0}", "orchard.theme.po")); }
public static byte[] Zip(Path zipPaths, Func <Path, byte[]> zipPathToContent) { using (var result = new MemoryStream()) { ZipToStream(zipPaths, zipPathToContent, result); return(result.ToArray()); } }
public static VirtualFile FromStream(Stream contentStream) { int counter = 1; string baseName = "_temp_"; FPath basePath = new FPath(AppDomain.CurrentDomain.BaseDirectory); FPath uniquePath; do { uniquePath = basePath.Combine(baseName + counter + ".btw"); if (!uniquePath.Exists) { break; } if (counter > 100) { throw new TooManyVirtualFilesException(); } counter++; } while (true); using var fs = new FileStream(uniquePath.ToString(), FileMode.CreateNew); contentStream.CopyTo(fs); return(new VirtualFile(uniquePath.ToString())); }
private static Path GetThemeLocalizationPath(Path siteRoot, string themeName) { return(siteRoot.Combine( "Themes", themeName, "App_Data", "Localization", "en-US", "orchard.theme.po")); }
private static string GetModuleLocalizationPath(Path siteRoot, string moduleName) { return(System.IO.Path.Combine( "Modules", moduleName, "App_Data", "Localization", "{0}", "orchard.module.po")); }
/// <summary> /// Load Scriban templates and build HTML. /// </summary> /// <param name="layout">Header, footer and other layout fragments.</param> public HtmlBuilder Build(IDictionary <String, Document?> layout) { _logger.LogInformation($"Building {{h}} in {_project.PathTo(".").FullPathString()}/{{path}}...", "HTML", _outPath); var start = DateTime.Now; var inFile = _inPath.Combine("page.scriban-html").FullPath; var templateContext = new TemplateContext { TemplateLoader = new ScribanTemplateLoader(_inPath), MemberRenamer = _ => _.Name }; var json = new ScriptObject(); json.Import(typeof(Json), null, member => member.Name); var model = new ScriptObject { { "Project", this._project }, // { "HtmlConfigJson", JsonConvert.SerializeObject(config) }, { "Scripts", _jsBuilder.ScriptPaths }, { "Timestamp", DateTime.Now.Ticks }, { "Json", json }, { "MainMenu", layout["main_menu"] }, { "Footer", layout["footer"] }, }; templateContext.PushGlobal(model); var template = Template.Parse(File.ReadAllText(inFile)); var docs = this._project.Pages.Append( new Document { ProjectFilePath = "components/settings.md" } ); foreach (Document doc in docs) { var outFile = doc.ProjectFilePath.TrimSuffix(".md") + ".html"; Path outPath = _outPath.Combine(outFile); outPath.Parent().CreateDirectories(); this._logger.LogDebug("Rendering {page}...", outFile); model["ComponentName"] = doc.ProjectFilePath.StartsWith("components/") ? outFile.TrimPrefix("components/").TrimSuffix(".html") : ""; model["Document"] = doc; model["PathToRoot"] = "../".Repeat(doc.Depth).TrimSuffix("/"); var html = template.Render(templateContext); File.WriteAllText(outPath.ToString(), html); } var elapsed = (DateTime.Now - start).TotalSeconds; _logger.LogInformation( "{pages} HTML page(s), including layout files, built in {s:0.00} seconds.", this._project.Pages.Count + layout.Count, elapsed ); return(this); }
private static void SaveResults(MemoryStore store, Path file) { using (var writer = new RdfXmlWriter(file.FullPath)) writer.Write(store); Console.WriteLine("Wrote {0} statements.", store.StatementCount); }
private static Path GetModuleLocalizationPath(Path siteRoot, string moduleName) { return(siteRoot.Combine( "Modules", moduleName, "App_Data", "Localization", "en-US", "orchard.module.po")); }
/// <inheritdoc cref="JsBuilder"/> public JsBuilder(BuildArguments args, ILogger <JsBuilder> logger) { _args = args; _logger = logger; _inPath = Program.InPath.Combine("js"); _outPath = _args.ProjectRoot.Combine(Program.OutFolder); }
/// <summary> /// Scans the site for *.po files and imports them into database /// </summary> /// <param name="culture"></param> /// <returns></returns> public ActionResult ImportCurrentPos(bool?overwrite) { if (!Services.Authorizer.Authorize( Permissions.ImportExport, T("You are not allowed to upload translations."))) { return(new HttpUnauthorizedResult()); } List <StringEntry> strings = new List <StringEntry>(); var files = Directory.GetFiles(Server.MapPath("~"), "*.po", SearchOption.AllDirectories); foreach (var file in files) { var match = cultureRegex.Match(file); if (!match.Success || match.Groups.Count < 2) { Logger.Error("Importing current PO's; cannot find culture in path " + file); } else { string culture = match.Groups[1].Value; string path = new Fluent.IO.Path(file).MakeRelativeTo(new Fluent.IO.Path(Server.MapPath("~"))).ToString().Replace('\\', '/'); strings.AddRange(_localizationService.TranslateFile(path, System.IO.File.ReadAllText(file, Encoding.UTF8), culture)); } } _localizationService.SaveStringsToDatabase(strings, overwrite ?? false); Services.Notifier.Add(NotifyType.Information, T("Imported {0} translations from {1} *.po files", strings.Count, files.Count())); _localizationService.ResetCache(); return(RedirectToAction("Import")); }
public void SyncTranslation(string sitePath, string cultureCode) { Path.Get(sitePath) .Files("orchard.*.po", true) .Where(p => p.Parent().FileName.Equals("en-US", StringComparison.OrdinalIgnoreCase)) .ForEach(baselinePath => { var path = baselinePath.Parent().Parent().Combine(cultureCode, baselinePath.FileName); var translations = new List <StringEntry>(); if (path.Exists) { path.Open(inStream => ReadTranslations(inStream, translations), FileMode.Open, FileAccess.Read, FileShare.Read); } path.Parent().CreateDirectory(); path.Open(outStream => { var englishTranslations = new List <StringEntry>(); baselinePath.Open(baselineStream => ReadTranslations(baselineStream, englishTranslations), FileMode.Open, FileAccess.Read, FileShare.Read); using (var writer = new StreamWriter(outStream)) { foreach (var englishTranslation in englishTranslations) { var entry = englishTranslation; var translation = translations.Where( t => t.Context == entry.Context && t.Key == entry.Key).FirstOrDefault(); if (translation == default(StringEntry) || translation.Translation == null || translation.Translation.Equals(@"msgstr """"")) { writer.WriteLine("# Untranslated string"); } writer.WriteLine(entry.Context); writer.WriteLine(entry.Key); writer.WriteLine(entry.English); if (translation != null) { translation.Used = true; writer.WriteLine(translation.Translation); } else { writer.WriteLine("msgstr \"\""); } writer.WriteLine(); } foreach (var translation in translations.Where(t => !t.Used)) { writer.WriteLine("# Obsolete translation"); writer.WriteLine(translation.Context); writer.WriteLine(translation.Key); writer.WriteLine(translation.English); writer.WriteLine(translation.Translation); writer.WriteLine(); } } }, FileMode.Create, FileAccess.Write, FileShare.None); }); }
private static IEnumerable <StringEntry> DispatchResourceString( string corePoPath, string rootPoPath, Path sitePath, Path path, Path currentInputPath, string contents, string str) { var current = "~/" + path.MakeRelativeTo(currentInputPath).ToString().Replace('\\', '/'); // exclude items from the /obj/ directories, this is where packages reside if (!path.FullPath.Contains("\\obj\\")) { var context = current; if (path.Extension == ".cs") { var ns = NamespaceExpression.Match(contents).Groups[1].ToString(); var type = ClassExpression.Match(contents).Groups[1].ToString(); context = ns + "." + type; } string targetPath; if (current.StartsWith("~/core/", StringComparison.OrdinalIgnoreCase)) { targetPath = corePoPath; } else if (current.StartsWith("~/themes/", StringComparison.OrdinalIgnoreCase)) { targetPath = GetThemeLocalizationPath(sitePath, current.Substring(9, current.IndexOf('/', 9) - 9)).ToString(); } else if (current.StartsWith("~/modules/", StringComparison.OrdinalIgnoreCase)) { targetPath = GetModuleLocalizationPath(sitePath, current.Substring(10, current.IndexOf('/', 10) - 10)).ToString(); } else if (current.StartsWith("~/obj/", StringComparison.OrdinalIgnoreCase)) { targetPath = null; } else { targetPath = rootPoPath; } if (!string.IsNullOrEmpty(targetPath)) { yield return(new StringEntry { Culture = null, Context = context, Key = str, English = str, Translation = str, Path = targetPath.Replace('\\', '/') }); } } }
static void Main(string[] args) { var assemblyPath = typeof(Program).Assembly.Location; var assemblyDirectory = Path.Get(assemblyPath); _testPath = assemblyDirectory.Parent().Parent().Parent().Parent().Combine("Jint.Tests.Ecma"); _suitePath = _testPath; if (!_suitePath.Exists) { _suitePath.CreateDirectory(); } _classTemplate = File.ReadAllText("ClassTemplate.txt"); _methodTemplate = File.ReadAllText("MethodTemplate.txt"); foreach (var url in suites) { var content = new WebClient().DownloadString(url); dynamic suite = JsonConvert.DeserializeObject(content); var collection = suite.testsCollection; var tests = collection.tests; Console.Write("."); foreach (var test in tests) { byte[] data = Convert.FromBase64String((string)test.code); string decodedString = Encoding.UTF8.GetString(data); RenderTest( decodedString, (string)test.commentary, (string)test.description, (string)test.path, test.negative != null ); } } foreach (var category in categorizedTests.Keys) { var file = _testPath.Combine("Ecma").Combine(category + ".cs"); var methods = new StringBuilder(); foreach (var test in categorizedTests[category]) { methods.Append(test.Test); methods.AppendLine(); } var fileContent = _classTemplate; fileContent = fileContent.Replace("$$Methods$$", methods.ToString()); fileContent = fileContent.Replace("$$ClassName$$", GetClassName(category)); File.WriteAllText(file.FullPath, fileContent); } }
/// <inheritdoc cref="HtmlBuilder"/> public HtmlBuilder(Project project, ILogger <HtmlBuilder> logger, JsBuilder jsBuilder) { _project = project; _logger = logger; _jsBuilder = jsBuilder; _inPath = Program.InPath.Combine("scriban"); _outPath = _project.PathTo(Program.OutFolder); }
static void Main(string[] args) { var assemblyPath = typeof(Program).Assembly.Location; var assemblyDirectory = Path.Get(assemblyPath); _testPath = assemblyDirectory.Parent().Parent().Parent().Parent().Combine("Jint.Tests.Ecma"); _suitePath = _testPath; if (!_suitePath.Exists) { _suitePath.CreateDirectory(); } _classTemplate = File.ReadAllText("ClassTemplate.txt"); _methodTemplate = File.ReadAllText("MethodTemplate.txt"); foreach(var url in suites) { var content = new WebClient().DownloadString(url); dynamic suite = JsonConvert.DeserializeObject(content); var collection = suite.testsCollection; var tests = collection.tests; Console.Write("."); foreach (var test in tests) { byte[] data = Convert.FromBase64String((string)test.code); string decodedString = Encoding.UTF8.GetString(data); RenderTest( decodedString, (string)test.commentary, (string)test.description, (string)test.path, test.negative != null ); } } foreach (var category in categorizedTests.Keys) { var file = _testPath.Combine("Ecma").Combine(category + ".cs"); var methods = new StringBuilder(); foreach (var test in categorizedTests[category]) { methods.Append(test.Test); methods.AppendLine(); } var fileContent = _classTemplate; fileContent = fileContent.Replace("$$Methods$$", methods.ToString()); fileContent = fileContent.Replace("$$ClassName$$", GetClassName(category)); File.WriteAllText(file.FullPath, fileContent); } }
/// <summary> /// Zips all files in the path to the target. /// <remarks> /// When a directory is being pointed to as a source, all contents are recursively added. /// Individual files are added at the root, and directories are added at the root under their name. /// To have more control over the path of the files in the zip, use the overload. /// </remarks> /// </summary> /// <param name="path">The files to compress.</param> /// <param name="target"> /// The path of the target zip file. /// If target has more than one file, only the first one is used. /// </param> /// <returns>The zipped path.</returns> public static Path Zip(this Path path, Path target) { var files = path.AllFiles() .ToDictionary( p => p.MakeRelativeTo(path), p => p); Zip(target, new Path(files.Keys), p => files[p].ReadBytes()); return(target); }
/// <summary> /// Zips all files in the path to the target. /// </summary> /// <param name="path">The files to compress.</param> /// <param name="target"> /// The path of the target zip file. /// If target has more than one file, only the first one is used.</param> /// <param name="fileSystemToZip"> /// A function that maps the paths of the files and directories to zip into relative paths inside the zip. /// </param> /// <returns>The zipped path.</returns> public static Path Zip(this Path path, Path target, Func <Path, Path> fileSystemToZip) { var files = path.AllFiles() .ToDictionary( fileSystemToZip, p => p); Zip(target, new Path(files.Keys), p => files[p].ReadBytes()); return(target); }
public byte[] ExtractDefaultTranslation(string sitePath) { var site = Path.Get(sitePath); var corePoPath = site.Combine( "Core", "App_Data", "Localization", "en-US", "orchard.core.po"); var rootPoPath = site.Combine( "App_Data", "Localization", "en-US", "orchard.root.po"); var zipFiles = new Dictionary <Path, StringBuilder>(); // Extract resources for module manifests site.Files("module.txt", true) .Read((content, path) => { var moduleName = path.Parent().FileName; var poPath = path.MakeRelativeTo(sitePath).Tokens[0].Equals("core", StringComparison.OrdinalIgnoreCase) ? corePoPath : GetModuleLocalizationPath(site, moduleName); ExtractPoFromManifest(zipFiles, poPath, content, path, site); }); // Extract resources for theme manifests site.Files("theme.txt", true) .Read((content, path) => { var themeName = path.Parent().FileName; var poPath = GetThemeLocalizationPath(site, themeName); ExtractPoFromManifest(zipFiles, poPath, content, path, site); }); // Extract resources from views and cs files, for the web site // as well as for the framework and Azure projects. site.Add(site.Parent().Combine("Orchard")) .Add(site.Parent().Combine("Orchard.Azure")) .ForEach(p => p.Files("*", true) .WhereExtensionIs(".cshtml", ".aspx", ".ascx", ".cs") .Grep( ResourceStringExpression, (path, match, contents) => { var str = match.Groups[1].ToString(); DispatchResourceString(zipFiles, corePoPath, rootPoPath, site, path, p, contents, str); } ) .Grep( PluralStringExpression, (path, match, contents) => { var str = match.Groups[1].ToString(); DispatchResourceString(zipFiles, corePoPath, rootPoPath, site, path, p, contents, str); str = match.Groups[6].ToString(); DispatchResourceString(zipFiles, corePoPath, rootPoPath, site, path, p, contents, str); } )); return(ZipExtensions.Zip( new Path(zipFiles.Keys.Select(p => p.MakeRelativeTo(site))), p => Encoding.UTF8.GetBytes(zipFiles[site.Combine(p)].ToString()))); }
private void ExtractResourcesFromCode(string contents, Path corePoPath, Path rootPoPath, Path site, Path path, Dictionary <Path, StringEntryBuilder> zipFiles) { foreach (var str in FindLocalizedStrings(contents)) { DispatchResourceString(zipFiles, corePoPath, rootPoPath, site, path, site, contents, '"' + str + '"'); } foreach (var str in FindLocalizedStrings(contents, "T.Plural(", true)) { DispatchResourceString(zipFiles, corePoPath, rootPoPath, site, path, site, contents, '"' + str + '"'); } }
private static MemoryStore LoadResults(Path file) { var store = new MemoryStore(); using (var reader = new RdfXmlReader(file.FullPath)) reader.Select(store); Console.WriteLine("Loaded {0} statements.", store.StatementCount); return(store); }
public static void ZipToStream(Path zipPaths, Func <Path, byte[]> zipPathToContent, Stream output) { using var zipArchive = new ZipArchive(output, ZipArchiveMode.Create); foreach (Path path in zipPaths) { ZipArchiveEntry entry = zipArchive.CreateEntry(path.First().ToString(), CompressionLevel.Optimal); Stream writer = entry.Open(); writer.Write(zipPathToContent(path)); writer.Close(); } }
private static async void Resize(Path jpegImagePath, Path createDirectory, int size, string filename, MemoryStream original) { var result = Resizer.LowJpeg(original, size, size).ToArray(); using (var sourceStream = File.Open(createDirectory.Combine(filename).FullPath, FileMode.Create)) { sourceStream.Seek(0, SeekOrigin.End); await sourceStream.WriteAsync(result, 0, result.Length); } Console.WriteLine("Resized {0}: {1}", size, jpegImagePath.FileName); }
private static Path GetResourceCache(Statement statement, Path cache) { var name = Path.Get(statement.Subject.Uri).FileName.ToLower(); var extension = Path.Get(statement.Object.Uri).Extension.ToLower(); if (extension == "" || extension.StartsWith("._")) { extension = ".html"; } return(cache.Combine(name + extension)); }
public byte[] PackageTranslations(string cultureCode, string sitePath) { var site = Path.Get(sitePath); var translationFiles = site .Files("orchard.*.po", true) .Where(p => p.Parent().FileName.Equals(cultureCode, StringComparison.OrdinalIgnoreCase)) .MakeRelativeTo(site); return(ZipExtensions.Zip( translationFiles, p => site.Combine(p).ReadBytes())); }
public byte[] ExtractDefaultTranslation(string sitePath, IEnumerable <string> extensionNames) { if (extensionNames == null || extensionNames.Count() == 0) { return(ExtractDefaultTranslation(sitePath)); } var site = Path.Get(sitePath); var zipFiles = new Dictionary <Path, StringBuilder>(); // Extract resources for module manifests site.Files("module.txt", true) .Where(p => extensionNames.Contains(p.Parent().FileName)) .Read((content, path) => { var moduleName = path.Parent().FileName; var poPath = GetModuleLocalizationPath(site, moduleName); ExtractPoFromManifest(zipFiles, poPath, content, path, site); }); // Extract resources for theme manifests site.Files("theme.txt", true) .Where(p => extensionNames.Contains(p.Parent().FileName)) .Read((content, path) => { var themeName = path.Parent().FileName; var poPath = GetThemeLocalizationPath(site, themeName); ExtractPoFromManifest(zipFiles, poPath, content, path, site); }); // Extract resources from views and cs files site.Files("*", true) .WhereExtensionIs(".cshtml", ".aspx", ".ascx", ".cs") .Where(p => { var tokens = p.MakeRelativeTo(site).Tokens; return(new[] { "themes", "modules" }.Contains(tokens[0]) && extensionNames.Contains(tokens[1])); }) .Grep( ResourceStringExpression, (path, match, contents) => { var str = match.Groups[1].ToString(); DispatchResourceString(zipFiles, null, null, site, path, site, contents, str); } ) .Grep( PluralStringExpression, (path, match, contents) => { var str = match.Groups[1].ToString(); DispatchResourceString(zipFiles, null, null, site, path, site, contents, str); str = match.Groups[6].ToString(); DispatchResourceString(zipFiles, null, null, site, path, site, contents, str); } ); return(ZipExtensions.Zip( new Path(zipFiles.Keys.Select(p => p.MakeRelativeTo(site))), p => Encoding.UTF8.GetBytes(zipFiles[site.Combine(p)].ToString()))); }
private bool ShouldIgnore(string actualPath, Path drivePath, USNChangeRange range, NtfsUsnJournal journal) { if (actualPath == "Unavailable") { return(true); } string relativePath; try { Uri drivePathUri = new Uri(drivePath.FullPath, UriKind.Absolute); Uri actualPathUri = new Uri(actualPath, UriKind.Absolute); relativePath = Uri.UnescapeDataString(drivePathUri.MakeRelativeUri(actualPathUri).ToString()); } catch (Exception e) { relativePath = "#ERROR#"; } if (relativePath == "#ERROR#" || relativePath.StartsWith("System Volume Information") || relativePath.StartsWith("$RECYCLE.BIN")) { return(true); } string renameFromRelativePath = ""; if (range.RenameFrom != null) { string renameFromPath = GetActualPath(journal, range.RenameFrom); try { Uri drivePathUri = new Uri(drivePath.FullPath, UriKind.Absolute); Uri actualPathUri = new Uri(renameFromPath, UriKind.Absolute); renameFromRelativePath = Uri.UnescapeDataString(drivePathUri.MakeRelativeUri(actualPathUri).ToString()); } catch (Exception e) { renameFromRelativePath = ""; } } if (!String.IsNullOrWhiteSpace(renameFromRelativePath) && renameFromRelativePath.StartsWith(".proximaTemp")) { return(true); } return(false); }
public byte[] PackageTranslations(string cultureCode, string sitePath, IEnumerable <string> extensionNames) { var site = Path.Get(sitePath); var translationFiles = site .Files("orchard.*.po", true) .Where(p => p.Parent().FileName.Equals(cultureCode, StringComparison.OrdinalIgnoreCase) && (extensionNames.Contains(p.MakeRelativeTo(site.Combine("Modules")).Tokens[0]) || extensionNames.Contains(p.MakeRelativeTo(site.Combine("Themes")).Tokens[0]))) .MakeRelativeTo(site); return(ZipExtensions.Zip( translationFiles, p => site.Combine(p).ReadBytes())); }
private static string GetEntityName(MemoryStore store, Uri uri) { var name = store.Select(new Statement(uri.AbsoluteUri, foaf + "name", null)) .Select(statement => statement.Object) .Cast <Literal>() .Select(literal => literal.Value) .FirstOrDefault(); if (name == null) { name = Path.Get(uri.AbsolutePath).FileName; } return(name); }
public static void ZipToStream(Path zipPaths, Func <Path, byte[]> zipPathToContent, Stream output) { using (var zipStream = new ZipOutputStream(output)) { zipStream.CompressionLevel = CompressionLevel.BestCompression; foreach (var path in zipPaths) { var buffer = zipPathToContent(path); var entry = zipStream.PutNextEntry(path.ToString()); entry.CreationTime = DateTime.Now; zipStream.Write(buffer, 0, buffer.Length); } zipStream.Flush(); zipStream.Close(); } }
private static void ParseFiles(MemoryStore store, Path cache) { Console.WriteLine("Searching cross-references..."); foreach (var statement in store.Select(new Statement(null, foaf + "page", null)).OrderBy(statement => statement.Subject.Uri)) { var file = GetResourceCache(statement, cache); if (file.Exists) { Console.WriteLine("\t{0}", GetEntityName(store, new Uri(statement.Subject.Uri))); var document = new HtmlDocument(); document.Load(file.FullPath); foreach (var node in document.DocumentNode.SelectNodes("//div[@id='bodyContent']/p//a[@href]")) { var uri = new Uri(new Uri(statement.Object.Uri), node.Attributes["href"].Value); var tag = GetBeforeWords(node, 4).Concat(new[] { node.InnerText }).Concat(GetAfterWords(node, 4)); var predicate = StripNonAscii(string.Join("-", tag)); foreach (var stop in new[] { "&", "#", ";", ":", ",", ".", " ", "(", ")", "[", "]", "\"", "'", "$" }) { predicate = predicate.Replace(stop, ""); } predicate = myd + predicate; var entity = store.SelectSingle(new Statement(null, foaf + "page", new Entity(uri.AbsoluteUri))); if (entity.HasValue) { if (entity.Value.Subject != statement.Subject) { Console.WriteLine("\t\t[{0}] {1} = {2}", predicate, node.InnerText, uri.AbsoluteUri); store.Add(new Statement(statement.Subject, new Entity(predicate), entity.Value.Subject)); } } else { //Console.WriteLine("\t\t[{0}] {1} = {2}", predicate, node.InnerText, uri.AbsoluteUri); //store.Add(new Statement(statement.Subject, new Entity(predicate), new Entity(uri.AbsoluteUri))); } } } } }
private static void DownloadResource(WebClient client, Statement statement, Path path) { var file = GetResourceCache(statement, path); if (!file.Exists) { Console.WriteLine("Downloading {0}...", statement.Object.Uri); try { client.DownloadFile(statement.Object.Uri, file.FullPath); } catch (Exception exception) { Console.WriteLine(exception.Message); } } }
private static void CreateThumbnails(Path jpegImagePath, Path eaDir) { using (var original = new MemoryStream(File.ReadAllBytes(jpegImagePath.FullPath))) { Console.WriteLine("Resizing 120: {0}", jpegImagePath.FileName); Resize(jpegImagePath, eaDir.Combine(jpegImagePath.FileName).CreateDirectory(), 120, "SYNOPHOTO-THUMB_S.jpg", original); original.Position = 0; Console.WriteLine("Resizing 320: {0}", jpegImagePath.FileName); Resize(jpegImagePath, eaDir.Combine(jpegImagePath.FileName).CreateDirectory(), 320, "SYNOPHOTO-THUMB_M.jpg", original); original.Position = 0; Console.WriteLine("Resizing 640: {0}", jpegImagePath.FileName); Resize(jpegImagePath, eaDir.Combine(jpegImagePath.FileName).CreateDirectory(), 640, "SYNOPHOTO-THUMB_B.jpg", original); original.Position = 0; Console.WriteLine("Resizing 800: {0}", jpegImagePath.FileName); Resize(jpegImagePath, eaDir.Combine(jpegImagePath.FileName).CreateDirectory(), 800, "SYNOPHOTO-THUMB_L.jpg", original); } }
/// <summary> /// Zips all files in the path to the target. /// </summary> /// <param name="path">The files to compress.</param> /// <param name="target"> /// The path of the target zip file. /// If target has more than one file, only the first one is used.</param> /// <param name="fileSystemToZip"> /// A function that maps the paths of the files and directories to zip into relative paths inside the zip. /// </param> /// <returns>The zipped path.</returns> public static Path Zip(this Path path, Path target, Func<Path, Path> fileSystemToZip) { var files = path.AllFiles() .ToDictionary( fileSystemToZip, p => p); Zip(target, new Path(files.Keys), p => files[p].ReadBytes()); return target; }
public static byte[] Zip(Path zipPaths, Func<Path, byte[]> zipPathToContent) { using (var result = new MemoryStream()) { ZipToStream(zipPaths, zipPathToContent, result); return result.ToArray(); } }
/// <summary> /// Zips dynamically created contents. /// </summary> /// <param name="target"> /// The path of the target zip file. /// If target has more than one file, only the first one is used.</param> /// <param name="zipPaths">The zipped paths of the files to zip.</param> /// <param name="zipPathToContent"> /// A function that maps the zipped paths to the binary content of the file to zip. /// </param> /// <returns>The zipped path.</returns> public static Path Zip(this Path target, Path zipPaths, Func<Path, byte[]> zipPathToContent) { target.Open(s => ZipToStream(zipPaths, p => p.ReadBytes(), s), FileMode.Create, FileAccess.ReadWrite, FileShare.None); return target; }
/// <summary> /// Scans the site for *.po files and imports them into database /// </summary> /// <param name="culture"></param> /// <returns></returns> public ActionResult ImportCurrentPos(bool? overwrite) { if (!Services.Authorizer.Authorize( Permissions.ImportExport, T("You are not allowed to upload translations."))) return new HttpUnauthorizedResult(); List<StringEntry> strings = new List<StringEntry>(); var files = Directory.GetFiles(Server.MapPath("~"), "*.po", SearchOption.AllDirectories); foreach (var file in files) { var match = cultureRegex.Match(file); if (!match.Success || match.Groups.Count < 2) { Logger.Error("Importing current PO's; cannot find culture in path " + file); } else { string culture = match.Groups[1].Value; string path = new Fluent.IO.Path(file).MakeRelativeTo(new Fluent.IO.Path(Server.MapPath("~"))).ToString().Replace('\\', '/'); strings.AddRange(_localizationService.TranslateFile(path, System.IO.File.ReadAllText(file, Encoding.UTF8), culture)); } } _localizationService.SaveStringsToDatabase(strings, overwrite ?? false); Services.Notifier.Add(NotifyType.Information, T("Imported {0} translations from {1} *.po files", strings.Count, files.Count())); _localizationService.ResetCache(); return RedirectToAction("Import"); }
private static Path GetThemeLocalizationPath(Path siteRoot, string themeName) { return siteRoot.Combine( "Themes", themeName, "App_Data", "Localization", "en-US", "orchard.theme.po"); }
private static Path GetModuleLocalizationPath(Path siteRoot, string moduleName) { return siteRoot.Combine( "Modules", moduleName, "App_Data", "Localization", "en-US", "orchard.module.po"); }
private static void DispatchResourceString( IDictionary<Path, StringBuilder> fileCatalog, Path corePoPath, Path rootPoPath, Path sitePath, Path path, Path currentInputPath, string contents, string str) { var current = "~/" + path.MakeRelativeTo(currentInputPath).ToString().Replace('\\', '/'); var context = current; if (path.Extension == ".cs") { var ns = NamespaceExpression.Match(contents).Groups[1].ToString(); var type = ClassExpression.Match(contents).Groups[1].ToString(); context = ns + "." + type; } Path targetPath; if (current.StartsWith("~/core/", StringComparison.OrdinalIgnoreCase)) { targetPath = corePoPath; } else if (current.StartsWith("~/themes/", StringComparison.OrdinalIgnoreCase)) { targetPath = GetThemeLocalizationPath(sitePath, current.Substring(9, current.IndexOf('/', 9) - 9)); } else if (current.StartsWith("~/modules/", StringComparison.OrdinalIgnoreCase)) { targetPath = GetModuleLocalizationPath(sitePath, current.Substring(10, current.IndexOf('/', 10) - 10)); } else { targetPath = rootPoPath; } WriteResourceString(GetBuilder(fileCatalog, targetPath), context, str); }
private IEnumerable<StringEntry> DispatchResourceString( string corePoPath, string rootPoPath, Path sitePath, Path path, Path currentInputPath, string contents, string str) { var current = "~/" + path.MakeRelativeTo(currentInputPath).ToString().Replace('\\', '/'); // exclude items from the /obj/ directories, this is where packages reside if (!path.FullPath.Contains("\\obj\\")) { var context = current; if (path.Extension == ".cs") { var ns = NamespaceExpression.Match(contents).Groups[1].ToString(); var type = ClassExpression.Match(contents).Groups[1].ToString(); context = ns + "." + type; } string targetPath = null; if (current.StartsWith("~/core/", StringComparison.OrdinalIgnoreCase)) { targetPath = corePoPath; } else if (current.StartsWith("~/themes/", StringComparison.OrdinalIgnoreCase)) { try { targetPath = GetThemeLocalizationPath(sitePath, current.Substring(9, current.IndexOf('/', 9) - 9)).ToString(); } catch (ArgumentOutOfRangeException ex) { Logger.Error(ex, "Error substinging {0}, skipping string {1}!", current, str); } } else if (current.StartsWith("~/modules/", StringComparison.OrdinalIgnoreCase)) { try { targetPath = GetModuleLocalizationPath(sitePath, current.Substring(10, current.IndexOf('/', 10) - 10)).ToString(); } catch (ArgumentOutOfRangeException ex) { Logger.Error(ex, "Error substinging {0}, skipping string {1}!", current, str); } } else if (current.StartsWith("~/obj/", StringComparison.OrdinalIgnoreCase)) { targetPath = null; } else { targetPath = rootPoPath; } if (!string.IsNullOrEmpty(targetPath)) { yield return new StringEntry { Culture = null, Context = context, Key = str, English = str, Translation = str, Path = targetPath.Replace('\\', '/') }; } } }
private void ExtractResourcesFromCode(string contents, Path corePoPath, Path rootPoPath, Path site, Path path, Dictionary<Path, StringBuilder> zipFiles) { foreach (var str in FindLocalizedStrings(contents)) { DispatchResourceString(zipFiles, corePoPath, rootPoPath, site, path, site, contents, '"' + str + '"'); } foreach (var str in FindLocalizedStrings(contents, "T.Plural(", true)) { DispatchResourceString(zipFiles, corePoPath, rootPoPath, site, path, site, contents, '"' + str + '"'); } }
private static StringBuilder GetBuilder(IDictionary<Path, StringBuilder> fileCatalog, Path path) { StringBuilder entry; if (!fileCatalog.ContainsKey(path)) { entry = new StringBuilder(); fileCatalog.Add(path, entry); } else { entry = fileCatalog[path]; } return entry; }
/// <summary> /// Unzips all files in the path. /// </summary> /// <param name="path">The zip files.</param> /// <param name="target">The directory where the files must be unzipped.</param> /// <returns>The uncompressed files and folders.</returns> public static Path Unzip(this Path path, Path target) { Unzip(path, (p, ba) => target.Combine(p).Write(ba)); return target; }
private static string GetThemeLocalizationPath(Path siteRoot, string themeName) { return System.IO.Path.Combine( "Themes", themeName, "App_Data", "Localization", "{0}", "orchard.theme.po"); }
/// <summary> /// /// </summary> /// <param name="poPath">PO file that the translation would be in traditional po setup</param> /// <param name="manifest">content of the manifest file</param> /// <param name="manifestPath">path to the manifest file</param> /// <param name="rootPath">path to the root of the site</param> /// <returns></returns> private static IEnumerable<StringEntry> ExtractPoFromManifest( string poPath, string manifest, Path manifestPath, Path rootPath) { if (!manifestPath.FullPath.Contains("\\obj\\")) { var context = "~/" + manifestPath.MakeRelativeTo(rootPath).ToString() .Replace('\\', '/'); var reader = new StringReader(manifest); string line; while ((line = reader.ReadLine()) != null) { var split = line.Split(new[] { ':' }, 2, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim()).ToArray(); if (split.Length == 2) { var key = split[0]; if (new[] { "Name", "Description", "Author", "Website", "Tags" }.Contains(key)) { var value = split[1]; yield return new StringEntry { Culture = null, Context = context, Key = key, English = value, Translation = value, Path = poPath.Replace('\\', '/') }; } } if (line.StartsWith("Features:")) { var feature = ""; while ((line = reader.ReadLine()) != null) { var match = FeatureNameExpression.Match(line); if (match.Success) { feature = match.Groups[1].Value; continue; } split = line.Split(new[] { ':' }, 2, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim()).ToArray(); if (split.Length != 2) continue; var key = split[0]; if (new[] { "Name", "Description", "Category" }.Contains(key)) { var value = split[1]; yield return new StringEntry { Culture = null, Context = context, Key = feature + "." + key, English = value, Translation = value, Path = poPath.Replace('\\', '/') }; } } } } } }
public static void ZipToStream(Path zipPaths, Func<Path, byte[]> zipPathToContent, Stream output) { using (var zipStream = new ZipOutputStream(output)) { zipStream.CompressionLevel = CompressionLevel.BestCompression; foreach (var path in zipPaths) { var buffer = zipPathToContent(path); var entry = zipStream.PutNextEntry(path.ToString()); entry.CreationTime = DateTime.Now; zipStream.Write(buffer, 0, buffer.Length); } zipStream.Flush(); zipStream.Close(); } }
private static void ExtractPoFromManifest( IDictionary<Path, StringBuilder> fileCatalog, Path poPath, string manifest, Path manifestPath, Path rootPath) { var context = "~/" + manifestPath.MakeRelativeTo(rootPath).ToString() .Replace('\\', '/'); var reader = new StringReader(manifest); var builder = GetBuilder(fileCatalog, poPath); string line; while ((line = reader.ReadLine()) != null) { var split = line.Split(new[] {':'}, 2, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim()).ToArray(); if (split.Length == 2) { var key = split[0]; if (new[] {"Name", "Description", "Author", "Website", "Tags"}.Contains(key)) { var value = split[1]; WriteResourceString( builder, context, '"' + key + '"', '"' + value + '"'); } } if (line.StartsWith("Features:")) { var feature = ""; while ((line = reader.ReadLine()) != null) { var match = FeatureNameExpression.Match(line); if (match.Success) { feature = match.Groups[1].Value; continue; } split = line.Split(new[] { ':' }, 2, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim()).ToArray(); if (split.Length != 2) continue; var key = split[0]; if (new[] { "Name", "Description", "Category" }.Contains(key)) { var value = split[1]; WriteResourceString( builder, context, '"' + feature + '.' + key + "\"", '"' + value + '"'); } } } } }
private static string GetModuleLocalizationPath(Path siteRoot, string moduleName) { return System.IO.Path.Combine( "Modules", moduleName, "App_Data", "Localization", "{0}", "orchard.module.po"); }
/// <summary> /// Zips all files in the path to the target. /// <remarks> /// When a directory is being pointed to as a source, all contents are recursively added. /// Individual files are added at the root, and directories are added at the root under their name. /// To have more control over the path of the files in the zip, use the overload. /// </remarks> /// </summary> /// <param name="path">The files to compress.</param> /// <param name="target"> /// The path of the target zip file. /// If target has more than one file, only the first one is used. /// </param> /// <returns>The zipped path.</returns> public static Path Zip(this Path path, Path target) { var files = path.AllFiles() .ToDictionary( p => p.MakeRelativeTo(path), p => p); Zip(target, new Path(files.Keys), p => files[p].ReadBytes()); return target; }