summaryrefslogtreecommitdiff
path: root/elm/Lib
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2020-05-12 19:04:46 +0200
committerYorhel <git@yorhel.nl>2020-05-13 15:33:23 +0200
commita6814eea0cfe2a0bf9db9779e94c3dd398361522 (patch)
treea398dcd66c2306d7d535b7746b2788a9fd961f31 /elm/Lib
parente169129934ac56d0f0758c981da009523bf4619f (diff)
Chars::Edit: Add image editing
Diffstat (limited to 'elm/Lib')
-rw-r--r--elm/Lib/Api.elm25
-rw-r--r--elm/Lib/Util.elm8
2 files changed, 32 insertions, 1 deletions
diff --git a/elm/Lib/Api.elm b/elm/Lib/Api.elm
index 63f519cc..5dfda6d8 100644
--- a/elm/Lib/Api.elm
+++ b/elm/Lib/Api.elm
@@ -1,6 +1,7 @@
module Lib.Api exposing (..)
import Json.Encode as JE
+import File exposing (File)
import Http
import Gen.Api exposing (..)
@@ -22,7 +23,7 @@ showResponse res =
in case res of
HTTPError (Http.Timeout) -> "Network timeout, please try again later."
HTTPError (Http.NetworkError) -> "Network error, please try again later."
- HTTPError (Http.BadStatus r) -> "Server error " ++ String.fromInt r ++ ", please try again later, or report an issue if this persists."
+ HTTPError (Http.BadStatus r) -> "Server error " ++ String.fromInt r ++ ", please try again later or report an issue if this persists."
HTTPError (Http.BadBody r) -> "Invalid response from the server, please report a bug (debug info: " ++ r ++")."
HTTPError (Http.BadUrl _) -> unexp
Success -> unexp
@@ -42,6 +43,8 @@ showResponse res =
DoubleIP -> "You can only register one account from the same IP within 24 hours."
BadCurPass -> "Current password is invalid."
MailChange -> unexp
+ ImgFormat -> "Unrecognized image format, only JPEG and PNG are accepted."
+ Image _ _ _ -> unexp
Releases _ -> unexp
BoardResult _ -> unexp
TagResult _ -> unexp
@@ -69,3 +72,23 @@ post name body msg =
, body = Http.jsonBody body
, expect = expectResponse msg
}
+
+
+type ImageType
+ = Ch
+ | Cv
+ | Sf
+
+postImage : ImageType -> File -> (Response -> msg) -> Cmd msg
+postImage ty file msg =
+ Http.post
+ { url = "/elm/ImageUpload.json"
+ , body = Http.multipartBody
+ [ Http.stringPart "type" <| case ty of
+ Cv -> "cv"
+ Sf -> "sf"
+ Ch -> "ch"
+ , Http.filePart "img" file
+ ]
+ , expect = expectResponse msg
+ }
diff --git a/elm/Lib/Util.elm b/elm/Lib/Util.elm
index f840d003..34189565 100644
--- a/elm/Lib/Util.elm
+++ b/elm/Lib/Util.elm
@@ -2,6 +2,7 @@ module Lib.Util exposing (..)
import Dict
import Task
+import Lib.Ffi as Ffi
-- Delete an element from a List
delidx : Int -> List a -> List a
@@ -57,3 +58,10 @@ validateGtin =
|| n >= 9770000000000
|| modBy 10 (check n) /= 0
in String.filter Char.isDigit >> String.toInt >> Maybe.map (not << inval) >> Maybe.withDefault False
+
+
+-- Convert an image ID (e.g. "sf500") into a URL.
+imageUrl : String -> String
+imageUrl id =
+ let num = String.dropLeft 2 id |> String.toInt |> Maybe.withDefault 0
+ in Ffi.urlStatic ++ "/" ++ String.left 2 id ++ "/" ++ String.fromInt (modBy 10 (num // 10)) ++ String.fromInt (modBy 10 num) ++ "/" ++ String.fromInt num ++ ".jpg"