Esempio n. 1
0
 public void Setup()
 {
     Factory = new WebApplicationFactory <AcBlog.Server.Api.Program>();
     Service = new ApiBlogService(Factory.CreateClient());
 }
Esempio n. 2
0
        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");
            }
        }
Esempio n. 3
0
        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);
        }