From adfaf3c28ae352df0e7cbd9ae3e8e65e7279dbd4 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Mon, 7 Oct 2024 09:29:44 +0300 Subject: [PATCH] Prefix objectPath with slash --- IHP/FileStorage/ControllerFunctions.hs | 7 ++++--- Test/FileStorage/ControllerFunctionsSpec.hs | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/IHP/FileStorage/ControllerFunctions.hs b/IHP/FileStorage/ControllerFunctions.hs index 9ae9dab88..cd9901ea8 100644 --- a/IHP/FileStorage/ControllerFunctions.hs +++ b/IHP/FileStorage/ControllerFunctions.hs @@ -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) @@ -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 diff --git a/Test/FileStorage/ControllerFunctionsSpec.hs b/Test/FileStorage/ControllerFunctionsSpec.hs index 70768db4d..02f6892a0 100644 --- a/Test/FileStorage/ControllerFunctionsSpec.hs +++ b/Test/FileStorage/ControllerFunctionsSpec.hs @@ -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"