Haskell mongodb: Convert Binary value back to ByteString -


this simple , stupid cant see.

if new type defined:

newtype binary  constructors binary bytestring  instances: eq binary     ord binary    read binary   show binary   typeable binary   val binary 

how can deconstruct binary value bytestring back?

if want save binary data mongodb, jpg picture, able construct val binary type out of bytestring read filesystem. insert document.

when read data database , take out of document end binary type , sooo stuck it. can not bytestring type use bytestring.writefile.

so skipping connection stuff flow this:

file <- b.readfile "pic.jpg" -- reading file let doc = ["file" =: (binary file)] -- constructing document inserted run $ insert_ "files" doc -- insert document r <- run $ fetch (select [] "files") -- either failure document db let d = either (error . show) (id ) r -- document out let f  = @ "file" d :: binary -- data out of document of type binary 

thank you.

assuming newtype looks this,

newtype binary = binary bytestring 

then can pattern match on constructor bytestring back:

unbinary :: binary -> bytestring unbinary (binary s) = s 

Comments