ldp_nr.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.dictionaries.namespaces import ns_collection as nsc
  6. from lakesuperior.model.ldpr import Ldpr
  7. from lakesuperior.model.ldp_rs import LdpRs
  8. class LdpNr(Ldpr):
  9. '''LDP-NR (Non-RDF Source).
  10. Definition: https://www.w3.org/TR/ldp/#ldpnr
  11. '''
  12. base_types = {
  13. nsc['fcrepo'].Binary,
  14. nsc['fcrepo'].Resource,
  15. nsc['ldp'].Resource,
  16. nsc['ldp'].NonRDFSource,
  17. }
  18. def __init__(self, uuid, stream=None, mimetype=None,
  19. disposition=None, **kwargs):
  20. '''
  21. Extends Ldpr.__init__ by adding LDP-NR specific parameters.
  22. '''
  23. super().__init__(uuid, **kwargs)
  24. self._imr_options = {}
  25. if stream:
  26. self.workflow = self.WRKF_INBOUND
  27. self.stream = stream
  28. else:
  29. self.workflow = self.WRKF_OUTBOUND
  30. if not mimetype:
  31. self.mimetype = (
  32. self.metadata.value(nsc['ebucore'].hasMimeType)
  33. if self.is_stored
  34. else 'application/octet-stream')
  35. else:
  36. self.mimetype = mimetype
  37. self.disposition = disposition
  38. @property
  39. def filename(self):
  40. return self.imr.value(nsc['ebucore'].filename)
  41. @property
  42. def local_path(self):
  43. cksum_term = self.imr.value(nsc['premis'].hasMessageDigest)
  44. cksum = str(cksum_term.identifier.replace('urn:sha1:',''))
  45. return self.nonrdfly.local_path(cksum)
  46. ## LDP METHODS ##
  47. def _create_or_replace_rsrc(self, create_only=False):
  48. '''
  49. Create a new binary resource with a corresponding RDF representation.
  50. @param file (Stream) A Stream resource representing the uploaded file.
  51. '''
  52. # Persist the stream.
  53. file_uuid = self.digest = self.nonrdfly.persist(self.stream)
  54. # Try to persist metadata. If it fails, delete the file.
  55. self._logger.debug('Persisting LDP-NR triples in {}'.format(self.urn))
  56. try:
  57. ev_type = super()._create_or_replace_rsrc(create_only)
  58. except:
  59. self.nonrdfly.delete(file_uuid)
  60. raise
  61. else:
  62. return ev_type
  63. ## PROTECTED METHODS ##
  64. def _add_srv_mgd_triples(self, create=False):
  65. '''
  66. Add all metadata for the RDF representation of the LDP-NR.
  67. @param stream (BufferedIO) The uploaded data stream.
  68. @param mimetype (string) MIME type of the uploaded file.
  69. @param disposition (defaultdict) The `Content-Disposition` header
  70. content, parsed through `parse_rfc7240`.
  71. '''
  72. super()._add_srv_mgd_triples(create)
  73. # File size.
  74. self._logger.debug('Data stream size: {}'.format(self.stream.limit))
  75. self.provided_imr.set(nsc['premis'].hasSize,
  76. Literal(self.stream.limit))
  77. # Checksum.
  78. cksum_term = URIRef('urn:sha1:{}'.format(self.digest))
  79. self.provided_imr.set(nsc['premis'].hasMessageDigest, cksum_term)
  80. # MIME type.
  81. self.provided_imr.set(nsc['ebucore']['hasMimeType'],
  82. Literal(self.mimetype))
  83. # File name.
  84. self._logger.debug('Disposition: {}'.format(self.disposition))
  85. try:
  86. self.provided_imr.set(nsc['ebucore']['filename'], Literal(
  87. self.disposition['attachment']['parameters']['filename']))
  88. except (KeyError, TypeError) as e:
  89. pass