wip, fixed metadata extraction method

This commit is contained in:
Dmitry Zuikov 2024-07-29 16:04:32 +03:00
parent 21724705eb
commit dfb9e9ad92
2 changed files with 47 additions and 19 deletions

View File

@ -13,9 +13,11 @@ import HBS2.Peer.RPC.API.RefLog
import HBS2.Peer.RPC.API.LWWRef import HBS2.Peer.RPC.API.LWWRef
import HBS2.Peer.Proto hiding (request) import HBS2.Peer.Proto hiding (request)
import HBS2.Peer.Proto.LWWRef
import HBS2.Base58 import HBS2.Base58
import HBS2.Net.Auth.Credentials import HBS2.Net.Auth.Credentials
import HBS2.Net.Auth.Schema() import HBS2.Net.Auth.Schema()
import HBS2.Data.Types.SignedBox
import HBS2.KeyMan.Keys.Direct import HBS2.KeyMan.Keys.Direct
import HBS2.KeyMan.App.Types import HBS2.KeyMan.App.Types
@ -86,17 +88,17 @@ lwwRefEntries = do
so <- detectRPC `orDie` "rpc not found" so <- detectRPC `orDie` "rpc not found"
api <- ContT $ withRPC2 @LWWRefAPI @UNIX so api <- ContT $ withRPC2 @LWWRefAPI @UNIX so
what <- callService @RpcLWWRefGet api ref what <- callService @RpcLWWRefGet api ref
>>= orThrowUser "can't get reflog" >>= orThrowUser "can't get lwwref value"
pure $ mkStr (show $ pretty what) pure $ mkStr (show $ pretty what)
_ -> throwIO (BadFormException @C nil) _ -> throwIO (BadFormException @C nil)
entry $ bindMatch "hbs2:lwwref:update" $ \case entry $ bindMatch "hbs2:lwwref:update" $ \case
[StringLike puks, HashLike what] -> do [StringLike puks, HashLike new] -> do
flip runContT pure do flip runContT pure do
puk <- orThrowUser "bad lwwref key" (fromStringMay @(PubKey 'Sign 'HBS2Basic) puks) puk <- orThrowUser "bad lwwref key" (fromStringMay puks)
so <- detectRPC `orDie` "rpc not found" so <- detectRPC `orDie` "rpc not found"
api <- ContT $ withRPC2 @LWWRefAPI @UNIX so api <- ContT $ withRPC2 @LWWRefAPI @UNIX so
@ -105,11 +107,21 @@ lwwRefEntries = do
>>= orThrowUser "can't load credentials" >>= orThrowUser "can't load credentials"
pure ( view peerSignSk creds, view peerSignPk creds ) pure ( view peerSignSk creds, view peerSignPk creds )
what <- callService @RpcLWWRefGet api puk
>>= orThrowUser "can't get lwwref value"
error "YAY!" sno' <- case what of
-- what <- callService @RpcLWWRefGet api ref Nothing -> pure 0
-- >>= orThrowUser "can't get reflog" Just lwwv -> pure (lwwSeq lwwv)
-- pure $ mkStr (show $ pretty what)
let sno = succ sno'
let box = makeSignedBox pk sk (LWWRef sno new Nothing)
callService @RpcLWWRefUpdate api box
>>= orThrowUser "lww ref update error"
pure nil
_ -> throwIO (BadFormException @C nil) _ -> throwIO (BadFormException @C nil)

View File

@ -34,11 +34,14 @@ import Data.Either
import Codec.Serialise (deserialiseOrFail) import Codec.Serialise (deserialiseOrFail)
import Data.Aeson (object, (.=)) import Data.Aeson (object, (.=))
import Data.ByteString.Lazy.Char8 qualified as LBS8 import Data.ByteString.Lazy.Char8 qualified as LBS8
import Data.Text qualified as Text
import Control.Monad.Reader import Control.Monad.Reader
import Lens.Micro.Platform (view) import Lens.Micro.Platform (view)
import System.FilePath import System.FilePath
import Control.Monad.Except import Control.Monad.Except
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Cont import Control.Monad.Trans.Cont
import Data.Coerce
import UnliftIO (async) import UnliftIO (async)
@ -46,11 +49,29 @@ import UnliftIO (async)
-- TODO: introduce-http-of-off-feature -- TODO: introduce-http-of-off-feature
extractMetadataHash :: Hash HbSync -> LBS.ByteString -> Maybe (Hash HbSync) extractMetadataHash :: MonadIO m
extractMetadataHash what blob = => AnyStorage
case tryDetect what blob of -> HashRef
MerkleAnn (MTreeAnn {_mtaMeta = AnnHashRef h, _mtaCrypt = NullEncryption}) -> Just h -> m (Maybe [Syntax C])
_ -> Nothing
extractMetadataHash sto what = runMaybeT do
blob <- getBlock sto (coerce what)
>>= toMPlus
case tryDetect (coerce what) blob of
MerkleAnn (MTreeAnn {_mtaMeta = AnnHashRef h, _mtaCrypt = NullEncryption}) -> do
getBlock sto h
>>= toMPlus
<&> LBS8.unpack
<&> fromRight mempty . parseTop
MerkleAnn (MTreeAnn {_mtaMeta = ShortMetadata txt, _mtaCrypt = NullEncryption}) -> do
parseTop (Text.unpack txt) & toMPlus
_ -> mzero
orElse :: m r -> Maybe a -> ContT r m a orElse :: m r -> Maybe a -> ContT r m a
orElse a mb = ContT $ maybe1 mb a orElse a mb = ContT $ maybe1 mb a
@ -174,15 +195,10 @@ httpWorker (PeerConfig syn) pmeta e = do
getTreeHash :: AnyStorage -> HashRef -> ActionM () getTreeHash :: AnyStorage -> HashRef -> ActionM ()
getTreeHash sto what' = void $ flip runContT pure do getTreeHash sto what' = void $ flip runContT pure do
blob <- liftIO (getBlock sto what)
meta <- extractMetadataHash sto what'
>>= orElse (status status404) >>= orElse (status status404)
mh <- orElse (status status404) (extractMetadataHash what blob)
meta <- lift (getBlock sto mh) >>= orElse (status status404)
<&> LBS8.unpack
<&> fromRight mempty . parseTop
let tp = headDef "application/octet-stream" let tp = headDef "application/octet-stream"
[ show (pretty w) [ show (pretty w)
| ListVal [SymbolVal "mime-type:", LitStrVal w] <- meta | ListVal [SymbolVal "mime-type:", LitStrVal w] <- meta