From 10c29b71808b80d74e71b13ca693bd37de1ec90a Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Tue, 30 May 2023 11:58:11 +0300 Subject: [PATCH] Allow passing fileName in the StoreFileOptions --- IHP/FileStorage/ControllerFunctions.hs | 4 +++- IHP/FileStorage/Types.hs | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/IHP/FileStorage/ControllerFunctions.hs b/IHP/FileStorage/ControllerFunctions.hs index 5e495ee8b..5ce2d7c78 100644 --- a/IHP/FileStorage/ControllerFunctions.hs +++ b/IHP/FileStorage/ControllerFunctions.hs @@ -91,8 +91,10 @@ storeFileWithOptions :: (?context :: context, ConfigProvider context) => Wai.Fil storeFileWithOptions fileInfo options = do objectId <- UUID.nextRandom + let fileName = options.fileName |> fromMaybe objectId + let directory = options.directory - let objectPath = directory <> "/" <> UUID.toText objectId + let objectPath = directory <> "/" <> UUID.toText fileName let preprocess = options.preprocess fileInfo <- preprocess fileInfo diff --git a/IHP/FileStorage/Types.hs b/IHP/FileStorage/Types.hs index 47218d75b..defd24c3a 100644 --- a/IHP/FileStorage/Types.hs +++ b/IHP/FileStorage/Types.hs @@ -25,6 +25,7 @@ data StoreFileOptions = StoreFileOptions { directory :: Text -- ^ Directory on S3 or inside the @static@ where all files are placed , contentDisposition :: Wai.FileInfo LByteString -> IO (Maybe Text) -- ^ The browser uses the content disposition header to detect if the file should be shown inside the browser or should be downloaded as a file attachment. You can provide a function here that returns a custom content-disposition header based on the uploaded file. This currently only works with the S3 storage. See 'contentDispositionAttachmentAndFileName' for standard configuration. , preprocess :: Wai.FileInfo LByteString -> IO (Wai.FileInfo LByteString) -- ^ Can be used to preprocess the file before storing it inside the storage. See 'applyImageMagick' for preprocessing images. + , fileName :: Maybe UUID -- ^ Optional filename. We use UUID for security measures. } instance Default StoreFileOptions where @@ -32,6 +33,7 @@ instance Default StoreFileOptions where { directory = "" , contentDisposition = const (pure Nothing) , preprocess = pure + , fileName = Nothing } -- | A signed url to a file. See 'createTemporaryDownloadUrl'