Module: Cul::Hydra::Controllers::Helpers::ResourcesHelperBehavior

Defined in:
lib/cul_hydra/controllers/helpers/resources_helper_behavior.rb

Constant Summary collapse

IMAGE_MIME_TYPES =
[
  'image/bmp',
  'image/gif',
  'image/jpeg',
  'image/png',
  'image/tiff'
]

Instance Method Summary collapse

Instance Method Details

#add_posted_blob_to_resource(file, resource = @resource) ⇒ Resource

Puts the contents of params (posted blob) into a datastream within the given @resource Sets resource label and title to filename if they’re empty

Parameters:

  • the (Resource)

    Resource to add the blob to

Returns:



111
112
113
# File 'lib/cul_hydra/controllers/helpers/resources_helper_behavior.rb', line 111

def add_posted_blob_to_resource(file, resource=@resource)
  resource.add_content_blob(file.tempfile, :file_name=>file.original_filename, :mime_type=>file.content_type)
end

#apply_posted_file_metadata(resource = @resource) ⇒ Object

Apply any posted file metadata to the file asset



138
139
140
# File 'lib/cul_hydra/controllers/helpers/resources_helper_behavior.rb', line 138

def (resource=@resource)         
  @metadata_update_response = update_document(resource, @sanitized_params)
end

#associate_resource_with_container(resource = @resource, container_id = nil) ⇒ Object

Associate the new file resource with its container



116
117
118
119
120
121
122
123
# File 'lib/cul_hydra/controllers/helpers/resources_helper_behavior.rb', line 116

def associate_resource_with_container(resource=@resource, container_id=nil)
  if container_id.nil?
    container_id = params[:container_id]
  end
  container_id = "info:fedora/#{container_id}" unless container_id =~ /info:fedora\/.+/
  resource.containers_append(container_id)
  resource.datastreams["RELS-EXT"].dirty = true
end

#create_and_save_resources_from_paramsResource

Creates a Resource, adding the posted blob to the Resource’s datastreams and saves the Resource

Returns:



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
# File 'lib/cul_hydra/controllers/helpers/resources_helper_behavior.rb', line 18

def create_and_save_resources_from_params
  if params.has_key?(:Fileurl)
    # parse url for file name, default to index.html
    file_url = params[:Fileurl]
    file_url = URI.parse(file_url) unless file_url.nil?
    file_name = 'index.html'
    if file_url.scheme
      file_name = file_url.path[1...file_url.path.length]
    end
    # download resource; override file name with header value if present
    blob = Tempfile.new('temp')
    blob.binmode
    # download header? buffered writing?
    response = Net::HTTP.get_response(file_url)
    blob.write response.body
    if response['Content-Disposition']
      header = response['Content-Disposition']
      if header =~ /filename=\"?(\w+)\"?/
        file_name = $1
      end
    end
    # add filename and resource to params
    params[:Filedata] = [ActionDispatch::Http::UploadedFile.new(:tempfile=>blob,:filename=>file_name,:type=>mime_type(file_name))]
  end
  if params.has_key?(:Filedata)
    @resources = []
    params[:Filedata].each do |file|
      file.content_type = mime_type(file.original_filename) unless file.content_type
      @resource = create_resource_from_file(file)
      @resource.save
      @resources << @resource
      @resource.refresh
      add_posted_blob_to_resource(file, @resource)
      @resource.save
    end
  else
    render :text => "400 Bad Request", :status => 400
  end
  @resources
end

#create_resource_from_file(file) ⇒ Object



100
101
102
103
104
# File 'lib/cul_hydra/controllers/helpers/resources_helper_behavior.rb', line 100

def create_resource_from_file(file)
  resource = Resource.new
  resource.datastreams["rightsMetadata"].ng_xml = Hydra::RightsMetadata.xml_template
  update_resource_from_file(resource,file)
end

#filename_for(file) ⇒ Object

A best-guess filename If Filename was submitted, it uses that. Otherwise, it calls original_filename on the posted file



145
146
147
# File 'lib/cul_hydra/controllers/helpers/resources_helper_behavior.rb', line 145

def filename_for(file)
  file.instance_variable_get(:@original_filename) || file.original_filename
end

#remove_resource_from_container(resource = nil, container_id = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cul_hydra/controllers/helpers/resources_helper_behavior.rb', line 125

def remove_resource_from_container(resource=nil, container_id=nil)
  if container_id.nil?
    container_id = params[:container_id]
  end
  if resource.nil?
    resource = @resource
  end
  resource.containers_remove(container_id)
  #resource.remove_relationship(:cul_member_of, container_id)
  resource.datastreams["RELS-EXT"].dirty = true
end

#update_resource_from_file(resource, file) ⇒ Object



94
95
96
97
98
99
# File 'lib/cul_hydra/controllers/helpers/resources_helper_behavior.rb', line 94

def update_resource_from_file(resource,file)
  file_name = filename_for(file)
  resource.label = file_name
  resource.datastreams["DC"].update_values([:source=>0]=>[file_name])
  resource
end

#update_resource_from_paramsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cul_hydra/controllers/helpers/resources_helper_behavior.rb', line 59

def update_resource_from_params
  if params.has_key?(:Fileurl)
    # parse url for file name, default to index.html
    file_url = params[:Fileurl]
    file_url = URI.parse(file_url) unless file_url.nil?
    file_name = 'index.html'
    if file_url.scheme
      file_name = file_url.path[1...file_url.path.length]
    end
    # download resource; override file name with header value if present
    blob = Tempfile.new('temp')
    blob.binmode
    # download header? buffered writing?
    response = Net::HTTP.get_response(file_url)
    blob.write response.body
    if response['Content-Disposition']
      header = response['Content-Disposition']
      if header =~ /filename=\"?(\w+)\"?/
        file_name = $1
      end
    end
    # add filename and resource to params
    params[:Filedata] = ActionDispatch::Http::UploadedFile.new(:tempfile=>blob,:filename=>file_name,:type=>mime_type(file_name))
  end
  if params.has_key?(:Filedata)
    file = params[:Filedata]
    file.content_type = mime_type(file.original_filename) unless file.content_type
    update_resource_from_file(@document_fedora,file)
    add_posted_blob_to_resource(file, @document_fedora)
    @document_fedora.save
  else
    render :text => "400 Bad Request", :status => 400
  end
  @document_fedora
end