ldp_nr.py 3.3 KB

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