Module: Hyrax::ValkyrieUpload

Defined in:
app/services/hyrax/valkyrie_upload.rb

Class Method Summary collapse

Class Method Details

.add_file_to_file_set(file_set:, file_metadata:, user:) ⇒ Hyrax::FileSet

Returns updated file set.

Parameters:

  • file_set (Hyrax::FileSet)

    the file set to add to

  • file_metadata (Hyrax::FileMetadata)

    the metadata object representing the file to add

  • user (::User)

    the user performing the add

Returns:


67
68
69
70
71
72
73
# File 'app/services/hyrax/valkyrie_upload.rb', line 67

def self.add_file_to_file_set(file_set:, file_metadata:, user:)
  file_set.file_ids << .id
  set_file_use_ids(file_set, )

  Hyrax.persister.save(resource: file_set)
  Hyrax.publisher.publish('object.membership.updated', object: file_set, user: user)
end

.file(filename:, file_set:, io:, storage_adapter: Hyrax.storage_adapter, use: Hyrax::FileMetadata::Use::ORIGINAL_FILE, user: nil) ⇒ Hyrax::FileMetadata

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/ParameterLists

Parameters:

  • io (IO)
  • filename (String)
  • file_set (Hyrax::FileSet)
  • use (RDF::URI) (defaults to: Hyrax::FileMetadata::Use::ORIGINAL_FILE)
  • user (User) (defaults to: nil)

Returns:

See Also:


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/services/hyrax/valkyrie_upload.rb', line 14

def self.file(
  filename:,
  file_set:,
  io:,
  storage_adapter: Hyrax.storage_adapter,
  use: Hyrax::FileMetadata::Use::ORIGINAL_FILE,
  user: nil
)

  streamfile = storage_adapter.upload(
    file: io,
    original_filename: filename,
    resource: file_set,
    use: use
  )
  io.close

   = Hyrax::FileMetadata(streamfile)
  .file_set_id = file_set.id

  case use
  when Hyrax::FileMetadata::Use::ORIGINAL_FILE
    # Set file set label.
    reset_title = file_set.title.first == file_set.label
    # set title to label if that's how it was before this characterization
    file_set.title = .original_filename if reset_title
    # always set the label to the original_name
    file_set.label = .original_filename
  when Hyrax::FileMetadata::Use::THUMBNAIL
    # TODO: the parent work's thumbnail_id remains incorrect (it's set to the
    # FileSet ID, rather than the ID of this thumbnail FileMetadata; but
    # trying to update the parent attributes here doesn't seem to stick
    file_set.thumbnail_id = .id
  end

   = Hyrax.persister.save(resource: )
  Hyrax.publisher.publish("object.file.uploaded", metadata: )

  add_file_to_file_set(file_set: file_set,
                       file_metadata: ,
                       user: user)

  Hyrax.publisher.publish('file.metadata.updated', metadata: , user: user)

  
end

.set_file_use_ids(file_set, file_metadata) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/services/hyrax/valkyrie_upload.rb', line 80

def self.set_file_use_ids(file_set, )
  .type.each do |type|
    case type
    when Hyrax::FileMetadata::Use::ORIGINAL_FILE
      file_set.original_file_id = .id
    when Hyrax::FileMetadata::Use::THUMBNAIL
      file_set.thumbnail_id = .id
    when Hyrax::FileMetadata::Use::EXTRACTED_TEXT
      file_set.extracted_text_id = .id
    else
      Rails.logger.warn "Unknown file use #{.type} specified for #{.file_identifier}"
    end
  end
end