Skip to content

Instantly share code, notes, and snippets.

@rafagil

rafagil/core.clj Secret

Created November 18, 2020 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafagil/8bf03c4de4ef19dae728b61542f26861 to your computer and use it in GitHub Desktop.
Save rafagil/8bf03c4de4ef19dae728b61542f26861 to your computer and use it in GitHub Desktop.
(ns clojure-sync.core
(:require [immutant.web :as web]
[compojure.core :refer :all]
[compojure.route :as route]
[ring.middleware.defaults :refer :all]
[ring.middleware [multipart-params :as mp]]
[clojure.string :as str]
[clojure.java.io :as io]
[clojure.data.json :as json])
(:gen-class))
(def BACKUP_FOLDER "/home/rafa/samba/public/iOSBackup/")
(defn send-json [contents]
{:status 200
:headers {"Content-Type" "application/json"}
:body (str (json/write-str contents))})
(defn ext [filename]
(second (str/split filename #"\.")))
(defn upload-file [file date]
(->> (str BACKUP_FOLDER date "." (ext (file :filename)))
io/file
(io/copy (file :tempfile)))
(send-json "OK"))
(defn list-files [req]
(->> BACKUP_FOLDER
io/file
file-seq
(filter #(.isFile %))
(mapv #(.getName %))
(mapv #(first (str/split % #"\.")))
(reduce #(assoc %1 %2 1) {})
send-json))
(defroutes app-routes
(GET "/processed" [] list-files)
(mp/wrap-multipart-params
(POST "/upload" {params :params} (upload-file (get params "photo") (get params "date"))))
(route/not-found "Not Found"))
(defn -main []
(web/run (wrap-defaults app-routes api-defaults) :host "192.168.1.112" :port 3003))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment