Skip to content

Commit

Permalink
Fix drep image parsing
Browse files Browse the repository at this point in the history
Fixes #1857
  • Loading branch information
kderme committed Oct 4, 2024
1 parent 9ca4d54 commit 15510cc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cardano-db-sync/src/Cardano/DbSync/OffChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions cardano-db-sync/src/Cardano/DbSync/OffChain/Vote/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 15510cc

Please sign in to comment.