|
|
@@ -1,16 +1,14 @@ |
|
|
|
(ns tservice.plugins.xps2pdf |
|
|
|
(:require [clojure.core.async :as async] |
|
|
|
[clojure.data.json :as json] |
|
|
|
(:require [clojure.data.json :as json] |
|
|
|
[clojure.java.io :as io] |
|
|
|
[clojure.spec.alpha :as s] |
|
|
|
[clojure.string :as str] |
|
|
|
[clojure.tools.logging :as log] |
|
|
|
[me.raynes.fs :as fs] |
|
|
|
[spec-tools.core :as st] |
|
|
|
[tservice.lib.files :refer [get-workdir]] |
|
|
|
[tservice.events :as events] |
|
|
|
[tservice.lib.fs :as fs-lib] |
|
|
|
[tservice.util :as u] |
|
|
|
[tservice.api.config :refer [get-workdir get-tservice-workdir]] |
|
|
|
[tservice.api.task :refer [make-plugin-metadata make-events-init publish-event!]] |
|
|
|
[tservice.api.storage.fs :as fs-lib] |
|
|
|
[tservice.plugins.xps2pdf.xps :as xps-lib])) |
|
|
|
|
|
|
|
;;; ------------------------------------------------ Event Specs ------------------------------------------------ |
|
|
@@ -45,58 +43,32 @@ |
|
|
|
|
|
|
|
;;; ------------------------------------------------ Event Metadata ------------------------------------------------ |
|
|
|
(def metadata |
|
|
|
{:route ["/tool/xps2pdf" |
|
|
|
{:tags ["Tool"] |
|
|
|
:post {:summary "Convert xps to pdf." |
|
|
|
:parameters {:body xps2pdf-params-body} |
|
|
|
:responses {201 {:body {:download_url string? :files [string?] :log_url string?}}} |
|
|
|
:handler (fn [{{{:keys [filepath]} :body} :parameters}] |
|
|
|
(let [workdir (get-workdir) |
|
|
|
from-path (if (re-matches #"^file:\/\/\/.*" filepath) |
|
|
|
; Absolute path with file:// |
|
|
|
(str/replace filepath #"^file:\/\/" "") |
|
|
|
(fs-lib/join-paths workdir (str/replace filepath #"^file:\/\/" ""))) |
|
|
|
from-files (if (fs-lib/directory? from-path) |
|
|
|
(filter #(fs-lib/regular-file? %) |
|
|
|
(map #(.getPath %) (file-seq (io/file from-path)))) |
|
|
|
[from-path]) |
|
|
|
relative-dir (fs-lib/join-paths "download" (u/uuid)) |
|
|
|
to-dir (fs-lib/join-paths workdir relative-dir) |
|
|
|
to-files (vec (map #(fs-lib/join-paths to-dir (str (fs/base-name % ".xps") ".pdf")) from-files)) |
|
|
|
zip-path (fs-lib/join-paths relative-dir "merged.zip") |
|
|
|
pdf-path (fs-lib/join-paths relative-dir "merged.pdf") |
|
|
|
log-path (fs-lib/join-paths relative-dir "log")] |
|
|
|
(fs-lib/create-directories! to-dir) |
|
|
|
; Launch the batchxps2pdf-convert |
|
|
|
(spit (fs-lib/join-paths workdir log-path) (json/write-str {:status "Running" :msg ""})) |
|
|
|
(events/publish-event! :batchxps2pdf-convert {:from-files from-files :to-dir to-dir}) |
|
|
|
{:status 201 |
|
|
|
:body {:download_url relative-dir |
|
|
|
:files (vec (map #(str/replace % (re-pattern workdir) "") to-files)) |
|
|
|
:log_url log-path |
|
|
|
:zip_url zip-path |
|
|
|
:pdf_url pdf-path}}))}}] |
|
|
|
:manifest {:description "Convert XPS to PDF File." |
|
|
|
:category "Tool" |
|
|
|
:home "https://github.com/clinico-omics/tservice-plugins" |
|
|
|
:name "XPS to PDF" |
|
|
|
:source "PGx" |
|
|
|
:short_name "xps2pdf" |
|
|
|
:icons [{:src "", :type "image/png", :sizes "192x192"} |
|
|
|
{:src "", :type "image/png", :sizes "192x192"}] |
|
|
|
:author "Jingcheng Yang" |
|
|
|
:hidden false |
|
|
|
:id "a4314e073061b74dc16e4c6a9dcd3999" |
|
|
|
:app_name "yangjingcheng/xps2pdf"}}) |
|
|
|
|
|
|
|
(def ^:const xps2pdf-topics |
|
|
|
"The `Set` of event topics which are subscribed to for use in xps2pdf tracking." |
|
|
|
#{:xps2pdf-convert |
|
|
|
:batchxps2pdf-convert}) |
|
|
|
|
|
|
|
(def ^:private xps2pdf-channel |
|
|
|
"Channel for receiving event xps2pdf we want to subscribe to for xps2pdf events." |
|
|
|
(async/chan)) |
|
|
|
(make-plugin-metadata |
|
|
|
{:name "xps2pdf" |
|
|
|
:params-schema xps2pdf-params-body |
|
|
|
:handler (fn [{:keys [filepath]}] |
|
|
|
(let [tservice-workdir (get-tservice-workdir) |
|
|
|
from-path (if (re-matches #"^file:\/\/\/.*" filepath) |
|
|
|
; Absolute path with file:// |
|
|
|
(str/replace filepath #"^file:\/\/" "") |
|
|
|
(fs-lib/join-paths tservice-workdir (str/replace filepath #"^file:\/\/" ""))) |
|
|
|
from-files (if (fs-lib/directory? from-path) |
|
|
|
(filter #(fs-lib/regular-file? %) |
|
|
|
(map #(.getPath %) (file-seq (io/file from-path)))) |
|
|
|
[from-path]) |
|
|
|
workdir (get-workdir) |
|
|
|
zip-path (fs-lib/join-paths workdir "merged.zip") |
|
|
|
pdf-path (fs-lib/join-paths workdir "merged.pdf") |
|
|
|
log-path (fs-lib/join-paths workdir "log")] |
|
|
|
(fs-lib/create-directories! workdir) |
|
|
|
; Launch the batchxps2pdf-convert |
|
|
|
(spit (fs-lib/join-paths workdir log-path) (json/write-str {:status "Running" :msg ""})) |
|
|
|
(publish-event! :batchxps2pdf-convert {:from-files from-files :to-dir workdir}) |
|
|
|
{:files [pdf-path |
|
|
|
zip-path] |
|
|
|
:log log-path})) |
|
|
|
:plugin-type :ToolPlugin |
|
|
|
:response-type "data2files"})) |
|
|
|
|
|
|
|
;;; ------------------------------------------------ Event Processing ------------------------------------------------ |
|
|
|
|
|
|
@@ -110,7 +82,7 @@ |
|
|
|
log (json/write-str {:status status :msg msg})] |
|
|
|
(spit log-path log))) |
|
|
|
|
|
|
|
(defn- batch-xps2pdf! [from-files to-dir] |
|
|
|
(defn- batch-xps2pdf! [{:keys [from-files to-dir]}] |
|
|
|
(log/info "Converted xps files in a zip to pdf files.") |
|
|
|
(let [to-files (vec (map #(fs-lib/join-paths to-dir (str (fs/base-name % ".xps") ".pdf")) from-files)) |
|
|
|
zip-path (fs-lib/join-paths to-dir "merged.zip") |
|
|
@@ -119,22 +91,8 @@ |
|
|
|
(fs-lib/zip-files to-files zip-path) |
|
|
|
(fs-lib/merge-pdf-files to-files pdf-path))) |
|
|
|
|
|
|
|
(defn- process-xps2pdf-event! |
|
|
|
"Handle processing for a single event notification received on the xps2pdf-channel" |
|
|
|
[xps2pdf-event] |
|
|
|
;; try/catch here to prevent individual topic processing exceptions from bubbling up. better to handle them here. |
|
|
|
(try |
|
|
|
(when-let [{topic :topic object :item} xps2pdf-event] |
|
|
|
;; TODO: only if the definition changed?? |
|
|
|
(case (events/topic->model topic) |
|
|
|
"xps2pdf" (xps2pdf! (:from object) (:to object)) |
|
|
|
"batchxps2pdf" (batch-xps2pdf! (:from-files object) (:to-dir object)))) |
|
|
|
(catch Throwable e |
|
|
|
(log/warn (format "Failed to process xps2pdf event. %s" (:topic xps2pdf-event)) e)))) |
|
|
|
|
|
|
|
;;; --------------------------------------------------- Lifecycle ---------------------------------------------------- |
|
|
|
|
|
|
|
(defn events-init |
|
|
|
(def events-init |
|
|
|
"Automatically called during startup; start event listener for xps2pdf events." |
|
|
|
[] |
|
|
|
(events/start-event-listener! xps2pdf-topics xps2pdf-channel process-xps2pdf-event!)) |
|
|
|
(make-events-init "batchxps2pdf" batch-xps2pdf!)) |