ldp_nr.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.workflow = self.WRKF_OUTBOUND
  29. self.mimetype = mimetype
  30. self.disposition = disposition
  31. @property
  32. def filename(self):
  33. return self.imr.value(nsc['ebucore'].filename)
  34. @property
  35. def local_path(self):
  36. cksum_term = self.imr.value(nsc['premis'].hasMessageDigest)
  37. cksum = str(cksum_term.identifier.replace('urn:sha1:',''))
  38. return self.nonrdfly.local_path(cksum)
  39. ## LDP METHODS ##
  40. def get(self, **kwargs):
  41. return LdpRs(self.uuid).get(**kwargs)
  42. @atomic
  43. def _create_or_replace_rsrc(self, create_only=False):
  44. '''
  45. Create a new binary resource with a corresponding RDF representation.
  46. @param file (Stream) A Stream resource representing the uploaded file.
  47. '''
  48. # Persist the stream.
  49. file_uuid = self.digest = self.nonrdfly.persist(self.stream)
  50. # Try to persist metadata. If it fails, delete the file.
  51. self._logger.debug('Persisting LDP-NR triples in {}'.format(self.urn))
  52. try:
  53. ev_type = super()._create_or_replace_rsrc(create_only)
  54. except:
  55. self.nonrdfly.delete(file_uuid)
  56. raise
  57. else:
  58. return ev_type
  59. ## PROTECTED METHODS ##
  60. def _add_srv_mgd_triples(self, create=False):
  61. '''
  62. Add all metadata for the RDF representation of the LDP-NR.
  63. @param stream (BufferedIO) The uploaded data stream.
  64. @param mimetype (string) MIME type of the uploaded file.
  65. @param disposition (defaultdict) The `Content-Disposition` header
  66. content, parsed through `parse_rfc7240`.
  67. '''
  68. super()._add_srv_mgd_triples(create)
  69. # File size.
  70. self._logger.debug('Data stream size: {}'.format(self.stream.limit))
  71. self.provided_imr.set(nsc['premis'].hasSize, 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