Copyright(c) digitally induced GmbH 2020
Safe HaskellNone

IHP.Controller.FileUpload

Description

This modules provides high-level file and image upload functionality.

All uploaded files are saved to the uploads directory. Given e.g. an User entity with id = 550e8400-e29b-11d4-a716-446655440000, the file is saved to /uploads/users/550e8400-e29b-11d4-a716-446655440000/picture.jpg. If the directory does not exists, it will be created.

Synopsis

Documentation

fileOrNothing :: (?requestContext :: RequestContext) => ByteString -> Maybe (FileInfo ByteString) Source #

Returns a file upload from the request as a ByteString.

Returns Nothing when the file is not found in the request body.

data ImageUploadOptions Source #

Options to be used together with uploadImageWithOptions

Example:

ImageUploadOptions { convertTo = "jpg", imageMagickOptions = "-resize '1024x1024^' -gravity north -extent 1024x1024 -quality 85% -strip" }

Constructors

ImageUploadOptions 

Fields

  • convertTo :: Text

    The file extension to be used when saving the file, e.g. "jpg" or "png".

  • imageMagickOptions :: Text

    Command line options passed to imagemagick. Can used for e.g. resizing, rotating, file size reduction.

uploadImageWithOptions :: forall (fieldName :: Symbol) context record (tableName :: Symbol). (?requestContext :: RequestContext, ?controllerContext :: context, SetField fieldName record (Maybe Text), KnownSymbol fieldName, HasField "id" record (Id (NormalizeModel record)), Show (PrimaryKey (GetTableName (NormalizeModel record))), tableName ~ GetTableName record, KnownSymbol tableName) => ImageUploadOptions -> Proxy fieldName -> record -> IO record Source #

Saves an uploaded image file to the uploads directory and writes the relative path to the given record attribute.

Given e.g. an User entity with id = 550e8400-e29b-11d4-a716-446655440000, the file is saved to /uploads/users/550e8400-e29b-11d4-a716-446655440000/picture.jpg.

Before saving, the image is converted using imagemagick. You can supply custom image magick options using the options attribute.

If the upload directory does not exists, it will be created.

Example: Uploading a user profile picture

let profilePictureOptions = ImageUploadOptions
        { convertTo = "jpg"
        , imageMagickOptions = "-resize '1024x1024^' -gravity north -extent 1024x1024 -quality 85% -strip"
        }

user
    |> fill @["firstname", "lastname", "pictureUrl"]
    |> uploadImageWithOptions profilePictureOptions #pictureUrl
    >>= ifValid \case
        Left user -> render EditView { .. }
        Right user -> do
            user <- user |> updateRecord
            redirectTo EditUserAction { .. }

The uploaded image path is now stored in #pictureUrl.

uploadImageFile :: forall (fieldName :: Symbol) context record (tableName :: Symbol). (?requestContext :: RequestContext, ?controllerContext :: context, SetField fieldName record (Maybe Text), KnownSymbol fieldName, HasField "id" record (Id (NormalizeModel record)), Show (PrimaryKey (GetTableName (NormalizeModel record))), tableName ~ GetTableName record, KnownSymbol tableName) => Text -> Proxy fieldName -> record -> IO record Source #

Saves an uploaded image file to the uploads directory.

Given e.g. an User entity with id = 550e8400-e29b-11d4-a716-446655440000, the file is saved to /uploads/users/550e8400-e29b-11d4-a716-446655440000/picture.jpg.

No transformation or validation is applied to the given uploaded file. If you need this, take a look at uploadImageWithOptions.

Example: Uploading a user profile picture

let profilePictureOptions = ImageUploadOptions
        { convertTo = "jpg"
        , imageMagickOptions = "-resize '1024x1024^' -gravity north -extent 1024x1024 -quality 85% -strip"
        }

user
    |> fill @["firstname", "lastname", "pictureUrl"]
    |> uploadImageFile "png" #pictureUrl
    >>= ifValid \case
        Left user -> render EditView { .. }
        Right user -> do
            user <- user |> updateRecord
            redirectTo EditUserAction { .. }

uploadPng :: _ => Proxy fieldName -> record -> IO record Source #

Saves an uploaded png file. No validation or transformation applied. See uploadImageFile for details.

uploadSVG :: _ => Proxy fieldName -> record -> IO record Source #

Saves an uploaded svg file. No validation or transformation applied. See uploadImageFile for details.