Skip to content

Commit e95e389

Browse files
committed
Fix drep image parsing
Fixes #1857
1 parent eca6715 commit e95e389

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

cardano-db-sync/src/Cardano/DbSync/OffChain.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ fetchOffChainVoteData gateways time oVoteWorkQ =
323323
, DB.offChainVoteDrepDataObjectives = Vote.textValue <$> Vote.objectives (Vote.body dt)
324324
, DB.offChainVoteDrepDataMotivations = Vote.textValue <$> Vote.motivations (Vote.body dt)
325325
, DB.offChainVoteDrepDataQualifications = Vote.textValue <$> Vote.qualifications (Vote.body dt)
326-
, DB.offChainVoteDrepDataImageUrl = Vote.textValue . Vote.contentUrl <$> Vote.image (Vote.body dt)
327-
, DB.offChainVoteDrepDataImageHash = Vote.textValue . Vote.sha256 <$> Vote.image (Vote.body dt)
326+
, DB.offChainVoteDrepDataImageUrl = Vote.textValue . Vote.content <$> Vote.image (Vote.body dt)
327+
, DB.offChainVoteDrepDataImageHash = Vote.textValue <$> (Vote.msha256 =<< Vote.image (Vote.body dt))
328328
}
329329
_ -> Nothing
330330

cardano-db-sync/src/Cardano/DbSync/OffChain/Vote/Types.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,20 @@ data DrepBody = DrepBody
171171
deriving (Show, Generic)
172172

173173
data Image = Image
174+
{ content :: TextValue
175+
, msha256 :: Maybe TextValue
176+
}
177+
deriving (Show, Generic)
178+
179+
data ImageUrl = ImageUrl
174180
{ contentUrl :: TextValue
175181
, sha256 :: TextValue
176182
}
177183
deriving (Show, Generic, FromJSON)
178184

185+
fromImageUrl :: ImageUrl -> Image
186+
fromImageUrl img = Image (contentUrl img) (Just (sha256 img))
187+
179188
data Reference tp = Reference
180189
{ rtype :: TextValue -- key is @type. It can be "GovernanceMetadata" or "Other" or ?? "other" ?? or ""
181190
, label :: TextValue
@@ -297,6 +306,20 @@ instance FromJSON DrepBody where
297306
where
298307
withObjectV v' s p = withObject s p v'
299308

309+
instance FromJSON Image where
310+
parseJSON v = withObjectV v "Image" $ \o -> do
311+
curl <- o .: "contentUrl"
312+
case Text.stripPrefix "data:" (textValue curl) of
313+
Just ctb
314+
| (_, tb) <- Text.break (== '/') ctb
315+
, Text.isPrefixOf "/" tb
316+
, (_, b) <- Text.break (== ';') tb
317+
, Just imageData <- Text.stripPrefix ";base64," b ->
318+
pure $ Image (TextValue imageData) Nothing
319+
_ -> fromImageUrl <$> parseJSON v
320+
where
321+
withObjectV v' s p = withObject s p v'
322+
300323
parseTextLimit :: Int -> Key -> Object -> Parser TextValue
301324
parseTextLimit maxSize str o = do
302325
txt <- o .: str

0 commit comments

Comments
 (0)