From 15510ccaadb9579aa2bd75f7b8af7c1137fd77f5 Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Tue, 17 Sep 2024 21:42:55 +0300 Subject: [PATCH] Fix drep image parsing Fixes https://github.com/IntersectMBO/cardano-db-sync/issues/1857 --- .../src/Cardano/DbSync/OffChain.hs | 4 ++-- .../src/Cardano/DbSync/OffChain/Vote/Types.hs | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/cardano-db-sync/src/Cardano/DbSync/OffChain.hs b/cardano-db-sync/src/Cardano/DbSync/OffChain.hs index 542982f14..b89201791 100644 --- a/cardano-db-sync/src/Cardano/DbSync/OffChain.hs +++ b/cardano-db-sync/src/Cardano/DbSync/OffChain.hs @@ -323,8 +323,8 @@ fetchOffChainVoteData gateways time oVoteWorkQ = , DB.offChainVoteDrepDataObjectives = Vote.textValue <$> Vote.objectives (Vote.body dt) , DB.offChainVoteDrepDataMotivations = Vote.textValue <$> Vote.motivations (Vote.body dt) , DB.offChainVoteDrepDataQualifications = Vote.textValue <$> Vote.qualifications (Vote.body dt) - , DB.offChainVoteDrepDataImageUrl = Vote.textValue . Vote.contentUrl <$> Vote.image (Vote.body dt) - , DB.offChainVoteDrepDataImageHash = Vote.textValue . Vote.sha256 <$> Vote.image (Vote.body dt) + , DB.offChainVoteDrepDataImageUrl = Vote.textValue . Vote.content <$> Vote.image (Vote.body dt) + , DB.offChainVoteDrepDataImageHash = Vote.textValue <$> (Vote.msha256 =<< Vote.image (Vote.body dt)) } _ -> Nothing diff --git a/cardano-db-sync/src/Cardano/DbSync/OffChain/Vote/Types.hs b/cardano-db-sync/src/Cardano/DbSync/OffChain/Vote/Types.hs index 287894027..964edec0e 100644 --- a/cardano-db-sync/src/Cardano/DbSync/OffChain/Vote/Types.hs +++ b/cardano-db-sync/src/Cardano/DbSync/OffChain/Vote/Types.hs @@ -171,11 +171,20 @@ data DrepBody = DrepBody deriving (Show, Generic) data Image = Image + { content :: TextValue + , msha256 :: Maybe TextValue + } + deriving (Show, Generic) + +data ImageUrl = ImageUrl { contentUrl :: TextValue , sha256 :: TextValue } deriving (Show, Generic, FromJSON) +fromImageUrl :: ImageUrl -> Image +fromImageUrl img = Image (contentUrl img) (Just (sha256 img)) + data Reference tp = Reference { rtype :: TextValue -- key is @type. It can be "GovernanceMetadata" or "Other" or ?? "other" ?? or "" , label :: TextValue @@ -297,6 +306,20 @@ instance FromJSON DrepBody where where withObjectV v' s p = withObject s p v' +instance FromJSON Image where + parseJSON v = withObjectV v "Image" $ \o -> do + curl <- o .: "contentUrl" + case Text.stripPrefix "data:" (textValue curl) of + Just ctb + | (_, tb) <- Text.break (== '/') ctb + , Text.isPrefixOf "/" tb + , (_, b) <- Text.break (== ';') tb + , Just imageData <- Text.stripPrefix ";base64," b -> + pure $ Image (TextValue imageData) Nothing + _ -> fromImageUrl <$> parseJSON v + where + withObjectV v' s p = withObject s p v' + parseTextLimit :: Int -> Key -> Object -> Parser TextValue parseTextLimit maxSize str o = do txt <- o .: str