Skip to content

Commit

Permalink
Prefix objectPath with slash
Browse files Browse the repository at this point in the history
  • Loading branch information
amitaibu committed Oct 7, 2024
1 parent 4241679 commit adfaf3c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions IHP/FileStorage/ControllerFunctions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ storeFileWithOptions fileInfo options = do
|> LBS.writeFile (cs destPath)

let frameworkConfig = ?context.frameworkConfig
pure $ objectPath
-- Prefix with a slash so it can be used in URLs, even if the baseUrl is empty.
pure $ "/" <> objectPath
S3Storage { connectInfo, bucket, baseUrl } -> do
let payload = fileInfo
|> (.fileContent)
Expand Down Expand Up @@ -231,8 +232,8 @@ createTemporaryDownloadUrlFromPathWithExpiredAt validInSeconds objectPath = do
let url = if any (`isPrefixOf` objectPath) urlSchemes
-- BC, before we saved only the relative path of a file, we saved the full URL. So use it as is.
then objectPath
-- We have the relative path, so add the baseUrl.
else frameworkConfig.baseUrl <> "/" <> objectPath
-- We have the relative path (prefixed with slash), so add the baseUrl.
else frameworkConfig.baseUrl <> objectPath

pure TemporaryDownloadUrl { url = cs url, expiredAt = publicUrlExpiredAt }
S3Storage { connectInfo, bucket} -> do
Expand Down
4 changes: 2 additions & 2 deletions Test/FileStorage/ControllerFunctionsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ tests = describe "IHP.FileStorage.ControllerFunctions" $ do

result <- storeFileWithOptions fileInfo options

result.url `shouldBe` ("Test.FileStorage.ControllerFunctionsSpec/4c55dac2-e411-45ac-aa10-b957b01221df")
result.url `shouldBe` ("/Test.FileStorage.ControllerFunctionsSpec/4c55dac2-e411-45ac-aa10-b957b01221df")

describe "createTemporaryDownloadUrlFromPath" $ do
it "returns baseUrl concatenated with objectPath when objectPath does not start with http:// or https://" $ do
withFrameworkConfig \frameworkConfig -> do
context <- createControllerContext frameworkConfig
let ?context = context
let objectPath = "static/test.txt"
let objectPath = "/static/test.txt"
temporaryDownloadUrl <- createTemporaryDownloadUrlFromPath objectPath

temporaryDownloadUrl.url `shouldBe` "http://localhost:8000/static/test.txt"
Expand Down

0 comments on commit adfaf3c

Please sign in to comment.