 (ns mdr2.dtb
-  "Functions to query [DAISY Talking Books](http://www.daisy.org/daisypedia/daisy-digital-talking-book)"
+  "Functions to query [DAISY Talking
+  Books](http://www.daisy.org/daisypedia/daisy-digital-talking-book)"
   (:require [clojure.java.io :refer [file]]
             [clojure.string :as s]
             [pantomime.mime :refer [mime-type-of]])

  formats for a DAISY Talking Book namely *MPEG-4 AAC audio*, *MPEG-1/2
  Layer III (MP3) audio* and *Linear PCM - RIFF WAVE format audio*?"
  [file]
  (contains? #{"audio/mpeg" "audio/x-wav" "audio/mp4"} (mime-type-of file)))

(defn- text-file?
  "Is the given `file` a text file, i.e. any of the valid text file
  formats for a DAISY Talking Book namely DTBook XML?"
  [file]
  (contains? #{"application/xml"} (mime-type-of file)))

(defn- image-file?
  "Is the given `file` an image file, i.e. any of the valid image file
  formats for a DAISY Talking Book namely png and jpeg?"
  [file]
  (contains? #{"image/png" "image/jpeg"} (mime-type-of file)))

(defn- ncx-file?
  "Is the given `file` an ncx file?"

  "Return the format in which the audio files in the DTB file set are
  written for a given DAISY Talking Book"
  [{path :path}]
  (let [mime-types (->> (file-seq (file path))
                        (filter audio-file?)
                        (remove #(= (.getName %) "tpbnarrator_res.mp3"))
                        (map mime-type-of))]
    (cond
     (every? #{"audio/mp4"} mime-types) "MP4-AAC"
     (every? #{"audio/mpeg"} mime-types) "MP3"
     (every? #{"audio/x-wav"} mime-types) "WAV"
     :else "")))

(defn- file-audio-length
  "Get the length of the audio in seconds for a given audio `file`"
