2021-05-05 09:28:12 +02:00
|
|
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
;;
|
2022-09-20 23:23:22 +02:00
|
|
|
;; Copyright (c) KALEIDOS INC
|
2021-05-05 09:28:12 +02:00
|
|
|
|
|
|
|
(ns app.renderer.bitmap
|
|
|
|
"A bitmap renderer."
|
|
|
|
(:require
|
|
|
|
[app.browser :as bw]
|
2021-11-16 11:58:59 +01:00
|
|
|
[app.common.logging :as l]
|
2022-03-22 08:12:31 +01:00
|
|
|
[app.common.uri :as u]
|
2021-05-05 09:28:12 +02:00
|
|
|
[app.config :as cf]
|
2022-03-29 12:34:11 +02:00
|
|
|
[app.util.mime :as mime]
|
|
|
|
[app.util.shell :as sh]
|
2021-05-05 09:28:12 +02:00
|
|
|
[cuerdas.core :as str]
|
|
|
|
[promesa.core :as p]))
|
|
|
|
|
2022-03-29 12:34:11 +02:00
|
|
|
(defn render
|
2022-06-09 13:39:29 +02:00
|
|
|
[{:keys [file-id page-id token scale type objects] :as params} on-object]
|
2022-03-29 12:34:11 +02:00
|
|
|
(letfn [(prepare-options [uri]
|
|
|
|
#js {:screen #js {:width bw/default-viewport-width
|
|
|
|
:height bw/default-viewport-height}
|
|
|
|
:viewport #js {:width bw/default-viewport-width
|
|
|
|
:height bw/default-viewport-height}
|
|
|
|
:locale "en-US"
|
|
|
|
:storageState #js {:cookies (bw/create-cookies uri {:token token})}
|
|
|
|
:deviceScaleFactor scale
|
|
|
|
:userAgent bw/default-user-agent})
|
2021-05-05 09:28:12 +02:00
|
|
|
|
2022-03-29 12:34:11 +02:00
|
|
|
(render-object [page {:keys [id] :as object}]
|
2022-06-29 14:39:40 +02:00
|
|
|
(p/let [path (sh/tempfile :prefix "penpot.tmp.render.bitmap." :suffix (mime/get-extension type))
|
|
|
|
node (bw/select page (str/concat "#screenshot-" id))]
|
2022-03-29 12:34:11 +02:00
|
|
|
(bw/wait-for node)
|
|
|
|
(case type
|
|
|
|
:png (bw/screenshot node {:omit-background? true :type type :path path})
|
|
|
|
:jpeg (bw/screenshot node {:omit-background? false :type type :path path}))
|
|
|
|
(on-object (assoc object :path path))))
|
2021-05-05 09:28:12 +02:00
|
|
|
|
2022-03-29 12:34:11 +02:00
|
|
|
(render [uri page]
|
|
|
|
(l/info :uri uri)
|
|
|
|
(p/do
|
|
|
|
;; navigate to the page and perform basic setup
|
|
|
|
(bw/nav! page (str uri))
|
|
|
|
(bw/sleep page 1000) ; the good old fix with sleep
|
|
|
|
(bw/eval! page (js* "() => document.body.style.background = 'transparent'"))
|
2021-05-05 09:28:12 +02:00
|
|
|
|
2022-03-29 12:34:11 +02:00
|
|
|
;; take the screnshot of requested objects, one by one
|
|
|
|
(p/run! (partial render-object page) objects)
|
|
|
|
nil))]
|
2021-05-05 09:28:12 +02:00
|
|
|
|
2022-03-29 12:34:11 +02:00
|
|
|
(p/let [params {:file-id file-id
|
|
|
|
:page-id page-id
|
|
|
|
:object-id (mapv :id objects)
|
|
|
|
:route "objects"}
|
2022-06-08 07:42:03 +02:00
|
|
|
uri (-> (cf/get :public-uri)
|
2022-03-29 12:34:11 +02:00
|
|
|
(assoc :path "/render.html")
|
|
|
|
(assoc :query (u/map->query-string params)))]
|
|
|
|
(bw/exec! (prepare-options uri) (partial render uri)))))
|