class ActiveFedora::File

An LDP NonRDFSource. The base class for a bytestream stored in the repository.

Public Class Methods

new(identifier = nil) { |self| ... } click to toggle source

@param [Hash, RDF::URI, String, NilClass] identifier the id (path) or URI of this resource. The hash gets passed when calling Reflection#build_association, but currently we don’t do anything with it. @yield [self] Yields self @yieldparam [File] self the newly created file

# File lib/active_fedora/file.rb, line 32
def initialize(identifier = nil, &_block)
  identifier = identifier.delete(:id) if identifier.is_a? Hash
  identifier = identifier.uri if identifier.respond_to? :uri
  run_callbacks(:initialize) do
    case identifier
    when nil, ::RDF::URI
      @ldp_source = build_ldp_resource_via_uri identifier
    when String
      id = ActiveFedora::Associations::IDComposite.new([identifier], translate_uri_to_id).first
      @ldp_source = build_ldp_resource id
    else
      raise "The first argument to #{self} must be a Hash, String or RDF::URI. You provided a #{identifier.class}"
    end

    @local_attributes = {}.with_indifferent_access
    @readonly = false
    yield self if block_given?
  end
end

Private Class Methods

relation() click to toggle source
# File lib/active_fedora/file.rb, line 153
def self.relation
  FileRelation.new(self)
end

Public Instance Methods

changed?() click to toggle source
Calls superclass method
# File lib/active_fedora/file.rb, line 123
def changed?
  super || content_changed? || metadata_changed?
end
check_fixity() click to toggle source
# File lib/active_fedora/file.rb, line 92
def check_fixity
  FixityService.new(@ldp_source.subject).check
end
checksum() click to toggle source
# File lib/active_fedora/file.rb, line 109
def checksum
  ActiveFedora::Checksum.new(self)
end
content() click to toggle source
# File lib/active_fedora/file.rb, line 149
def content
  local_or_remote_content(true)
end
content=(string_or_io) click to toggle source
# File lib/active_fedora/file.rb, line 144
def content=(string_or_io)
  content_will_change! unless @content == string_or_io
  @content = string_or_io
end
content_changed?() click to toggle source
# File lib/active_fedora/file.rb, line 113
def content_changed?
  return true if new_record? && local_or_remote_content(false).present?
  local_or_remote_content(false) != @ds_content
end
datastream_will_change!() click to toggle source
# File lib/active_fedora/file.rb, line 96
def datastream_will_change!
  attribute_will_change! :ldp_source
end
described_by() click to toggle source
# File lib/active_fedora/file.rb, line 52
def described_by
  raise "#{self} isn't persisted yet" if new_record?
  links['describedby'].first
end
exists!() click to toggle source

If we know the record to exist (parent has LDP:contains), we can avoid unnecessary HEAD requests

# File lib/active_fedora/file.rb, line 72
def exists!
  @exists = true
end
inspect() click to toggle source
# File lib/active_fedora/file.rb, line 127
def inspect
  "#<#{self.class} uri=\"#{uri}\" >"
end
ldp_connection() click to toggle source
# File lib/active_fedora/file.rb, line 57
def ldp_connection
  ActiveFedora.fedora.connection
end
metadata() click to toggle source
# File lib/active_fedora/file.rb, line 105
def metadata
  @metadata ||= ActiveFedora::WithMetadata::MetadataNode.new(self)
end
metadata?() click to toggle source

@abstract Override this in your concrete datastream class. @return [boolean] does this datastream contain metadata (not file data)

# File lib/active_fedora/file.rb, line 133
def metadata?
  false
end
metadata_changed?() click to toggle source
# File lib/active_fedora/file.rb, line 118
def metadata_changed?
  return false if new_record? || links['describedby'].blank?
  metadata.changed?
end
new_record?() click to toggle source

If this file has a parent with ldp#contains, we know it is not new. By tracking exists we prevent an unnecessary HEAD request.

# File lib/active_fedora/file.rb, line 63
def new_record?
  !@exists && ldp_source.new?
end
refresh() click to toggle source
# File lib/active_fedora/file.rb, line 82
def refresh
  @ldp_source = build_ldp_resource_via_uri(uri)
  @original_name = nil
  @mime_type = nil
  @content = nil
  @metadata = nil
  @ds_content = nil
  clear_attribute_changes(changes.keys)
end
reload() click to toggle source

When restoring from previous versions, we need to reload certain attributes from Fedora

# File lib/active_fedora/file.rb, line 77
def reload
  return if new_record?
  refresh
end
remote_content() click to toggle source
# File lib/active_fedora/file.rb, line 100
def remote_content
  return if new_record?
  @ds_content ||= retrieve_content
end
serialize!() click to toggle source

serializes any changed data into the content field

# File lib/active_fedora/file.rb, line 138
def serialize!; end
to_solr(solr_doc = {}, _opts = {}) click to toggle source
# File lib/active_fedora/file.rb, line 140
def to_solr(solr_doc = {}, _opts = {})
  solr_doc
end
uri=(uri) click to toggle source
# File lib/active_fedora/file.rb, line 67
def uri=(uri)
  @ldp_source = build_ldp_resource_via_uri(uri)
end

Private Instance Methods

behaves_like_io?(obj) click to toggle source

Rack::Test::UploadedFile is often set via content=, however it’s not an IO, though it wraps an io object.

# File lib/active_fedora/file.rb, line 167
def behaves_like_io?(obj)
  [IO, Tempfile, StringIO].any? { |klass| obj.is_a? klass } || (defined?(Rack) && obj.is_a?(Rack::Test::UploadedFile))
end
build_ldp_resource(id) click to toggle source
# File lib/active_fedora/file.rb, line 181
def build_ldp_resource(id)
  build_ldp_resource_via_uri self.class.id_to_uri(id)
end
build_ldp_resource_via_uri(uri = nil, content = '') click to toggle source
# File lib/active_fedora/file.rb, line 185
def build_ldp_resource_via_uri(uri = nil, content = '')
  Ldp::Resource::BinarySource.new(ldp_connection, uri, content, base_path_for_resource)
end
create_or_update(*options) click to toggle source
# File lib/active_fedora/file.rb, line 160
def create_or_update(*options)
  super.tap do
    metadata.save if metadata.changed?
  end
end
ldp_headers() click to toggle source
# File lib/active_fedora/file.rb, line 175
def ldp_headers
  headers = { 'Content-Type'.freeze => mime_type, 'Content-Length'.freeze => content.size.to_s }
  headers['Content-Disposition'.freeze] = "attachment; filename=\"#{URI::DEFAULT_PARSER.escape(@original_name)}\"" if @original_name
  headers
end
local_or_remote_content(ensure_fetch = true) click to toggle source
# File lib/active_fedora/file.rb, line 193
def local_or_remote_content(ensure_fetch = true)
  @content ||= ensure_fetch ? remote_content : @ds_content unless new_record?
  @content.rewind if behaves_like_io?(@content)
  @content
end
retrieve_content() click to toggle source
# File lib/active_fedora/file.rb, line 171
def retrieve_content
  ldp_source.get.body
end
uploaded_file?(payload) click to toggle source
# File lib/active_fedora/file.rb, line 189
def uploaded_file?(payload)
  defined?(ActionDispatch::Http::UploadedFile) && payload.instance_of?(ActionDispatch::Http::UploadedFile)
end