diff --git a/src/Simplex/Chat/Web.hs b/src/Simplex/Chat/Web.hs index 30d674d55f..80f7432da2 100644 --- a/src/Simplex/Chat/Web.hs +++ b/src/Simplex/Chat/Web.hs @@ -16,6 +16,7 @@ module Simplex.Chat.Web webPreviewWorker, writeCorsConfig, removeStaleFiles, + publicGroupIdFileName, channelContentChanged, channelProfileUpdated, channelRemoved, @@ -416,9 +417,11 @@ removeStaleFiles dir activeFiles = do let f' = if takeExtension f == ".tmp" then dropExtension f else f base = dropExtension f' in takeExtension f' == ".json" && not (null base) && all isBase64Url base - isBase64Url c = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_' + isBase64Url c = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_' || c == '=' allFiles <- S.filter isPreviewFile . S.fromList <$> listDirectory dir - mapM_ (\f -> removeFile (dir f)) $ S.difference allFiles activeFiles + forM_ (S.difference allFiles activeFiles) $ \f -> + removeFile (dir f) `catchOwn'` \(e :: SomeException) -> + logError $ "web preview: error removing stale file " <> T.pack f <> ": " <> tshow e toFormattedText :: Text -> Maybe MarkdownList toFormattedText t = case parseMaybeMarkdownList t of diff --git a/tests/ChatTests/ChatRelays.hs b/tests/ChatTests/ChatRelays.hs index a30546ed91..a555fcc9ef 100644 --- a/tests/ChatTests/ChatRelays.hs +++ b/tests/ChatTests/ChatRelays.hs @@ -19,9 +19,10 @@ import qualified Data.Text as T import ProtocolTests (testGroupProfile) import Simplex.Chat.Controller (ChatConfig (..)) import Simplex.Chat.Protocol (LinkOwnerSig, MsgChatLink (..), MsgContent (..)) -import Simplex.Chat.Types (GroupProfile (..)) +import Simplex.Chat.Types (B64UrlByteString (..), GroupProfile (..)) import Simplex.Chat.Controller (CorsOrigin (..)) -import Simplex.Chat.Web (WebChannelPreview (..), WebMessage (..), extractOrigin, removeStaleFiles, writeCorsConfig) +import Simplex.Chat.Web (WebChannelPreview (..), WebMessage (..), extractOrigin, publicGroupIdFileName, removeStaleFiles, writeCorsConfig) +import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Crypto.BBS (bbsKeyGen) import Simplex.Messaging.Encoding.String (StrEncoding (..)) import Simplex.Messaging.Util (decodeJSON) @@ -569,8 +570,9 @@ testWebPreviewChannelDeleted ps = testWebPreviewStaleCleanup :: HasCallStack => TestParams -> IO () testWebPreviewStaleCleanup ps = do let webDir = tmpPath ps "web_stale_unit" - activeFile = "abc123.json" - staleFile = "AAAA_stale.json" + previewFileName s = publicGroupIdFileName (B64UrlByteString $ C.sha256Hash s) <> ".json" + activeFile = previewFileName "active" + staleFile = previewFileName "stale" safeFile = "my.config.json" createDirectoryIfMissing True webDir writeFile (webDir activeFile) "{}"