public Workspace(IOptions <WorkspaceOption> option, IOptions <DB> db, LocalBlogService localBlogService, IHttpClientFactory httpClientFactory, ILogger <Workspace> logger) { HttpClientFactory = httpClientFactory; Logger = logger; Remote = new FileSystemBlogService( new PhysicalFileProvider(Environment.CurrentDirectory).AsFileProvider()); Local = localBlogService; Option = option.Value; DB = db.Value; }
public async Task Setup() { RootPath = Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "fstest"); FSStaticBuilder.EnsureDirectoryExists(RootPath); PostData = Enumerable.Range(0, 100).Select(x => Generator.GetPost()).ToArray(); LayoutData = Enumerable.Range(0, 100).Select(x => Generator.GetLayout()).ToArray(); PageData = Enumerable.Range(0, 100).Select(x => Generator.GetPage()).ToArray(); FileData = Enumerable.Range(0, 100).Select(x => Generator.GetFile()).ToArray(); BlogOptionsData = new BlogOptions(); BlogBuilder builder = new BlogBuilder(BlogOptionsData, RootPath); await builder.Build(); await builder.BuildPosts(PostData); await builder.BuildLayouts(LayoutData); await builder.BuildPages(PageData); await builder.BuildFiles(FileData); BlogService = new FileSystemBlogService(new PhysicalFileProvider(RootPath).AsFileProvider()); }
public async Task Connect(string name = "") { if (string.IsNullOrEmpty(name)) { name = Option.CurrentRemote; } Logger.LogInformation($"Connect to remote {name}."); if (Option.Remotes.TryGetValue(name, out var remote)) { Logger.LogInformation($"Detect remote {remote.Name} ({Enum.GetName(typeof(RemoteType), remote.Type)})."); switch (remote.Type) { case RemoteType.LocalFS: Remote = new FileSystemBlogService( new PhysicalFileProvider(remote.Uri).AsFileProvider()); break; case RemoteType.RemoteFS: { var client = HttpClientFactory.CreateClient(); client.BaseAddress = new Uri(remote.Uri); Remote = new FileSystemBlogService( new HttpFileProvider(client)); } break; case RemoteType.Api: { var client = HttpClientFactory.CreateClient(); client.BaseAddress = new Uri(remote.Uri); Remote = new ApiBlogService(client); } break; case RemoteType.Git: { FSBuilder builder = new FSBuilder(Environment.CurrentDirectory); Logger.LogInformation("Pull git repository."); try { using var repo = new Repository(GitTempFolder); // Credential information to fetch LibGit2Sharp.PullOptions options = new LibGit2Sharp.PullOptions(); // User information to create a merge commit var signature = new LibGit2Sharp.Signature( new Identity("AcBlog.Tools.Sdk", "tools.sdk@acblog"), DateTimeOffset.Now); // Pull LibGit2Sharp.Commands.Pull(repo, signature, options); } catch { builder.EnsureDirectoryEmpty(GitTempFolder); Repository.Clone(remote.Uri, GitTempFolder); } Remote = new FileSystemBlogService( new PhysicalFileProvider(Path.Join(Environment.CurrentDirectory, GitTempFolder)).AsFileProvider()); } break; } Remote.PostService.Context.Token = remote.Token; Option.CurrentRemote = name; await SaveOption(); } else { throw new Exception("No remote"); } }
public ClientBlogService(IOptions <ServerSettings> serverOptions, IHttpClientFactory httpClientFactory) { var server = serverOptions.Value; { var client = httpClientFactory.CreateClient(); /*client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue * { * NoCache = true * };*/ if (string.IsNullOrEmpty(server.Main.Uri)) { client.BaseAddress = new Uri($"{server.BaseAddress.TrimEnd('/')}/data/"); Main = new FileSystemBlogService(new HttpFileProvider(client)); } else if (server.Main.IsStatic) { if (!server.Main.Uri.EndsWith("/")) { server.Main.Uri += "/"; } client.BaseAddress = new Uri(server.Main.Uri); Main = new FileSystemBlogService(new HttpFileProvider(client)); } else { client.BaseAddress = new Uri(server.Main.Uri); Main = new ApiBlogService(client); } } try { switch (server.Comment.Type) { case CommentServerType.Loment: { if (!server.Comment.Uri.EndsWith("/")) { server.Comment.Uri += "/"; } var client = httpClientFactory.CreateClient(); client.BaseAddress = new Uri(server.Comment.Uri); CommentService = new LomentCommentRepository(new LomentService(client)).AsService(this); } break; case CommentServerType.Main: { CommentService = Main.CommentService; } break; case CommentServerType.Disable: { CommentService = null; } break; } } catch { CommentService = null; } CommentService ??= new EmptyCommentRepo().AsService(this); try { switch (server.File.Type) { case FileServerType.Rebase: { FileService = new RebaseFileRepo(server.File.Uri).AsService(this); } break; case FileServerType.Main: { FileService = Main.FileService; } break; case FileServerType.Disable: { FileService = null; } break; } } catch { FileService = null; } FileService ??= new RebaseFileRepo(server.BaseAddress).AsService(this); try { switch (server.Statistic.Type) { case StatisticServerType.Listat: { if (!server.Statistic.Uri.EndsWith("/")) { server.Statistic.Uri += "/"; } var client = httpClientFactory.CreateClient(); client.BaseAddress = new Uri(server.Statistic.Uri); StatisticService = new ListatStatisticRepository(new ListatService(client)).AsService(this); } break; case StatisticServerType.Main: { StatisticService = Main.StatisticService; } break; case StatisticServerType.Disable: { StatisticService = null; } break; } } catch { StatisticService = null; } StatisticService ??= new EmptyStatisticRepo().AsService(this); }