Skip to content

Commit

Permalink
add system-check to filestore boot process
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramblurr committed May 14, 2024
1 parent 7cda0d0 commit c9beccf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/clj/app/file_utils.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
(ns app.file-utils
(:require
[clojure.string :as str]
[clojure.java.io :as io])
(:import [java.io File]
[java.nio.file Path Paths]
[java.nio.file Files]))
[clojure.java.io :as io]
[clojure.java.shell :refer [sh]]
[clojure.string :as str])
(:import
[java.io File]
[java.nio.file Path Paths]
[java.nio.file Files]))

(defn- file
^File [& args]
Expand Down Expand Up @@ -138,6 +140,24 @@
(defn delete-if-exists [f]
(Files/deleteIfExists (to-path f)))

(defn exists?
"Checks for the existence of file/directory. File can be a path or a File object."
[file] (.exists (io/file file)))

(defn writeable?
"Checks if the file is writeable. File can be a path or a File object."
[file]
(.canWrite (io/file file)))

(defn program-exists?
"Checks if the program exists in the PATH."
[program]
(let [cmd "which"]
(try
(= 0 (:exit (sh cmd program)))
(catch Exception e ;in the unlikely event where which is unavailable
(throw (ex-info (format "Unable to determine whether '%s' exists. Notifications may not work." program) {}))))))

(comment
(= true (validate-base-path "/" "/foo/bar"))
(= true (validate-base-path "/foo/bar" "/foo/bar"))
Expand Down
14 changes: 14 additions & 0 deletions src/clj/app/filestore.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
(ns app.filestore
(:require
[app.file-utils :as fs]
[app.filestore.image :as im]
[blocks.core :as block]
[blocks.store.file :as blocks.store.file]
[com.stuartsierra.component :as component]
[multiformats.hash :as mhash]))

(defn system-check! [store-path]
(when-not (fs/exists? store-path)
(throw (ex-info "Filestore path does not exist" {:store-path store-path})))
(when-not (fs/writeable? store-path)
(throw (ex-info "Filestore path is not writeable" {:store-path store-path})))
(when-not (fs/program-exists? "convert")
(throw (ex-info "ImageMagick convert program not found in PATH" {})))
(when-not (fs/program-exists? "mogrify")
(throw (ex-info "ImageMagick mogrify program not found in PATH" {})))
(when-not (fs/program-exists? "identify")
(throw (ex-info "ImageMagick identify program not found in PATH" {}))))

(defn start! [{:keys [store-path]}]
(system-check! store-path)
(-> store-path
(blocks.store.file/file-block-store)
(component/start)))
Expand Down

0 comments on commit c9beccf

Please sign in to comment.