Class: CocinaDisplay::Structural::File
- Inherits:
-
Object
- Object
- CocinaDisplay::Structural::File
- Defined in:
- lib/cocina_display/structural/file.rb
Overview
Represents a single file in a Cocina object.
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
URL to Stacks environment that will serve this file.
-
#cocina ⇒ Object
readonly
Underlying hash parsed from Cocina JSON.
Instance Method Summary collapse
-
#download_url ⇒ String?
Generate a download URL for this file from stacks.
-
#filename ⇒ String?
The name of the file on disk, including file extension.
-
#height ⇒ Integer?
The height of the image in pixels, if applicable.
-
#iiif_id ⇒ String?
For images served over IIIF, we use the encoded file ID minus the extension.
-
#iiif_url(region: "full", width: "!400", height: "400") ⇒ String?
Generate a IIIF image URL for this file.
-
#initialize(cocina, base_url: "https://stacks.stanford.edu", druid: nil) ⇒ File
constructor
Initialize the File with Cocina file data.
-
#jp2_image? ⇒ Boolean
True if this file is a JP2 image and has nonzero dimensions.
-
#mime_type ⇒ String?
The MIME type of the file.
-
#nonzero_dimensions? ⇒ Boolean
True if file is an image with nonzero height and width.
-
#size ⇒ Integer?
The size in bytes of the file.
-
#thumbnail? ⇒ Boolean
True if this file was marked as a thumbnail and has nonzero dimensions.
-
#use ⇒ String?
The relation of the file to the object.
-
#width ⇒ Integer?
The width of the image in pixels, if applicable.
Constructor Details
#initialize(cocina, base_url: "https://stacks.stanford.edu", druid: nil) ⇒ File
Staging objects can’t infer their DRUID and need it passed in explicitly.
Initialize the File with Cocina file data.
15 16 17 18 19 |
# File 'lib/cocina_display/structural/file.rb', line 15 def initialize(cocina, base_url: "https://stacks.stanford.edu", druid: nil) @cocina = cocina @base_url = base_url @druid = druid end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
URL to Stacks environment that will serve this file.
9 10 11 |
# File 'lib/cocina_display/structural/file.rb', line 9 def base_url @base_url end |
#cocina ⇒ Object (readonly)
Underlying hash parsed from Cocina JSON.
6 7 8 |
# File 'lib/cocina_display/structural/file.rb', line 6 def cocina @cocina end |
Instance Method Details
#download_url ⇒ String?
Generate a download URL for this file from stacks.
100 101 102 103 104 |
# File 'lib/cocina_display/structural/file.rb', line 100 def download_url return unless file_id.present? "#{base_url}/file/druid:#{file_id}" end |
#filename ⇒ String?
The name of the file on disk, including file extension.
24 25 26 |
# File 'lib/cocina_display/structural/file.rb', line 24 def filename cocina["filename"] end |
#height ⇒ Integer?
The height of the image in pixels, if applicable.
69 70 71 |
# File 'lib/cocina_display/structural/file.rb', line 69 def height cocina.dig("presentation", "height").to_i end |
#iiif_id ⇒ String?
For images served over IIIF, we use the encoded file ID minus the extension.
94 95 96 |
# File 'lib/cocina_display/structural/file.rb', line 94 def iiif_id ERB::Util.url_encode(file_id.delete_suffix(".jp2")) if file_id.present? && jp2_image? end |
#iiif_url(region: "full", width: "!400", height: "400") ⇒ String?
Generate a IIIF image URL for this file.
85 86 87 88 89 |
# File 'lib/cocina_display/structural/file.rb', line 85 def iiif_url(region: "full", width: "!400", height: "400") return unless iiif_id.present? "#{base_url}/image/iiif/#{iiif_id}/#{region}/#{width},#{height}/0/default.jpg" end |
#jp2_image? ⇒ Boolean
True if this file is a JP2 image and has nonzero dimensions.
57 58 59 |
# File 'lib/cocina_display/structural/file.rb', line 57 def jp2_image? mime_type == "image/jp2" && nonzero_dimensions? end |
#mime_type ⇒ String?
The MIME type of the file.
31 32 33 |
# File 'lib/cocina_display/structural/file.rb', line 31 def mime_type cocina["hasMimeType"] end |
#nonzero_dimensions? ⇒ Boolean
True if file is an image with nonzero height and width.
63 64 65 |
# File 'lib/cocina_display/structural/file.rb', line 63 def nonzero_dimensions? height&.positive? && width&.positive? end |
#size ⇒ Integer?
The size in bytes of the file.
45 46 47 |
# File 'lib/cocina_display/structural/file.rb', line 45 def size cocina["size"] end |
#thumbnail? ⇒ Boolean
True if this file was marked as a thumbnail and has nonzero dimensions.
51 52 53 |
# File 'lib/cocina_display/structural/file.rb', line 51 def thumbnail? use == "thumbnail" && nonzero_dimensions? end |
#use ⇒ String?
The relation of the file to the object.
38 39 40 |
# File 'lib/cocina_display/structural/file.rb', line 38 def use cocina["use"] end |
#width ⇒ Integer?
The width of the image in pixels, if applicable.
75 76 77 |
# File 'lib/cocina_display/structural/file.rb', line 75 def width cocina.dig("presentation", "width").to_i end |
”