示例#1
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));
            }
        }
示例#2
0
    public async Task <int> OnExecuteAsync()
    {
        var sess = _main.GetSession();

        using var client = sess.GetClient();

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

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

        if (!IOFile.Exists(File))
        {
            _console.WriteError("could not find input file.");
            return(1);
        }

        var fileContent = IOFile.ReadAllText(File);
        var fileInput   = JsonSerializer.Deserialize <NewRecordInput>(fileContent,
                                                                      MainCommand.JsonInputOpts);

        if (fileInput == null)
        {
            _console.WriteError("could not read or parse input file.");
            return(1);
        }

        var record = new Record();

        record.Summary          = new();
        record.Summary.Label    = fileInput.Label;
        record.Summary.Type     = fileInput.Type;
        record.Summary.Username = fileInput.Username;
        record.Summary.Address  = fileInput.Address;
        record.Summary.Tags     = fileInput.Tags;

        record.Content          = new();
        record.Content.Password = fileInput.Password;
        record.Content.Memo     = fileInput.Memo;

        record.Content.Fields = fileInput.Fields?.Select(x => new RecordField
        {
            Name  = x.Name,
            Type  = x.Type,
            Value = x.Value,
        }).ToList();

        var id = await client.SaveRecordAsync(vault, record);

        sess.Save(client);

        _console.WriteLine(id);

        if (Delete)
        {
            IOFile.Delete(File);
        }

        return(0);
    }