Skip to content

Instantly share code, notes, and snippets.

@rafagil

rafagil/core.clj Secret

Created Nov 18, 2020
Embed
What would you like to do?
(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