Skip to content

Commit f4c2d07

Browse files
committed
1966 - offchain image metadata
1 parent b3fcefd commit f4c2d07

File tree

5 files changed

+80
-3
lines changed

5 files changed

+80
-3
lines changed

cardano-db-sync/cardano-db-sync.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ test-suite test
334334
Cardano.DbSync.Era.Shelley.Generic.ScriptDataTest
335335
Cardano.DbSync.Era.Shelley.Generic.ScriptTest
336336
Cardano.DbSync.Gen
337+
Cardano.DbSync.OffChain.Vote.TypesTest
337338
Cardano.DbSync.Util.AddressTest
338339
Cardano.DbSync.Util.Bech32Test
339340
Cardano.DbSync.Util.CborTest

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,9 @@ instance FromJSON Image where
284284
| (_, tb) <- Text.break (== '/') ctb
285285
, Text.isPrefixOf "/" tb
286286
, (_, b) <- Text.break (== ';') tb
287-
, Just imageData <- Text.stripPrefix ";base64," b ->
288-
pure $ Image (TextValue imageData) Nothing
287+
, Just _ <- Text.stripPrefix ";base64," b ->
288+
-- Store the full data URI including prefix
289+
pure $ Image curl Nothing
289290
_ -> fromImageUrl <$> parseJSON v
290291
where
291292
withObjectV v' s p = withObject s p v'
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE ScopedTypeVariables #-}
3+
4+
module Cardano.DbSync.OffChain.Vote.TypesTest (tests) where
5+
6+
import Cardano.DbSync.OffChain.Vote.Types
7+
import Cardano.Prelude
8+
import qualified Data.Aeson as Aeson
9+
import qualified Data.ByteString.Lazy as LBS
10+
import qualified Data.Text as Text
11+
import Hedgehog
12+
import qualified Hedgehog as H
13+
import Prelude ()
14+
15+
tests :: IO Bool
16+
tests =
17+
checkParallel $
18+
Group
19+
"Cardano.DbSync.OffChain.Vote.Types"
20+
[ ("Image preserves data URI prefix", prop_image_preserves_data_uri_prefix)
21+
, ("Image handles URL with hash", prop_image_handles_url_with_hash)
22+
, ("Image preserves JPEG data URI", prop_image_preserves_jpeg_data_uri)
23+
]
24+
25+
prop_image_preserves_data_uri_prefix :: Property
26+
prop_image_preserves_data_uri_prefix = property $ do
27+
let jsonWithDataUri =
28+
LBS.fromStrict $
29+
encodeUtf8
30+
"{ \"contentUrl\": \"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA\" }"
31+
32+
case Aeson.eitherDecode jsonWithDataUri of
33+
Left err -> do
34+
H.footnote $ "Parse failed: " <> err
35+
H.failure
36+
Right (img :: Image) -> do
37+
let imgContent = textValue $ content img
38+
H.assert $ "data:" `Text.isPrefixOf` imgContent
39+
H.assert $ "image/png" `Text.isInfixOf` imgContent
40+
H.assert $ "base64" `Text.isInfixOf` imgContent
41+
imgContent === "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA"
42+
43+
prop_image_handles_url_with_hash :: Property
44+
prop_image_handles_url_with_hash = property $ do
45+
let jsonWithUrl =
46+
LBS.fromStrict $
47+
encodeUtf8
48+
"{ \"contentUrl\": \"https://example.com/image.png\", \"sha256\": \"abc123\" }"
49+
50+
case Aeson.eitherDecode jsonWithUrl of
51+
Left err -> do
52+
H.footnote $ "Parse failed: " <> err
53+
H.failure
54+
Right (img :: Image) -> do
55+
textValue (content img) === "https://example.com/image.png"
56+
(textValue <$> msha256 img) === Just "abc123"
57+
58+
prop_image_preserves_jpeg_data_uri :: Property
59+
prop_image_preserves_jpeg_data_uri = property $ do
60+
let jsonWithJpeg =
61+
LBS.fromStrict $
62+
encodeUtf8
63+
"{ \"contentUrl\": \"data:image/jpeg;base64,/9j/4AAQSkZJRg\" }"
64+
65+
case Aeson.eitherDecode jsonWithJpeg of
66+
Left err -> do
67+
H.footnote $ "Parse failed: " <> err
68+
H.failure
69+
Right (img :: Image) -> do
70+
let imgContent = textValue $ content img
71+
imgContent === "data:image/jpeg;base64,/9j/4AAQSkZJRg"

cardano-db-sync/test/Main.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import qualified Cardano.DbSync.ApiTest as Api
44
import qualified Cardano.DbSync.Config.TypesTest as Types
55
import qualified Cardano.DbSync.Era.Shelley.Generic.ScriptDataTest as ScriptData
66
import qualified Cardano.DbSync.Era.Shelley.Generic.ScriptTest as Script
7+
import qualified Cardano.DbSync.OffChain.Vote.TypesTest as VoteTypes
78
import qualified Cardano.DbSync.Util.AddressTest as Address
89
import qualified Cardano.DbSync.Util.Bech32Test as Bech32
910
import qualified Cardano.DbSync.Util.CborTest as Cbor
@@ -23,4 +24,5 @@ main =
2324
, DbSync.tests
2425
, Types.tests
2526
, Api.tests
27+
, VoteTypes.tests
2628
]

cardano-db/src/Cardano/Db/Statement/OffChain.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,9 @@ insertBulkOffChainVoteData offChainVoteData = do
507507

508508
insertBulkOffChainVoteDrepDataStmt :: HsqlStmt.Statement [SO.OffChainVoteDrepData] ()
509509
insertBulkOffChainVoteDrepDataStmt =
510-
insertBulk
510+
insertBulkWith
511+
(ReplaceWithColumns ["off_chain_vote_data_id"])
512+
False
511513
extractOffChainVoteDrepData
512514
SO.offChainVoteDrepDataBulkEncoder
513515
NoResultBulk

0 commit comments

Comments
 (0)