Esempio n. 1
0
    public async Task GenerateThumbnailAndPreview(int versionId)
    {
        TemplateVersionTbl?version = (await _templateVersionTbl.Get(
                                          x => x.Id.Equals(versionId),
                                          null,
                                          nameof(TemplateVersionTbl.Template)))
                                     .FirstOrDefault();

        if (version == null)
        {
            return;
        }

        // Compile HTML and test data
        HandlebarsTemplate <object, object> template = Handlebars.Compile(version.Html);
        string result = template(JObject.Parse(version.TestData !));

        HtmlConverter converter = new();

        byte[] preview   = converter.FromHtmlString(result, format: ImageFormat.Png);
        byte[] thumbnail = converter.FromHtmlString(result, 75, ImageFormat.Png, 50);

        Uri previewUri = await SaveImage(version.Template.ProjectId, preview, $"Template-{version.TemplateId}-Version-{version.Id}-preview.png");

        Uri thumbnailUri = await SaveImage(version.Template.ProjectId, thumbnail, $"Template-{version.TemplateId}-Version-{version.Id}-thumbnail.png");

        await _templateVersionTbl.UpdateFromQuery(x => x.Id.Equals(versionId), _ => new TemplateVersionTbl
        {
            PreviewImage   = previewUri.ToString(),
            ThumbnailImage = thumbnailUri.ToString()
        });
    }
Esempio n. 2
0
    public async Task <IActionResult> OnPostMarkAsActive()
    {
        TemplateVersionTbl?version = (await _templateVersionTbl.Get(x =>
                                                                    x.Id.Equals(MarkAsActive.VersionId) &&
                                                                    x.TemplateId.Equals(MarkAsActive.TemplateId) &&
                                                                    x.Template !.ProjectId.Equals(MarkAsActive.ProjectId)))
                                     .FirstOrDefault();

        if (version == null)
        {
            throw new NullReferenceException(nameof(version));
        }

        version.IsActive = true;

        _templateVersionTbl.Update(version);

        await _templateVersionTbl.UpdateFromQuery(x =>
                                                  x.IsActive &&
                                                  !x.Id.Equals(version.Id) &&
                                                  x.TemplateId.Equals(version.TemplateId),
                                                  _ =>
                                                  new TemplateVersionTbl
        {
            IsActive = false
        });

        await _templateTbl.UpdateFromQuery(x => x.Id.Equals(MarkAsActive.TemplateId), _ => new TemplateTbl
        {
            DateModified = DateTime.Now
        });

        await _projectTbl.UpdateFromQuery(x => x.Id.Equals(MarkAsActive.ProjectId), _ => new ProjectTbl
        {
            DateModified = DateTime.Now
        });

        TempData["toastStatus"]  = "success";
        TempData["toastMessage"] = "Template version marked as active";
        TempData["scrollToId"]   = $"version-{version.Id}";

        return(RedirectToPage("/Project/Details", new { id = MarkAsActive.ProjectId }));
    }