]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Node/Document/Export.hs
Merge remote-tracking branch 'origin/481-dev-node-calc-upload' into dev
[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 (Headers '[Header "Content-Disposition" T.Text] 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 Nothing
44 pure $ addHeader (T.concat [ "attachment; filename=GarganText_DocsList-"
45 , T.pack $ show pId
46 , ".json"])
47 DocumentExport { _de_documents = mapFacetDoc <$> docs
48 , _de_garg_version = T.pack $ showVersion PG.version }
49 where
50 mapFacetDoc (FacetDoc { .. }) =
51 Document { _d_document =
52 Node { _node_id = facetDoc_id
53 , _node_hash_id = Nothing
54 , _node_typename = toDBid NodeDocument
55 , _node_user_id = uId
56 , _node_parent_id = Nothing
57 , _node_name = facetDoc_title
58 , _node_date = facetDoc_created
59 , _node_hyperdata = facetDoc_hyperdata }
60 , _d_ngrams = Ngrams { _ng_ngrams = []
61 , _ng_hash = "" }
62 , _d_hash = "" }
63 _mapDoc d = Document { _d_document = d
64 , _d_ngrams = Ngrams { _ng_ngrams = []
65 , _ng_hash = "" }
66 , _d_hash = ""}
67
68 getDocumentsCSV :: UserId
69 -> DocId
70 -> GargNoServer (Headers '[Header "Content-Disposition" T.Text] T.Text) -- [Document]
71 getDocumentsCSV uId pId = do
72 dJSON <- getDocumentsJSON uId pId
73 let DocumentExport { _de_documents } = getResponse dJSON
74 let ret = TE.decodeUtf8 $ BSC.toStrict $ encodeDefaultOrderedByName _de_documents
75
76 pure $ addHeader (T.concat [ "attachment; filename=GarganText_DocsList-"
77 , T.pack $ show pId
78 , ".csv"])
79 ret