ldp_nr.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from rdflib import Graph
  2. from rdflib.namespace import RDF, XSD
  3. from rdflib.resource import Resource
  4. from rdflib.term import URIRef, Literal, Variable
  5. from lakesuperior.config_parser import config
  6. from lakesuperior.dictionaries.namespaces import ns_collection as nsc
  7. from lakesuperior.model.ldpr import Ldpr, transactional, must_exist
  8. from lakesuperior.util.digest import Digest
  9. class LdpNr(Ldpr):
  10. '''LDP-NR (Non-RDF Source).
  11. Definition: https://www.w3.org/TR/ldp/#ldpnr
  12. '''
  13. base_types = {
  14. nsc['fcrepo'].Binary,
  15. nsc['ldp'].NonRDFSource,
  16. }
  17. ## LDP METHODS ##
  18. def get(self, *args, **kwargs):
  19. raise NotImplementedError()
  20. def post(self, data):
  21. #self._logger.debug('Data: {}'.format(data[:256]))
  22. metadata_rsrc = Resource(Graph(), self.urn)
  23. for t in self.base_types:
  24. metadata_rsrc.add(RDF.type, t)
  25. cksum = Digest.non_rdf_cksum(data)
  26. cksum_term = URIRef('urn:sha1:{}'.format(cksum))
  27. metadata_rsrc.add(nsc['premis'].hasMessageDigest, cksum_term)
  28. self._store_binary(data, cksum)
  29. def put(self, data):
  30. raise NotImplementedError()
  31. ## PROTECTED METHODS ##
  32. def _store_binary(self, data, cksum):
  33. '''
  34. Move a binary file to persistent storage.
  35. @param data (bytes) Binary data to store.
  36. @param cksum (string) Digest of the data. This is used to determine the
  37. file location.
  38. '''
  39. pass