ldp_nr.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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, transactional, must_exist
  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. @property
  19. def nonrdfly(self):
  20. '''
  21. Load non-RDF (binary) store layout.
  22. '''
  23. if not hasattr(self, '_nonrdfly'):
  24. self._nonrdfly = __class__.load_layout('non_rdf')
  25. return self._nonrdfly
  26. @property
  27. def filename(self):
  28. return self.imr.value(nsc['ebucore'].filename)
  29. @property
  30. def local_path(self):
  31. cksum_term = self.imr.value(nsc['premis'].hasMessageDigest)
  32. cksum = str(cksum_term.identifier.replace('urn:sha1:',''))
  33. return self.nonrdfly.local_path(cksum)
  34. ## LDP METHODS ##
  35. def get(self, **kwargs):
  36. return LdpRs(self.uuid).get(**kwargs)
  37. @transactional
  38. def post(self, stream, mimetype=None, disposition=None):
  39. '''
  40. Create a new binary resource with a corresponding RDF representation.
  41. @param file (Stream) A Stream resource representing the uploaded file.
  42. '''
  43. # Persist the stream.
  44. file_uuid = self.nonrdfly.persist(stream)
  45. # Gather RDF metadata.
  46. self._add_metadata(stream, digest=file_uuid, mimetype=mimetype,
  47. disposition=disposition)
  48. # Try to persist metadata. If it fails, delete the file.
  49. self._logger.debug('Persisting LDP-NR triples in {}'.format(
  50. self.urn))
  51. try:
  52. rsrc = self.rdfly.create_rsrc(self.imr)
  53. except:
  54. self.nonrdfly.delete(file_uuid)
  55. else:
  56. return rsrc
  57. def put(self, stream, **kwargs):
  58. return self.post(stream, **kwargs)
  59. ## PROTECTED METHODS ##
  60. def _add_metadata(self, stream, digest,
  61. mimetype='application/octet-stream', disposition=None):
  62. '''
  63. Add all metadata for the RDF representation of the LDP-NR.
  64. @param stream (BufferedIO) The uploaded data stream.
  65. @param mimetype (string) MIME type of the uploaded file.
  66. @param disposition (defaultdict) The `Content-Disposition` header
  67. content, parsed through `parse_rfc7240`.
  68. '''
  69. # File size.
  70. self._logger.debug('Data stream size: {}'.format(stream.limit))
  71. self.stored_or_new_imr.set(nsc['premis'].hasSize, Literal(stream.limit))
  72. # Checksum.
  73. cksum_term = URIRef('urn:sha1:{}'.format(digest))
  74. self.imr.set(nsc['premis'].hasMessageDigest, cksum_term)
  75. # MIME type.
  76. self.imr.set(nsc['ebucore']['hasMimeType'], Literal(mimetype))
  77. # File name.
  78. self._logger.debug('Disposition: {}'.format(disposition))
  79. try:
  80. self.imr.set(nsc['ebucore']['filename'], Literal(
  81. disposition['attachment']['parameters']['filename']))
  82. except KeyError:
  83. pass