<commit_before>;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016 <NAME> <<EMAIL>>

(ns uxbox.media.impl
  "Implementation details and helpers."
  (:require [uxbox.media.proto :as p]
            [buddy.core.codecs :as codecs]
            [clojure.java.io :as io])
  (:import java.io.File
           java.io.ByteArrayInputStream
           java.io.InputStream
           java.nio.file.Path
           java.nio.file.Paths
           java.nio.file.Files
           java.nio.file.LinkOption
           java.nio.file.OpenOption
           java.nio.file.StandardOpenOption
           java.nio.file.attribute.FileAttribute
           java.nio.file.attribute.PosixFilePermissions))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Constants
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(def +open-opts+
  (->> [StandardOpenOption/CREATE_NEW
        StandardOpenOption/WRITE]
       (into-array OpenOption)))

(def +link-options+
  (into-array LinkOption [LinkOption/NOFOLLOW_LINKS]))

(def +file-attrs+
  (let [perms (PosixFilePermissions/fromString "rwxr-xr-x")
        attr (PosixFilePermissions/asFileAttribute perms)]
    (into-array FileAttribute [attr])))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helpers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn absolute?
  [path]
  (let [^Path path (p/-path path)]
    (.isAbsolute path)))

(defn exists?
  [path]
  (let [^Path path (p/-path path)]
    (Files/exists path +link-options+)))

(defn directory?
  [path]
  (let [^Path path (p/-path path)]
    (Files/isDirectory path +link-options+)))

(defn create-dir
  [path]
  (let [^Path path (p/-path path)]
    (Files/createDirectories path +file-attrs+)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Impl
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; TODO: Path
;; TODO: File
;; TODO: URI
;; TODO: URL

(extend-protocol p/IContent
  String
  (-input-stream [v]
    (ByteArrayInputStream. (codecs/str->bytes v)))

  InputStream
  (-input-stream [v]
    v)

  ratpack.http.TypedData
  (-input-stream [this]
    (.getInputStream this)))

(extend-protocol p/IPath
  Path
  (-path [v] v)

  String
  (-path [v] (Paths/get v (make-array String 0)))

  clojure.lang.Sequential
  (-path [v]
    (let [fv (first v)
          mv (rest v)]
      (Paths/get ^String fv (into-array String mv)))))

(extend-type Path
  io/IOFactory
  (make-reader [path opts]
    (let [^File file (.toFile path)]<commit_msg>Add IContent impl for URL, URI, File and Path.<commit_after>;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016 <NAME> <<EMAIL>>

(ns uxbox.media.impl
  "Implementation details and helpers."
  (:require [uxbox.media.proto :as p]
            [buddy.core.codecs :as codecs]
            [clojure.java.io :as io])
  (:import java.io.File
           java.io.ByteArrayInputStream
           java.io.InputStream
           java.nio.file.Path
           java.nio.file.Paths
           java.nio.file.Files
           java.nio.file.LinkOption
           java.nio.file.OpenOption
           java.nio.file.StandardOpenOption
           java.nio.file.attribute.FileAttribute
           java.nio.file.attribute.PosixFilePermissions))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Constants
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(def +open-opts+
  (->> [StandardOpenOption/CREATE_NEW
        StandardOpenOption/WRITE]
       (into-array OpenOption)))

(def +link-options+
  (into-array LinkOption [LinkOption/NOFOLLOW_LINKS]))

(def +file-attrs+
  (let [perms (PosixFilePermissions/fromString "rwxr-xr-x")
        attr (PosixFilePermissions/asFileAttribute perms)]
    (into-array FileAttribute [attr])))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helpers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn absolute?
  [path]
  (let [^Path path (p/-path path)]
    (.isAbsolute path)))

(defn exists?
  [path]
  (let [^Path path (p/-path path)]
    (Files/exists path +link-options+)))

(defn directory?
  [path]
  (let [^Path path (p/-path path)]
    (Files/isDirectory path +link-options+)))

(defn create-dir
  [path]
  (let [^Path path (p/-path path)]
    (Files/createDirectories path +file-attrs+)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Impl
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; TODO: Path
;; TODO: File
;; TODO: URI
;; TODO: URL

(extend-protocol p/IContent
  String
  (-input-stream [v]
    (ByteArrayInputStream. (codecs/str->bytes v)))

  Path
  (-input-stream [v]
    (io/input-stream v))

  File
  (-input-stream [v]
    (io/input-stream v))

  URI
  (-input-stream [v]
    (io/input-stream v))

  URL
  (-input-stream [v]
    (io/input-stream v))

  InputStream
  (-input-stream [v]
    v)

  ratpack.http.TypedData
  (-input-stream [this]
    (.getInputStream this)))

(extend-protocol p/IPath
  Path
  (-path [v] v)

  String
  (-path [v] (Paths/get v (make-array String 0)))

  clojure.lang.Sequential
  (-path [v]
    (let [fv (first v)
          mv (rest v)]
      (Paths/get ^String fv (into-array String mv)))))

(extend-type Path
  io/IOFactory
  (make-reader [path opts]
    (let [^File file (.toFile path)]
{'fraction_non_alphanumeric': 0.2689413823272091, 'fraction_numerical': 0.0031496062992125984, 'mean_word_length': 3.33030303030303, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3795086', 'n_tokens_mistral': 2160, 'n_tokens_neox': 1899, 'n_words': 433}