|
| 1 | +module Ocelot.Data.InputAcceptType |
| 2 | + ( allAudios |
| 3 | + , allImages |
| 4 | + , allVideos |
| 5 | + , validate |
| 6 | + ) where |
| 7 | + |
| 8 | +import Prelude |
| 9 | + |
| 10 | +import DOM.HTML.Indexed.InputAcceptType as DOM.HTML.Indexed.InputAcceptType |
| 11 | +import Data.Array as Data.Array |
| 12 | +import Data.Either (Either(..)) |
| 13 | +import Data.Maybe (Maybe(..)) |
| 14 | +import Data.MediaType as Data.MediaType |
| 15 | +import Data.String as Data.String |
| 16 | +import Data.String.CodeUnits as Data.String.CodeUnits |
| 17 | +import Data.String.Regex as Data.String.Regex |
| 18 | +import Web.File.File as Web.File.File |
| 19 | + |
| 20 | +allAudios :: DOM.HTML.Indexed.InputAcceptType.InputAcceptType |
| 21 | +allAudios = DOM.HTML.Indexed.InputAcceptType.mediaType (Data.MediaType.MediaType "audio/*") |
| 22 | + |
| 23 | +allImages :: DOM.HTML.Indexed.InputAcceptType.InputAcceptType |
| 24 | +allImages = DOM.HTML.Indexed.InputAcceptType.mediaType (Data.MediaType.MediaType "image/*") |
| 25 | + |
| 26 | +allVideos :: DOM.HTML.Indexed.InputAcceptType.InputAcceptType |
| 27 | +allVideos = DOM.HTML.Indexed.InputAcceptType.mediaType (Data.MediaType.MediaType "video/*") |
| 28 | + |
| 29 | +-- | Adapted from [attr-accept](https://github.com/react-dropzone/attr-accept) |
| 30 | +validate :: |
| 31 | + DOM.HTML.Indexed.InputAcceptType.InputAcceptType -> |
| 32 | + Web.File.File.File -> |
| 33 | + Boolean |
| 34 | +validate (DOM.HTML.Indexed.InputAcceptType.InputAcceptType xs) file = |
| 35 | + Data.Array.any (validateInputAcceptTypeAtom file) xs |
| 36 | + |
| 37 | +validateInputAcceptTypeAtom :: |
| 38 | + Web.File.File.File -> |
| 39 | + DOM.HTML.Indexed.InputAcceptType.InputAcceptTypeAtom -> |
| 40 | + Boolean |
| 41 | +validateInputAcceptTypeAtom file = case _ of |
| 42 | + DOM.HTML.Indexed.InputAcceptType.AcceptFileExtension extension -> |
| 43 | + validateFileExtension file extension |
| 44 | + DOM.HTML.Indexed.InputAcceptType.AcceptMediaType mediaType -> |
| 45 | + validateMediaType file mediaType |
| 46 | + |
| 47 | +validateFileExtension :: |
| 48 | + Web.File.File.File -> |
| 49 | + String -> |
| 50 | + Boolean |
| 51 | +validateFileExtension file accept = |
| 52 | + case Data.String.CodeUnits.stripSuffix (Data.String.Pattern (normalize accept)) (normalize (Web.File.File.name file)) of |
| 53 | + Nothing -> false |
| 54 | + Just _ -> true |
| 55 | + where |
| 56 | + normalize :: String -> String |
| 57 | + normalize = |
| 58 | + Data.String.toLower |
| 59 | + <<< Data.String.trim |
| 60 | + |
| 61 | +validateMediaType :: |
| 62 | + Web.File.File.File -> |
| 63 | + Data.MediaType.MediaType -> |
| 64 | + Boolean |
| 65 | +validateMediaType file (Data.MediaType.MediaType accept) = |
| 66 | + case Data.String.CodeUnits.stripSuffix (Data.String.Pattern "/*") validType of |
| 67 | + Nothing -> mimeType == validType |
| 68 | + Just baseValidType -> baseMimeType == baseValidType |
| 69 | + where |
| 70 | + mimeType :: String |
| 71 | + mimeType = normalize case Web.File.File.type_ file of |
| 72 | + Nothing -> "" |
| 73 | + Just (Data.MediaType.MediaType x) -> x |
| 74 | + |
| 75 | + baseMimeType :: String |
| 76 | + baseMimeType = case Data.String.Regex.regex "/.*$" mempty of |
| 77 | + Left _ -> mimeType |
| 78 | + Right regex -> Data.String.Regex.replace regex "" mimeType |
| 79 | + |
| 80 | + validType :: String |
| 81 | + validType = normalize accept |
| 82 | + |
| 83 | + normalize :: String -> String |
| 84 | + normalize = |
| 85 | + Data.String.toLower |
| 86 | + <<< Data.String.trim |
| 87 | + |
0 commit comments