]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Node/Document/Export.hs
Merge branch '111-dev-refactor-text-corpus-api-with-conduit-alp' of ssh://gitlab...
[gargantext.git] / src / Gargantext / API / Node / Document / Export.hs
1 {-|
2 Module : Gargantext.API.Node.Document.Export
3 Description : Document export
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9 -}
10
11 module Gargantext.API.Node.Document.Export
12 where
13
14 import qualified Data.ByteString.Lazy.Char8 as BSC
15 import Data.Csv (encodeDefaultOrderedByName)
16 import qualified Data.Text as T
17 import qualified Data.Text.Encoding as TE
18 import Data.Version (showVersion)
19 import Gargantext.API.Node.Document.Export.Types
20 import Gargantext.API.Prelude (GargNoServer, GargServer)
21 import Gargantext.Core (toDBid)
22 import Gargantext.Core.Types
23 -- import Gargantext.Database.Admin.Types.Hyperdata (HyperdataDocument(..))
24 import Gargantext.Database.Query.Facet (runViewDocuments, Facet(..))
25 import Gargantext.Database.Query.Table.Node (getClosestParentIdByType)
26 import Gargantext.Database.Schema.Node (NodePoly(..))
27 import Gargantext.Prelude
28 import qualified Paths_gargantext as PG -- cabal magic build module
29 import Servant
30
31 api :: UserId -> DocId -> GargServer API
32 api uid dId = getDocumentsJSON uid dId
33 :<|> getDocumentsCSV uid dId
34
35 --------------------------------------------------
36 -- | Hashes are ordered by Set
37 getDocumentsJSON :: UserId
38 -> DocId
39 -> GargNoServer DocumentExport
40 getDocumentsJSON uId pId = do
41 mcId <- getClosestParentIdByType pId NodeCorpus
42 let cId = maybe (panic "[G.A.N.D.Export] Node has no parent") identity mcId
43 docs <- runViewDocuments cId False Nothing Nothing Nothing Nothing
44 pure $ DocumentExport { _de_documents = mapFacetDoc <$> docs
45 , _de_garg_version = T.pack $ showVersion PG.version }
46 where
47 mapFacetDoc (FacetDoc { .. }) =
48 Document { _d_document =
49 Node { _node_id = facetDoc_id
50 , _node_hash_id = Nothing
51 , _node_typename = toDBid NodeDocument
52 , _node_user_id = uId
53 , _node_parent_id = Nothing
54 , _node_name = facetDoc_title
55 , _node_date = facetDoc_created
56 , _node_hyperdata = facetDoc_hyperdata }
57 , _d_ngrams = Ngrams { _ng_ngrams = []
58 , _ng_hash = "" }
59 , _d_hash = "" }
60 _mapDoc d = Document { _d_document = d
61 , _d_ngrams = Ngrams { _ng_ngrams = []
62 , _ng_hash = "" }
63 , _d_hash = ""}
64
65 getDocumentsCSV :: UserId
66 -> DocId
67 -> GargNoServer T.Text -- [Document]
68 getDocumentsCSV uId pId = do
69 DocumentExport { _de_documents } <- getDocumentsJSON uId pId
70 let ret = TE.decodeUtf8 $ BSC.toStrict $ encodeDefaultOrderedByName _de_documents
71
72 pure ret
73