Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve file storage docs #1876

Merged
merged 3 commits into from
Dec 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Guide/file-storage.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ Our Companies controller will now change to:

```haskell
-- Web/Controller/Companies.hs
-- ...
import qualified Network.Wai.Parse as Wai

action CreateCompanyAction = do
-- Upload file. If no file provided, we error and short-circuit.
let file = fileOrNothing "uploadedFile" |> fromMaybe (error "no file given")
Expand Down Expand Up @@ -539,11 +542,26 @@ Our Companies controller will now change to:
redirectTo ShowCompanyAction { companyId = company.id }
```

Your HTML form should now have a file field as part of the HTML.

```haskell
renderForm :: Company -> Html
renderForm company = formFor company [hsx|
{(textField #name)}

<h2>File upload</h2>
<input type="file" name="uploadedFile" />

{submitButton}
|]
```

Our Show action can now change to the following code, that will get the `UploadedFile` out of the company,
and ensure we have an up-to-date signed URL.

```haskell
-- Web/Controller/Companies.hs
import IHP.FileStorage.ControllerFunctions
action ShowCompanyAction { companyId } = do
company <- fetch companyId

Expand All @@ -561,6 +579,11 @@ The company show would now receive the following records:
```haskell
-- Web/View/Companies/Show.hs
data ShowView = ShowView { company :: Company, uploadedFile :: UploadedFile }

instance View ShowView where
html ShowView { .. } = [hsx|
<a href={uploadedFile.signedUrl}>{uploadedFile.fileName}</a>
|]
```

### Image Preprocessing
Expand Down
Loading