Skip to content

Commit

Permalink
Export uploadToStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
s0kil committed Jul 24, 2021
1 parent 9dd2297 commit ed8d5e1
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions IHP/FileStorage/ControllerFunctions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module IHP.FileStorage.ControllerFunctions
, contentDispositionAttachmentAndFileName
, createTemporaryDownloadUrl
, createTemporaryDownloadUrlFromPath
, uploadToStorage
, uploadToStorageWithOptions
) where

Expand Down Expand Up @@ -92,7 +93,7 @@ storeFile fileInfo directory = storeFileWithOptions fileInfo (def { directory })
storeFileWithOptions :: (?context :: context, ConfigProvider context) => Wai.FileInfo LByteString -> StoreFileOptions -> IO StoredFile
storeFileWithOptions fileInfo options = do
objectId <- UUID.nextRandom

let directory = get #directory options
let objectPath = directory <> "/" <> UUID.toText objectId
let preprocess = get #preprocess options
Expand Down Expand Up @@ -122,7 +123,7 @@ storeFileWithOptions fileInfo options = do
putObject bucket objectPath payload Nothing options

pure $ baseUrl <> objectPath

pure StoredFile { path = objectPath, url }

-- | Fetchs an url and uploads it to the storage.
Expand Down Expand Up @@ -208,7 +209,7 @@ createTemporaryDownloadUrlFromPath objectPath = do

pure TemporaryDownloadUrl { url = cs url, expiredAt = publicUrlExpiredAt }
S3Storage { connectInfo, bucket} -> do

url <- runMinio connectInfo do
presignedGetObjectUrl bucket objectPath validInSeconds [] []

Expand Down Expand Up @@ -241,11 +242,11 @@ contentDispositionAttachmentAndFileName fileInfo = pure (Just ("attachment; file
-- | Saves an upload to the storage and sets the record attribute to the url.
--
-- __Example:__ Upload a logo for a Company and convert it to a 512x512 PNG
--
--
-- > action UpdateCompanyAction { companyId } = do
-- > let uploadLogo = uploadToStorageWithOptions $ def
-- > { preprocess = applyImageMagick "png" "-resize '512x512^' -gravity north -extent 512x512 -quality 100% -strip" }
-- >
-- >
-- > company <- fetch companyId
-- > company
-- > |> fill @'["name"]
Expand Down Expand Up @@ -293,7 +294,7 @@ uploadToStorageWithOptions options field record = do
-- See 'uploadToStorageWithOptions' if you want to provide custom options.
--
-- __Example:__ Upload a logo for a Company
--
--
-- > action UpdateCompanyAction { companyId } = do
-- > company <- fetch companyId
-- > company
Expand Down Expand Up @@ -347,4 +348,4 @@ storage :: (?context :: context, ConfigProvider context) => FileStorage
storage = getFrameworkConfig ?context
|> get #appConfig
|> TMap.lookup @FileStorage
|> fromMaybe (error "Could not find FileStorage in config. Did you call initS3Storage from your Config.hs?")
|> fromMaybe (error "Could not find FileStorage in config. Did you call initS3Storage from your Config.hs?")

0 comments on commit ed8d5e1

Please sign in to comment.