Class: CocinaDisplay::Structural::File

Inherits:
Object
  • Object
show all
Defined in:
lib/cocina_display/structural/file.rb

Overview

Represents a single file in a Cocina object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cocina, base_url: "https://stacks.stanford.edu", druid: nil) ⇒ File

Note:

Staging objects can’t infer their DRUID and need it passed in explicitly.

Initialize the File with Cocina file data.

Parameters:

  • cocina (Hash)

    Cocina structured data for a single file

  • druid (String, nil) (defaults to: nil)

    DRUID of the object this file belongs to



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_urlObject (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

#cocinaObject (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_urlString?

Generate a download URL for this file from stacks.

Returns:

  • (String, nil)


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

#filenameString?

The name of the file on disk, including file extension.

Examples:

“bc798xr9549_30C_Kalsang_Yulgial_thumb.jp2”

Returns:

  • (String, nil)


24
25
26
# File 'lib/cocina_display/structural/file.rb', line 24

def filename
  cocina["filename"]
end

#heightInteger?

The height of the image in pixels, if applicable.

Returns:

  • (Integer, nil)


69
70
71
# File 'lib/cocina_display/structural/file.rb', line 69

def height
  cocina.dig("presentation", "height").to_i
end

#iiif_idString?

For images served over IIIF, we use the encoded file ID minus the extension.

Examples:

“ts786ny5936%2FPC0170_s1_E_0204”

Returns:

  • (String, nil)


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.

Examples:

Parameters:

  • region (String) (defaults to: "full")

    Desired region of the image (e.g., “full”, “square”, “x,y,w,h”, “pct:x,y,w,h”).

  • width (String) (defaults to: "!400")

    Desired width of the image in pixels (use “!” prefix to preserve aspect ratio).

  • height (String) (defaults to: "400")

    Desired height of the image in pixels.

Returns:

  • (String, nil)


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.

Returns:

  • (Boolean)


57
58
59
# File 'lib/cocina_display/structural/file.rb', line 57

def jp2_image?
  mime_type == "image/jp2" && nonzero_dimensions?
end

#mime_typeString?

The MIME type of the file.

Examples:

“image/jp2”

Returns:

  • (String, nil)


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.

Returns:

  • (Boolean)


63
64
65
# File 'lib/cocina_display/structural/file.rb', line 63

def nonzero_dimensions?
  height&.positive? && width&.positive?
end

#sizeInteger?

The size in bytes of the file.

Examples:

204800

Returns:

  • (Integer, nil)


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.

Returns:

  • (Boolean)


51
52
53
# File 'lib/cocina_display/structural/file.rb', line 51

def thumbnail?
  use == "thumbnail" && nonzero_dimensions?
end

#useString?

The relation of the file to the object.

Examples:

“thumbnail”

Returns:

  • (String, nil)


38
39
40
# File 'lib/cocina_display/structural/file.rb', line 38

def use
  cocina["use"]
end

#widthInteger?

The width of the image in pixels, if applicable.

Returns:

  • (Integer, nil)


75
76
77
# File 'lib/cocina_display/structural/file.rb', line 75

def width
  cocina.dig("presentation", "width").to_i
end