Class: Hyrax::ValkyrieUpload

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/valkyrie_upload.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_adapter: Hyrax.storage_adapter, file_set_file_service: Hyrax.config.file_set_file_service) ⇒ ValkyrieUpload

Returns a new instance of ValkyrieUpload.

Parameters:

  • storage_adapter (Valkyrie::StorageAdapter) (defaults to: Hyrax.storage_adapter)
  • file_set_file_service (Class) (defaults to: Hyrax.config.file_set_file_service)

    implementer of FileSetFileService



34
35
36
37
# File 'app/services/hyrax/valkyrie_upload.rb', line 34

def initialize(storage_adapter: Hyrax.storage_adapter, file_set_file_service: Hyrax.config.file_set_file_service)
  @storage_adapter = storage_adapter
  @file_set_file_service = file_set_file_service
end

Instance Attribute Details

#storage_adapterObject (readonly)

Returns the value of attribute storage_adapter.



30
31
32
# File 'app/services/hyrax/valkyrie_upload.rb', line 30

def storage_adapter
  @storage_adapter
end

Class Method Details

.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)
  • storage_adapter (Valkyrie::StorageAdapter) (defaults to: Hyrax.storage_adapter)
  • use (RDF::URI) (defaults to: Hyrax::FileMetadata::Use::ORIGINAL_FILE)
  • user (User) (defaults to: nil)

Returns:

See Also:



15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/hyrax/valkyrie_upload.rb', line 15

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

Instance 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:



85
86
87
88
89
# File 'app/services/hyrax/valkyrie_upload.rb', line 85

def add_file_to_file_set(file_set:, file_metadata:, user:)
  file_set.file_ids += [.id]
  Hyrax.persister.save(resource: file_set)
  Hyrax.publisher.publish('object.membership.updated', object: file_set, user: user)
end

#upload(filename:, file_set:, io:, use: Hyrax::FileMetadata::Use::ORIGINAL_FILE, user: nil, mime_type: nil) ⇒ Object

rubocop:disable Metrics/AbcSize



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/services/hyrax/valkyrie_upload.rb', line 39

def upload(filename:, file_set:, io:, use: Hyrax::FileMetadata::Use::ORIGINAL_FILE, user: nil, mime_type: nil) # rubocop:disable Metrics/AbcSize
  return version_upload(file_set: file_set, io: io, user: user) if use == Hyrax::FileMetadata::Use::ORIGINAL_FILE && file_set.original_file_id && storage_adapter.supports?(:versions)
  streamfile = storage_adapter.upload(file: io, original_filename: filename, resource: file_set)
   = Hyrax::FileMetadata(streamfile)
  .file_set_id = file_set.id
  .pcdm_use = [use]
  .recorded_size = [io.size]
  .mime_type = mime_type if mime_type
  .original_filename = File.basename(filename).to_s || File.basename(io)

  if use == 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
  end

   = Hyrax.persister.save(resource: )
  .original_filename = filename if .original_filename.blank?

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

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

  
end

#version_upload(file_set:, io:, user:) ⇒ Object



71
72
73
74
75
76
77
# File 'app/services/hyrax/valkyrie_upload.rb', line 71

def version_upload(file_set:, io:, user:)
   = @file_set_file_service.primary_file_for(file_set: file_set)

  Hyrax::VersioningService.create(, user, io)
  Hyrax.publisher.publish("file.uploaded", metadata: )
  ContentNewVersionEventJob.perform_later(file_set, user)
end