Add mime type info to file and image uploads.
This commit is contained in:
parent
03c472a78d
commit
c483e6ac6c
18
src/Main.elm
18
src/Main.elm
|
@ -86,30 +86,30 @@ update msg model = case msg of
|
||||||
SendFiles rid -> (model, Select.files [ "application/*" ] <| FilesSelected rid)
|
SendFiles rid -> (model, Select.files [ "application/*" ] <| FilesSelected rid)
|
||||||
ImagesSelected rid f fs -> updateUploadSelected model rid f fs (ImageUploadComplete rid)
|
ImagesSelected rid f fs -> updateUploadSelected model rid f fs (ImageUploadComplete rid)
|
||||||
FilesSelected rid f fs -> updateUploadSelected model rid f fs (FileUploadComplete rid)
|
FilesSelected rid f fs -> updateUploadSelected model rid f fs (FileUploadComplete rid)
|
||||||
ImageUploadComplete rid ur -> updateImageUploadComplete model rid ur
|
ImageUploadComplete rid mime ur -> updateImageUploadComplete model rid mime ur
|
||||||
FileUploadComplete rid ur -> updateFileUploadComplete model rid ur
|
FileUploadComplete rid mime ur -> updateFileUploadComplete model rid mime ur
|
||||||
SendImageResponse _ -> (model, Cmd.none)
|
SendImageResponse _ -> (model, Cmd.none)
|
||||||
SendFileResponse _ -> (model, Cmd.none)
|
SendFileResponse _ -> (model, Cmd.none)
|
||||||
|
|
||||||
updateFileUploadComplete : Model -> RoomId -> (Result Http.Error String) -> (Model, Cmd Msg)
|
updateFileUploadComplete : Model -> RoomId -> String -> (Result Http.Error String) -> (Model, Cmd Msg)
|
||||||
updateFileUploadComplete m rid ur =
|
updateFileUploadComplete m rid mime ur =
|
||||||
let
|
let
|
||||||
command = case ur of
|
command = case ur of
|
||||||
Ok u -> sendFileMessage m.apiUrl (Maybe.withDefault "" m.token) m.transactionId rid u
|
Ok u -> sendFileMessage m.apiUrl (Maybe.withDefault "" m.token) m.transactionId rid mime u
|
||||||
_ -> Cmd.none
|
_ -> Cmd.none
|
||||||
in
|
in
|
||||||
({ m | transactionId = m.transactionId + 1}, command)
|
({ m | transactionId = m.transactionId + 1}, command)
|
||||||
|
|
||||||
updateImageUploadComplete : Model -> RoomId -> (Result Http.Error String) -> (Model, Cmd Msg)
|
updateImageUploadComplete : Model -> RoomId -> String -> (Result Http.Error String) -> (Model, Cmd Msg)
|
||||||
updateImageUploadComplete m rid ur =
|
updateImageUploadComplete m rid mime ur =
|
||||||
let
|
let
|
||||||
command = case ur of
|
command = case ur of
|
||||||
Ok u -> sendImageMessage m.apiUrl (Maybe.withDefault "" m.token) m.transactionId rid u
|
Ok u -> sendImageMessage m.apiUrl (Maybe.withDefault "" m.token) m.transactionId rid mime u
|
||||||
_ -> Cmd.none
|
_ -> Cmd.none
|
||||||
in
|
in
|
||||||
({ m | transactionId = m.transactionId + 1}, command)
|
({ m | transactionId = m.transactionId + 1}, command)
|
||||||
|
|
||||||
updateUploadSelected : Model -> RoomId -> File -> List File -> (Result Http.Error String -> Msg) -> (Model, Cmd Msg)
|
updateUploadSelected : Model -> RoomId -> File -> List File -> (String -> Result Http.Error String -> Msg) -> (Model, Cmd Msg)
|
||||||
updateUploadSelected m rid f fs msg =
|
updateUploadSelected m rid f fs msg =
|
||||||
let
|
let
|
||||||
uploadCmds = List.map (uploadMediaFile m.apiUrl (Maybe.withDefault "" m.token) msg) (f::fs)
|
uploadCmds = List.map (uploadMediaFile m.apiUrl (Maybe.withDefault "" m.token) msg) (f::fs)
|
||||||
|
|
|
@ -40,13 +40,13 @@ sync apiUrl token nextBatch = request
|
||||||
, tracker = Nothing
|
, tracker = Nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadMediaFile : ApiUrl -> ApiToken -> (Result Http.Error String -> Msg) -> File -> Cmd Msg
|
uploadMediaFile : ApiUrl -> ApiToken -> (String -> Result Http.Error String -> Msg) -> File -> Cmd Msg
|
||||||
uploadMediaFile apiUrl token msg file = request
|
uploadMediaFile apiUrl token msg file = request
|
||||||
{ method = "POST"
|
{ method = "POST"
|
||||||
, headers = authenticatedHeaders token
|
, headers = authenticatedHeaders token
|
||||||
, url = Builder.crossOrigin (fullMediaUrl apiUrl) [ "upload" ] [ Builder.string "filename" (name file) ]
|
, url = Builder.crossOrigin (fullMediaUrl apiUrl) [ "upload" ] [ Builder.string "filename" (name file) ]
|
||||||
, body = fileBody file
|
, body = fileBody file
|
||||||
, expect = expectJson msg <| Json.Decode.field "content_uri" Json.Decode.string
|
, expect = expectJson (msg <| mime file) <| Json.Decode.field "content_uri" Json.Decode.string
|
||||||
, timeout = Nothing
|
, timeout = Nothing
|
||||||
, tracker = Nothing
|
, tracker = Nothing
|
||||||
}
|
}
|
||||||
|
@ -82,18 +82,20 @@ sendTextMessage apiUrl token transactionId room message = sendMessage apiUrl tok
|
||||||
, ("body", string message)
|
, ("body", string message)
|
||||||
]
|
]
|
||||||
|
|
||||||
sendImageMessage : ApiUrl -> ApiToken -> Int -> RoomId -> String -> Cmd Msg
|
sendImageMessage : ApiUrl -> ApiToken -> Int -> RoomId -> String -> String -> Cmd Msg
|
||||||
sendImageMessage apiUrl token transactionId room message = sendMessage apiUrl token transactionId room SendImageResponse
|
sendImageMessage apiUrl token transactionId room mime message = sendMessage apiUrl token transactionId room SendImageResponse
|
||||||
[ ("msgtype", string "m.image")
|
[ ("msgtype", string "m.image")
|
||||||
, ("body", string "Image")
|
, ("body", string "Image")
|
||||||
, ("url", string message)
|
, ("url", string message)
|
||||||
|
, ("info", object [ ("mimetype", string mime) ])
|
||||||
]
|
]
|
||||||
|
|
||||||
sendFileMessage : ApiUrl -> ApiToken -> Int -> RoomId -> String -> Cmd Msg
|
sendFileMessage : ApiUrl -> ApiToken -> Int -> RoomId -> String -> String -> Cmd Msg
|
||||||
sendFileMessage apiUrl token transactionId room message = sendMessage apiUrl token transactionId room SendFileResponse
|
sendFileMessage apiUrl token transactionId room mime message = sendMessage apiUrl token transactionId room SendFileResponse
|
||||||
[ ("msgtype", string "m.file")
|
[ ("msgtype", string "m.file")
|
||||||
, ("body", string "File")
|
, ("body", string "File")
|
||||||
, ("url", string message)
|
, ("url", string message)
|
||||||
|
, ("info", object [ ("mimetype", string mime) ])
|
||||||
]
|
]
|
||||||
|
|
||||||
login : ApiUrl -> Username -> Password -> Cmd Msg
|
login : ApiUrl -> Username -> Password -> Cmd Msg
|
||||||
|
|
|
@ -57,8 +57,8 @@ type Msg =
|
||||||
| SendFiles RoomId
|
| SendFiles RoomId
|
||||||
| ImagesSelected RoomId File (List File)
|
| ImagesSelected RoomId File (List File)
|
||||||
| FilesSelected RoomId File (List File)
|
| FilesSelected RoomId File (List File)
|
||||||
| ImageUploadComplete RoomId (Result Http.Error String)
|
| ImageUploadComplete RoomId String (Result Http.Error String)
|
||||||
| FileUploadComplete RoomId (Result Http.Error String)
|
| FileUploadComplete RoomId String (Result Http.Error String)
|
||||||
| SendImageResponse (Result Http.Error ())
|
| SendImageResponse (Result Http.Error ())
|
||||||
| SendFileResponse (Result Http.Error ())
|
| SendFileResponse (Result Http.Error ())
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user