Пример #1
0
        public async Task <IActionResult> Settings(SettingsViewModel model)
        {
            if (ModelState.IsValid)
            {
                var settings = _serviceProvider.GetService <ISynologyConnectionSettings>();

                settings.BaseHost = model.SynologyHost;
                settings.Password = model.SynologyPass;
                settings.Port     = model.SynologyPort;
                settings.Ssl      = model.UseSsl;
                settings.SslPort  = model.SynologyPort;
                settings.Username = model.SynologyUser;

                using (var syno = _serviceProvider.GetService <ISynologyConnection>())
                {
                    var result = await syno.Api().Auth().LoginAsync();

                    if (!result.Success && result.Error.Code != 403)
                    {
                        ModelState.AddModelError("", "Invalid connection settings.");
                    }
                    else
                    {
                        var json = JsonConvert.SerializeObject(model, Formatting.Indented);

                        IOFile.WriteAllText("synosettings.json", json);

                        return(RedirectToAction("Index"));
                    }
                }
            }

            return(View(model));
        }
Пример #2
0
    public int OnExecute()
    {
        var sess = _main.GetSession();

        using var client = sess.GetClient();

        var vault = _main.GetVault(client, Vault !);

        if (vault == null)
        {
            return(1);
        }

        var output = new GetVaultOutput
        {
            Id    = vault.Id !.Value,
            Label = vault.Summary !.Label,
            Type  = vault.Summary !.Type,
        };

        var outputSer = JsonSerializer.Serialize(output,
                                                 Indented ? Common.IndentedJsonOutput : Common.DefaultJsonOutput);

        if (File == null)
        {
            _console.WriteLine(outputSer);
        }
        else
        {
            IOFile.WriteAllText(File, outputSer);
        }

        return(0);
    }
Пример #3
0
    public int OnExecute()
    {
        var sess = _main.GetSession();

        using var client = sess.GetClient();

        var vault = Vault == null
            ? null
            : _main.GetVault(client, Vault !);

        if (vault == null && Vault != null)
        {
            return(1);
        }

        var record = _main.GetRecord(client, ref vault, Record !);

        if (record == null)
        {
            return(1);
        }

        client.UnlockRecord(vault !, record);

        var output = new GetRecordOutput
        {
            Id       = record.Id !.Value,
            Label    = record.Summary !.Label,
            Type     = record.Summary !.Type,
            Address  = record.Summary !.Address,
            Username = record.Summary !.Username,
            Tags     = record.Summary !.Tags,
            Memo     = record.Content !.Memo,
            Password = record.Content !.Password,
            Fields   = record.Content.Fields?.Select(x =>
                                                     new NewCommands.NewRecordCommand.NewRecordInput.Field()
            {
                Name  = x.Name,
                Type  = x.Type,
                Value = x.Value,
            }).ToArray(),
        };

        var outputSer = JsonSerializer.Serialize(output,
                                                 Indented ? Common.IndentedJsonOutput : Common.DefaultJsonOutput);

        if (File == null)
        {
            _console.WriteLine(outputSer);
        }
        else
        {
            IOFile.WriteAllText(File, outputSer);
        }

        return(0);
    }
Пример #4
0
    public int OnExecute()
    {
        var t = Templates.Templates.GetTemplate(Name !);

        if (t == null)
        {
            _console.WriteError("could not resolve Template for given name");
            return(0);
        }

        if (File != null)
        {
            IOFile.WriteAllText(File, t);
        }
        else
        {
            _console.WriteLine(t);
        }

        return(0);
    }
Пример #5
0
        public IActionResult UpdateYamlFile([FromBody] string yaml)
        {
            if (!OptionsSnapshot.Value.RemoteConfiguration)
            {
                return(Forbid());
            }

            if (!TryValidateYaml(yaml, out var error))
            {
                Logger.Error(error, "Failed to validate YAML configuration");
                return(BadRequest(error));
            }

            try
            {
                IOFile.WriteAllText(Program.ConfigurationFile, yaml);
                return(Ok());
            }
            catch (Exception ex)
            {
                throw new IOException($"Failed to update configuration file: {ex.Message}");
            }
        }
Пример #6
0
        public ActionResult Html2Png(int width, int height, string html)
        {
            // fit to max 1920 x 1080.
            if (width > 1920)
            {
                var scale = 1920.0 / width;
                width  = 1920;
                height = (int)(height * scale);
            }
            if (height > 1080)
            {
                var scale = 1080.0 / height;
                width  = (int)(width * scale);
                height = 1080;
            }

            // Realize html string to file.
            var htmlFilePath = Server.MapPath("~/App_Data/" + Guid.NewGuid().ToString("N") + ".html");

            IOFile.WriteAllText(htmlFilePath, html);

            // Prepare for save image file.
            var imageFilePath = Server.MapPath("~/App_Data/" + Guid.NewGuid().ToString("N") + ".png");
            var imageBytes    = default(byte[]);

            try
            {
                // Launch PhantomJS and take a screen shot into image file.
                var procInfo = new ProcessStartInfo
                {
                    FileName  = Server.MapPath("~/bin/phantomjs.exe"),
                    Arguments = string.Format("\"{0}\" {1} {2} \"{3}\" \"{4}\"",
                                              Server.MapPath("~/bin/take-screen-shot.js"),
                                              width,
                                              height,
                                              htmlFilePath,
                                              imageFilePath)
                };
                var proc = Process.Start(procInfo);
                proc.WaitForExit();
            }
            finally
            {
                // Sweep the html file.
                if (IOFile.Exists(htmlFilePath))
                {
                    try { IOFile.Delete(htmlFilePath); }
                    catch (Exception)
                    {
                        // TODO: Report
                    }
                }

                // Read image file into memory and sweep the image file.
                if (IOFile.Exists(imageFilePath))
                {
                    try
                    {
                        imageBytes = IOFile.ReadAllBytes(imageFilePath);
                        IOFile.Delete(imageFilePath);
                    }
                    catch (Exception)
                    {
                        // TODO: Report
                    }
                }
            }

            // Respond to client.
            if (imageBytes != null)
            {
                return(new FileContentResult(imageBytes, "image/png"));
            }
            else
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
            }
        }