🐛 Fix template import (#6299)

This commit is contained in:
Alejandro Alonso 2025-04-15 10:39:22 +02:00 committed by GitHub
parent 202b9f3075
commit 2aaa2f3033
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 41 additions and 40 deletions

View file

@ -156,10 +156,13 @@ http {
} }
location / { location / {
location ~ ^/github/penpot-files/(?<template_file>[a-zA-Z0-9\-\_\.]+) { location ~ ^/github/penpot-files/(.+)$ {
proxy_pass https://raw.githubusercontent.com/penpot/penpot-files/main/$template_file; rewrite ^/github/penpot-files/(.+) /penpot/penpot-files/refs/heads/main/$1 break;
proxy_pass https://raw.githubusercontent.com;
proxy_hide_header Access-Control-Allow-Origin; proxy_hide_header Access-Control-Allow-Origin;
proxy_set_header User-Agent "curl/7.74.0"; proxy_hide_header Cookies;
proxy_set_header User-Agent "curl/8.5.0";
proxy_set_header Host "raw.githubusercontent.com"; proxy_set_header Host "raw.githubusercontent.com";
proxy_set_header Accept "*/*"; proxy_set_header Accept "*/*";
add_header Access-Control-Allow-Origin $http_origin; add_header Access-Control-Allow-Origin $http_origin;

View file

@ -135,10 +135,13 @@ http {
} }
location / { location / {
location ~ ^/github/penpot-files/(?<template_file>[a-zA-Z0-9\-\_\.]+) { location ~ ^/github/penpot-files/(.+)$ {
proxy_pass https://raw.githubusercontent.com/penpot/penpot-files/main/$template_file; rewrite ^/github/penpot-files/(.+) /penpot/penpot-files/refs/heads/main/$1 break;
proxy_pass https://raw.githubusercontent.com;
proxy_hide_header Access-Control-Allow-Origin; proxy_hide_header Access-Control-Allow-Origin;
proxy_set_header User-Agent "curl/7.74.0"; proxy_hide_header Cookies;
proxy_set_header User-Agent "curl/8.5.0";
proxy_set_header Host "raw.githubusercontent.com"; proxy_set_header Host "raw.githubusercontent.com";
proxy_set_header Accept "*/*"; proxy_set_header Accept "*/*";
add_header Access-Control-Allow-Origin $http_origin; add_header Access-Control-Allow-Origin $http_origin;

View file

@ -70,7 +70,7 @@
(mf/defc dashboard-legacy-redirect* (mf/defc dashboard-legacy-redirect*
{::mf/props :obj {::mf/props :obj
::mf/private true} ::mf/private true}
[{:keys [section team-id project-id search-term plugin-url template-url]}] [{:keys [section team-id project-id search-term plugin-url template]}]
(let [section (case section (let [section (case section
:dashboard-legacy-search :dashboard-legacy-search
:dashboard-search :dashboard-search
@ -98,7 +98,7 @@
:project-id project-id :project-id project-id
:search-term search-term :search-term search-term
:plugin plugin-url :plugin plugin-url
:template-url template-url}] :template template}]
(st/emit! (rt/nav section (d/without-nils params))))) (st/emit! (rt/nav section (d/without-nils params)))))
[:> loader* [:> loader*
@ -217,7 +217,7 @@
project-id (some-> params :project-id uuid/parse*) project-id (some-> params :project-id uuid/parse*)
search-term (some-> params :search-term) search-term (some-> params :search-term)
plugin-url (some-> params :plugin) plugin-url (some-> params :plugin)
template-url (some-> params :template)] template (some-> params :template)]
[:? [:?
#_[:& app.main.ui.releases/release-notes-modal {:version "2.5"}] #_[:& app.main.ui.releases/release-notes-modal {:version "2.5"}]
#_[:& app.main.ui.onboarding/onboarding-templates-modal] #_[:& app.main.ui.onboarding/onboarding-templates-modal]
@ -244,7 +244,7 @@
:search-term search-term :search-term search-term
:plugin-url plugin-url :plugin-url plugin-url
:project-id project-id :project-id project-id
:template-url template-url}]]]) :template template}]]])
:workspace :workspace
(let [params (get params :query) (let [params (get params :query)
@ -326,14 +326,14 @@
project-id (some-> params :path :project-id uuid/parse*) project-id (some-> params :path :project-id uuid/parse*)
search-term (some-> params :query :search-term) search-term (some-> params :query :search-term)
plugin-url (some-> params :query :plugin) plugin-url (some-> params :query :plugin)
template-url (some-> params :template)] template (some-> params :template)]
[:> dashboard-legacy-redirect* [:> dashboard-legacy-redirect*
{:team-id team-id {:team-id team-id
:section section :section section
:project-id project-id :project-id project-id
:search-term search-term :search-term search-term
:plugin-url plugin-url :plugin-url plugin-url
:template-url template-url}]) :template template}])
:viewer-legacy :viewer-legacy
(let [{:keys [query-params path-params]} route (let [{:keys [query-params path-params]} route

View file

@ -34,12 +34,10 @@
[app.main.ui.workspace.plugins] [app.main.ui.workspace.plugins]
[app.plugins.register :as preg] [app.plugins.register :as preg]
[app.util.dom :as dom] [app.util.dom :as dom]
[app.util.http :as http]
[app.util.i18n :refer [tr]] [app.util.i18n :refer [tr]]
[app.util.keyboard :as kbd] [app.util.keyboard :as kbd]
[app.util.object :as obj] [app.util.object :as obj]
[app.util.storage :as storage] [app.util.storage :as storage]
[app.util.webapi :as wapi]
[beicon.v2.core :as rx] [beicon.v2.core :as rx]
[cuerdas.core :as str] [cuerdas.core :as str]
[goog.events :as events] [goog.events :as events]
@ -211,17 +209,22 @@
(swap! storage/session dissoc :plugin-url)))))) (swap! storage/session dissoc :plugin-url))))))
(defn use-templates-import (defn use-templates-import
[can-edit? template-url project] [can-edit? template project]
(let [project-id (get project :id) (let [project-id (get project :id)
team-id (get project :team-id)] team-id (get project :team-id)]
(mf/with-layout-effect [can-edit? template-url project-id team-id] (mf/with-layout-effect [can-edit? template project-id team-id]
(when (and (some? template-url) (when (and (some? template)
(some? project-id) (some? project-id)
(some? team-id)) (some? team-id))
(if can-edit? (if can-edit?
(let [valid-url? (and (str/ends-with? template-url ".penpot") (let [valid-url? (str/ends-with? template ".penpot")
(str/starts-with? template-url cf/templates-uri))
template-name (when valid-url? (subs template-url (count cf/templates-uri))) ;; Backwards compatibility, ideally the template should be only the .penpot file name, not the full url
template-name (if (str/starts-with? template "http")
(subs template (count cf/templates-uri))
template)
template-url (str "/github/penpot-files/" template-name)
on-import #(st/emit! (dpj/fetch-files project-id) on-import #(st/emit! (dpj/fetch-files project-id)
(dd/fetch-recent-files team-id) (dd/fetch-recent-files team-id)
(dd/fetch-projects team-id) (dd/fetch-projects team-id)
@ -230,30 +233,22 @@
:name template-name :name template-name
:url template-url}))] :url template-url}))]
(if valid-url? (if valid-url?
(do (st/emit!
(st/emit! (ptk/event ::ev/event {::ev/name "install-template-from-link" :name template-name :url template-url})) (ptk/event ::ev/event {::ev/name "install-template-from-link" :name template-name :url template-url})
(->> (http/send! {:method :get (modal/show
:uri template-url
:response-type :blob
:omit-default-headers true})
(rx/subs!
(fn [result]
(if (or (< (:status result) 200) (>= (:status result) 300))
(st/emit! (notif/error (tr "dashboard.import.error")))
(st/emit! (modal/show
{:type :import {:type :import
:project-id project-id :project-id project-id
:entries [{:name template-name :uri (wapi/create-uri (:body result))}] :entries [{:name template-name :uri template-url}]
:on-finish-import on-import}))))))) :on-finish-import on-import}))
(st/emit! (notif/error (tr "dashboard.import.bad-url"))))) (st/emit! (notif/error (tr "dashboard.import.bad-url")))))
(st/emit! (notif/error (tr "dashboard.import.no-perms")))) (st/emit! (notif/error (tr "dashboard.import.no-perms"))))
(binding [storage/*sync* true] (binding [storage/*sync* true]
(swap! storage/session dissoc :template-url)))))) (swap! storage/session dissoc :template))))))
(mf/defc dashboard* (mf/defc dashboard*
{::mf/props :obj} {::mf/props :obj}
[{:keys [profile project-id team-id search-term plugin-url template-url section]}] [{:keys [profile project-id team-id search-term plugin-url template section]}]
(let [team (mf/deref refs/team) (let [team (mf/deref refs/team)
projects (mf/deref refs/projects) projects (mf/deref refs/projects)
@ -263,7 +258,7 @@
(filterv #(= team-id (:team-id %))))) (filterv #(= team-id (:team-id %)))))
can-edit? (dm/get-in team [:permissions :can-edit]) can-edit? (dm/get-in team [:permissions :can-edit])
template-url (or template-url (:template-url storage/session)) template (or template (:template storage/session))
plugin-url (or plugin-url (:plugin-url storage/session)) plugin-url (or plugin-url (:plugin-url storage/session))
default-project default-project
@ -289,7 +284,7 @@
(events/unlistenByKey key)))) (events/unlistenByKey key))))
(use-plugin-register plugin-url team-id (:id default-project)) (use-plugin-register plugin-url team-id (:id default-project))
(use-templates-import can-edit? template-url default-project) (use-templates-import can-edit? template default-project)
[:& (mf/provider ctx/current-project-id) {:value project-id} [:& (mf/provider ctx/current-project-id) {:value project-id}
[:> modal-container*] [:> modal-container*]

View file

@ -82,7 +82,7 @@
(binding [storage/*sync* true] (binding [storage/*sync* true]
(when (some? template) (when (some? template)
(swap! storage/session assoc (swap! storage/session assoc
:template-url template)) :template template))
(when (some? plugin) (when (some? plugin)
(swap! storage/session assoc (swap! storage/session assoc
:plugin-url plugin)))) :plugin-url plugin))))