mirror of https://github.com/logseq/logseq
fix(fs): asset url handling
parent
31d7c8f836
commit
ad564a4d72
|
@ -41,7 +41,8 @@
|
|||
(let [ast (map first ast)
|
||||
file (if uri-encoded? (js/decodeURI file-path) file-path)]
|
||||
;; check backward compatibility?
|
||||
(if (string/includes? file "pages/contents.")
|
||||
;; FIXME: use pre-config dir
|
||||
(if (string/starts-with? file "pages/contents.")
|
||||
"Contents"
|
||||
(let [first-block (last (first (filter gp-block/heading-block? ast)))
|
||||
property-name (when (contains? #{"Properties" "Property_Drawer"} (ffirst ast))
|
||||
|
|
|
@ -201,6 +201,7 @@
|
|||
asset-path (gp-config/remove-asset-protocol src)]
|
||||
(if (string/blank? asset-path)
|
||||
(reset! *exist? false)
|
||||
;; FIXME(andelf): possible bug here
|
||||
(p/let [exist? (fs/file-or-href-exists? "" asset-path)]
|
||||
(reset! *exist? (boolean exist?))))
|
||||
(assoc state ::asset-path asset-path ::asset-file? true))
|
||||
|
|
|
@ -47,7 +47,6 @@
|
|||
(defn- write-file-impl!
|
||||
[this repo dir rpath content {:keys [ok-handler error-handler old-content skip-compare?]} stat]
|
||||
(prn ::write-file-impl repo dir rpath)
|
||||
(js/console.trace)
|
||||
(let [file-path (fs2-path/path-join dir rpath)]
|
||||
(if skip-compare?
|
||||
(p/catch
|
||||
|
|
|
@ -15,8 +15,9 @@
|
|||
|
||||
|
||||
|
||||
(defn file-name
|
||||
"File name of a path or URL"
|
||||
(defn filename
|
||||
"File name of a path or URL.
|
||||
Returns nil when it's a path."
|
||||
[path]
|
||||
(let [fname (if (string/ends-with? path "/")
|
||||
nil
|
||||
|
@ -29,11 +30,11 @@
|
|||
(defn split-ext
|
||||
"Split file name into stem and extension, for both path and URL"
|
||||
[path]
|
||||
(let [fname (file-name path)
|
||||
(let [fname (filename path)
|
||||
pos (string/last-index-of fname ".")]
|
||||
(if-not (or (nil? pos) (zero? pos))
|
||||
[(subs fname 0 pos)
|
||||
(subs fname (+ pos 1))]
|
||||
(string/lower-case (subs fname (+ pos 1)))]
|
||||
[fname ""])))
|
||||
|
||||
(defn file-stem
|
||||
|
@ -42,12 +43,12 @@
|
|||
(first (split-ext path)))
|
||||
|
||||
(defn file-ext
|
||||
"File extension"
|
||||
"File extension, lowercased"
|
||||
[path]
|
||||
(second (split-ext path)))
|
||||
|
||||
(defn safe-file-name?
|
||||
"Safe path on all platforms"
|
||||
(defn safe-filename?
|
||||
"Safe filename on all platforms"
|
||||
[fname]
|
||||
(and (not (string/blank? fname))
|
||||
(< (count fname) 255)
|
||||
|
@ -57,9 +58,6 @@
|
|||
(re-find #"(?i)^(COM[0-9]|CON|LPT[0-9]|NUL|PRN|AUX|com[0-9]|con|lpt[0-9]|nul|prn|aux)\..+" fname)
|
||||
(re-find #"[\u0000-\u001f\u0080-\u009f]" fname)))))
|
||||
|
||||
(comment defn inspect [x]
|
||||
(prn ::inspect x)
|
||||
x)
|
||||
|
||||
(defn path-join
|
||||
"Joins the given path segments into a single path, handling relative paths,
|
||||
|
|
|
@ -54,7 +54,8 @@
|
|||
[logseq.graph-parser.util.block-ref :as block-ref]
|
||||
[logseq.graph-parser.util.page-ref :as page-ref]
|
||||
[promesa.core :as p]
|
||||
[rum.core :as rum]))
|
||||
[rum.core :as rum]
|
||||
[frontend.fs2.path :as fs2-path]))
|
||||
|
||||
;; FIXME: should support multiple images concurrently uploading
|
||||
|
||||
|
@ -1386,21 +1387,25 @@
|
|||
(p/let [repo-dir (config/get-repo-dir repo)
|
||||
assets-dir "assets"
|
||||
_ (fs/mkdir-if-not-exists (str repo-dir "/" assets-dir))]
|
||||
(prn ::ensure-assets-dir repo-dir assets-dir)
|
||||
[repo-dir assets-dir]))
|
||||
|
||||
(defn get-asset-path
|
||||
"Get asset path from filename, ensure assets dir exists"
|
||||
[filename]
|
||||
(p/let [[repo-dir assets-dir] (ensure-assets-dir! (state/get-current-repo))]
|
||||
(util/safe-path-join repo-dir assets-dir filename)))
|
||||
(fs2-path/path-join repo-dir assets-dir filename)))
|
||||
|
||||
(defn save-assets!
|
||||
"Save incoming(pasted) assets to assets directory.
|
||||
|
||||
Returns: [filename file-obj file-fpath matched-alias]"
|
||||
([_ repo files]
|
||||
(p/let [[repo-dir assets-dir] (ensure-assets-dir! repo)]
|
||||
(save-assets! repo repo-dir assets-dir files
|
||||
(fn [index file-base]
|
||||
(fn [index file-stem]
|
||||
;; TODO: maybe there're other chars we need to handle?
|
||||
(let [file-base (-> file-base
|
||||
(let [file-base (-> file-stem
|
||||
(string/replace " " "_")
|
||||
(string/replace "%" "_")
|
||||
(string/replace "/" "_"))
|
||||
|
@ -1411,24 +1416,23 @@
|
|||
(for [[index ^js file] (map-indexed vector files)]
|
||||
;; WARN file name maybe fully qualified path when paste file
|
||||
(let [file-name (util/node-path.basename (.-name file))
|
||||
[file-base ext-full ext-base] (if file-name
|
||||
(let [ext-base (util/node-path.extname file-name)
|
||||
ext-full (if-not (config/extname-of-supported? ext-base)
|
||||
(util/full-path-extname file-name) ext-base)]
|
||||
[(subs file-name 0 (- (count file-name)
|
||||
(count ext-full))) ext-full ext-base])
|
||||
["" "" ""])
|
||||
filename (str (gen-filename index file-base) ext-full)
|
||||
[file-stem ext-full ext-base] (if file-name
|
||||
(let [ext-base (util/node-path.extname file-name)
|
||||
ext-full (if-not (config/extname-of-supported? ext-base)
|
||||
(util/full-path-extname file-name) ext-base)]
|
||||
[(subs file-name 0 (- (count file-name)
|
||||
(count ext-full))) ext-full ext-base])
|
||||
["" "" ""])
|
||||
filename (str (gen-filename index file-stem) ext-full)
|
||||
filename (str path "/" filename)
|
||||
matched-alias (assets-handler/get-matched-alias-by-ext ext-base)
|
||||
filename (cond-> filename
|
||||
(not (nil? matched-alias))
|
||||
(string/replace #"^[.\/\\]*assets[\/\\]+" ""))
|
||||
dir (or (:dir matched-alias) dir)]
|
||||
filename (cond-> filename
|
||||
(not (nil? matched-alias))
|
||||
(string/replace #"^[.\/\\]*assets[\/\\]+" ""))
|
||||
dir (or (:dir matched-alias) dir)]
|
||||
|
||||
(if (util/electron?)
|
||||
(let [from (.-path file)
|
||||
from (if (string/blank? from) nil from)]
|
||||
(let [from (not-empty (.-path file))]
|
||||
|
||||
(js/console.debug "Debug: Copy Asset #" dir filename from)
|
||||
|
||||
|
@ -1447,6 +1451,7 @@
|
|||
(defonce *assets-url-cache (atom {}))
|
||||
|
||||
(defn make-asset-url
|
||||
"Make asset URL for UI element, to fill img.src"
|
||||
[path] ;; path start with "/assets" or compatible for "../assets"
|
||||
(if config/publishing? path
|
||||
(let [repo (state/get-current-repo)
|
||||
|
@ -1454,6 +1459,7 @@
|
|||
path (string/replace path "../" "/")
|
||||
full-path (util/node-path.join repo-dir path)
|
||||
data-url? (string/starts-with? path "data:")]
|
||||
(prn ::make-asset-url full-path)
|
||||
(cond
|
||||
data-url?
|
||||
path ;; just return the original
|
||||
|
@ -1499,25 +1505,32 @@
|
|||
|
||||
;; assets/journals_2021_02_03_1612350230540_0.png
|
||||
(defn resolve-relative-path
|
||||
"Relative path to current file path.
|
||||
|
||||
Requires editing state"
|
||||
[file-path]
|
||||
(if-let [current-file (or (db-model/get-block-file-path (state/get-edit-block))
|
||||
(if-let [current-file-rpath (or (db-model/get-block-file-path (state/get-edit-block))
|
||||
;; fix dummy file path of page
|
||||
(and (util/electron?)
|
||||
(util/node-path.join
|
||||
(config/get-repo-dir (state/get-current-repo))
|
||||
(config/get-pages-directory) "_.md")))]
|
||||
(util/get-relative-path current-file file-path)
|
||||
(and (util/electron?)
|
||||
(util/node-path.join
|
||||
(config/get-repo-dir (state/get-current-repo))
|
||||
(config/get-pages-directory) "_.md")))]
|
||||
(let [repo-dir (config/get-repo-dir (state/get-current-repo))
|
||||
current-file-fpath (fs2-path/path-join repo-dir current-file-rpath)]
|
||||
(util/get-relative-path current-file-fpath file-path))
|
||||
file-path))
|
||||
|
||||
(defn upload-asset
|
||||
"Paste asset and insert link to current editing block"
|
||||
[id ^js files format uploading? drop-or-paste?]
|
||||
(let [repo (state/get-current-repo)
|
||||
block (state/get-edit-block)]
|
||||
(when (config/local-db? repo)
|
||||
(-> (save-assets! block repo (js->clj files))
|
||||
;; FIXME: only the first asset is handled
|
||||
(p/then
|
||||
(fn [res]
|
||||
(when-let [[asset-file-name file full-file-path matched-alias] (and (seq res) (first res))]
|
||||
(when-let [[asset-file-name file-obj asset-file-fpath matched-alias] (and (seq res) (first res))]
|
||||
(let [image? (config/ext-of-image? asset-file-name)]
|
||||
(insert-command!
|
||||
id
|
||||
|
@ -1526,8 +1539,8 @@
|
|||
(str
|
||||
(if image? "../assets/" "")
|
||||
"@" (:name matched-alias) "/" asset-file-name)
|
||||
(resolve-relative-path (or full-file-path asset-file-name)))
|
||||
(if file (.-name file) (if image? "image" "asset"))
|
||||
(resolve-relative-path (or asset-file-fpath asset-file-name)))
|
||||
(if file-obj (.-name file-obj) (if image? "image" "asset"))
|
||||
image?)
|
||||
format
|
||||
{:last-pattern (if drop-or-paste? "" (state/get-editor-command-trigger))
|
||||
|
|
Loading…
Reference in New Issue