Skip to content

Commit

Permalink
custom storage upload directory via IHP_STORAGE_DIR env var
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Nov 19, 2024
1 parent c4307ac commit 2c23415
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
4 changes: 3 additions & 1 deletion IHP/FileStorage/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ initMinioStorage server bucket = do
-- > initStaticDirStorage
--
initStaticDirStorage :: State.StateT TMap.TMap IO ()
initStaticDirStorage = option StaticDirStorage
initStaticDirStorage = do
directory <- EnvVar.envOrDefault "IHP_STORAGE_DIR" "static/"
option StaticDirStorage { directory }

-- | The Filebase access key and secret key have to be provided using the @FILEBASE_KEY@ and @FILEBASE_SECRET@ env vars.
--
Expand Down
17 changes: 8 additions & 9 deletions IHP/FileStorage/ControllerFunctions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,15 @@ storeFileWithOptions fileInfo options = do

let fileName = options.fileName |> fromMaybe objectId

let directory = options.directory
let objectPath = directory <> "/" <> UUID.toText fileName
let objectPath = options.directory <> "/" <> UUID.toText fileName
let preprocess = options.preprocess

fileInfo <- preprocess fileInfo

url <- case storage of
StaticDirStorage -> do
let destPath :: Text = "static/" <> objectPath
Directory.createDirectoryIfMissing True (cs $ "static/" <> directory)
StaticDirStorage { directory } -> do
let destPath :: Text = directory <> objectPath
Directory.createDirectoryIfMissing True (cs $ directory <> options.directory)

fileInfo
|> (.fileContent)
Expand Down Expand Up @@ -225,7 +224,7 @@ createTemporaryDownloadUrlFromPathWithExpiredAt :: (?context :: context, ConfigP
createTemporaryDownloadUrlFromPathWithExpiredAt validInSeconds objectPath = do
publicUrlExpiredAt <- addUTCTime (fromIntegral validInSeconds) <$> getCurrentTime
case storage of
StaticDirStorage -> do
StaticDirStorage {} -> do
let frameworkConfig = ?context.frameworkConfig
let urlSchemes = ["http://", "https://"]

Expand Down Expand Up @@ -398,8 +397,8 @@ uploadToStorage field record = uploadToStorageWithOptions def field record
removeFileFromStorage :: (?context :: context, ConfigProvider context) => StoredFile -> IO (Either MinioErr ())
removeFileFromStorage StoredFile { path, url } = do
case storage of
StaticDirStorage -> do
let fullPath :: String = cs $ "static/" <> path
StaticDirStorage { directory } -> do
let fullPath :: String = cs $ directory <> path
Directory.removeFile fullPath
pure $ Right ()
S3Storage { connectInfo, bucket} -> do
Expand All @@ -415,5 +414,5 @@ storage = ?context.frameworkConfig.appConfig
-- | Returns the prefix for the storage. This is either @static/@ or an empty string depending on the storage.
storagePrefix :: (?context :: ControllerContext) => Text
storagePrefix = case storage of
StaticDirStorage -> "static/"
StaticDirStorage { directory } -> directory
_ -> ""
2 changes: 1 addition & 1 deletion IHP/FileStorage/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import qualified Network.Minio as Minio
import qualified Network.Wai.Parse as Wai

data FileStorage
= StaticDirStorage -- ^ Stores files publicly visible inside the project's @static@ directory
= StaticDirStorage { directory :: !Text } -- ^ Stores files publicly visible inside the project's @static@ directory
| S3Storage { connectInfo :: Minio.ConnectInfo, bucket :: Text, baseUrl :: Text } -- ^ Stores files inside a S3 compatible cloud storage

-- | Result of a 'storeFile' operation
Expand Down

0 comments on commit 2c23415

Please sign in to comment.