From adcf7ca3cc6fcd1a98a54192905030d0fa7177d6 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Tue, 23 Nov 2021 14:09:11 -0700 Subject: [PATCH] sends 404s if we can't find the package rather than just blowing up and going 500 --- src/Handler/Apps.hs | 4 +++- src/Lib/PkgRepository.hs | 13 +++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Handler/Apps.hs b/src/Handler/Apps.hs index 304bafa..7267d44 100644 --- a/src/Handler/Apps.hs +++ b/src/Handler/Apps.hs @@ -75,7 +75,9 @@ getAppR file = do `orThrow` sendResponseStatus status404 (NotFoundE [i|#{pkg} satisfying #{versionSpec}|]) addPackageHeader pkg version void $ recordMetrics pkg version - (len, src) <- getPackage pkg version + (len, src) <- getPackage pkg version >>= \case + Nothing -> sendResponseStatus status404 ([i|#{pkg}@#{version} not found|] :: Text) + Just a -> pure a addHeader "Content-Length" (show len) respondSource typeOctet $ src .| awaitForever sendChunkBS diff --git a/src/Lib/PkgRepository.hs b/src/Lib/PkgRepository.hs index f5debcd..56a7251 100644 --- a/src/Lib/PkgRepository.hs +++ b/src/Lib/PkgRepository.hs @@ -96,7 +96,8 @@ import UnliftIO ( MonadUnliftIO ) import UnliftIO ( tryPutMVar ) import UnliftIO.Concurrent ( forkIO ) -import UnliftIO.Directory ( getFileSize +import UnliftIO.Directory ( doesPathExist + , getFileSize , listDirectory , removeFile , renameFile @@ -257,9 +258,13 @@ getHash pkg version = do getPackage :: (MonadResource m, MonadReader r m, Has PkgRepo r) => PkgId -> Version - -> m (Integer, ConduitT () ByteString m ()) + -> m (Maybe (Integer, ConduitT () ByteString m ())) getPackage pkg version = do root <- asks pkgRepoFileRoot let pkgPath = root show pkg show version show pkg <.> "s9pk" - n <- getFileSize pkgPath - pure (n, sourceFile pkgPath) + found <- doesPathExist pkgPath + if found + then do + n <- getFileSize pkgPath + pure . Just $ (n, sourceFile pkgPath) + else pure Nothing