Add mime type info to file and image uploads.

This commit is contained in:
Danila Fedorin 2018-12-20 20:13:09 -08:00
parent 03c472a78d
commit c483e6ac6c
3 changed files with 19 additions and 17 deletions

View File

@ -86,30 +86,30 @@ update msg model = case msg of
SendFiles rid -> (model, Select.files [ "application/*" ] <| FilesSelected rid)
ImagesSelected rid f fs -> updateUploadSelected model rid f fs (ImageUploadComplete rid)
FilesSelected rid f fs -> updateUploadSelected model rid f fs (FileUploadComplete rid)
ImageUploadComplete rid ur -> updateImageUploadComplete model rid ur
FileUploadComplete rid ur -> updateFileUploadComplete model rid ur
ImageUploadComplete rid mime ur -> updateImageUploadComplete model rid mime ur
FileUploadComplete rid mime ur -> updateFileUploadComplete model rid mime ur
SendImageResponse _ -> (model, Cmd.none)
SendFileResponse _ -> (model, Cmd.none)
updateFileUploadComplete : Model -> RoomId -> (Result Http.Error String) -> (Model, Cmd Msg)
updateFileUploadComplete m rid ur =
updateFileUploadComplete : Model -> RoomId -> String -> (Result Http.Error String) -> (Model, Cmd Msg)
updateFileUploadComplete m rid mime ur =
let
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
in
({ m | transactionId = m.transactionId + 1}, command)
updateImageUploadComplete : Model -> RoomId -> (Result Http.Error String) -> (Model, Cmd Msg)
updateImageUploadComplete m rid ur =
updateImageUploadComplete : Model -> RoomId -> String -> (Result Http.Error String) -> (Model, Cmd Msg)
updateImageUploadComplete m rid mime ur =
let
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
in
({ 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 =
let
uploadCmds = List.map (uploadMediaFile m.apiUrl (Maybe.withDefault "" m.token) msg) (f::fs)

View File

@ -40,13 +40,13 @@ sync apiUrl token nextBatch = request
, 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
{ method = "POST"
, headers = authenticatedHeaders token
, url = Builder.crossOrigin (fullMediaUrl apiUrl) [ "upload" ] [ Builder.string "filename" (name 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
, tracker = Nothing
}
@ -82,18 +82,20 @@ sendTextMessage apiUrl token transactionId room message = sendMessage apiUrl tok
, ("body", string message)
]
sendImageMessage : ApiUrl -> ApiToken -> Int -> RoomId -> String -> Cmd Msg
sendImageMessage apiUrl token transactionId room message = sendMessage apiUrl token transactionId room SendImageResponse
sendImageMessage : ApiUrl -> ApiToken -> Int -> RoomId -> String -> String -> Cmd Msg
sendImageMessage apiUrl token transactionId room mime message = sendMessage apiUrl token transactionId room SendImageResponse
[ ("msgtype", string "m.image")
, ("body", string "Image")
, ("url", string message)
, ("info", object [ ("mimetype", string mime) ])
]
sendFileMessage : ApiUrl -> ApiToken -> Int -> RoomId -> String -> Cmd Msg
sendFileMessage apiUrl token transactionId room message = sendMessage apiUrl token transactionId room SendFileResponse
sendFileMessage : ApiUrl -> ApiToken -> Int -> RoomId -> String -> String -> Cmd Msg
sendFileMessage apiUrl token transactionId room mime message = sendMessage apiUrl token transactionId room SendFileResponse
[ ("msgtype", string "m.file")
, ("body", string "File")
, ("url", string message)
, ("info", object [ ("mimetype", string mime) ])
]
login : ApiUrl -> Username -> Password -> Cmd Msg

View File

@ -57,8 +57,8 @@ type Msg =
| SendFiles RoomId
| ImagesSelected RoomId File (List File)
| FilesSelected RoomId File (List File)
| ImageUploadComplete RoomId (Result Http.Error String)
| FileUploadComplete RoomId (Result Http.Error String)
| ImageUploadComplete RoomId String (Result Http.Error String)
| FileUploadComplete RoomId String (Result Http.Error String)
| SendImageResponse (Result Http.Error ())
| SendFileResponse (Result Http.Error ())