Class: CanvasLinkMigrator::LinkResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/canvas_link_migrator/link_resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(migration_id_converter) ⇒ LinkResolver

Returns a new instance of LinkResolver.



29
30
31
# File 'lib/canvas_link_migrator/link_resolver.rb', line 29

def initialize(migration_id_converter)
  @migration_id_converter = migration_id_converter
end

Instance Attribute Details

#migration_id_converterObject

Returns the value of attribute migration_id_converter.



25
26
27
# File 'lib/canvas_link_migrator/link_resolver.rb', line 25

def migration_id_converter
  @migration_id_converter
end

Instance Method Details

#attachment_path_id_lookup_lowerObject



43
44
45
# File 'lib/canvas_link_migrator/link_resolver.rb', line 43

def attachment_path_id_lookup_lower
  @attachment_path_id_lookup_lower ||= attachment_path_id_lookup&.transform_keys(&:downcase)
end

#find_file_in_context(rel_path) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/canvas_link_migrator/link_resolver.rb', line 156

def find_file_in_context(rel_path)
  mig_id = nil
  # This is for backward-compatibility: canvas attachment filenames are escaped
  # with '+' for spaces and older exports have files with that instead of %20
  alt_rel_path = rel_path.tr("+", " ")
  if attachment_path_id_lookup
    mig_id ||= attachment_path_id_lookup[rel_path]
    mig_id ||= attachment_path_id_lookup[alt_rel_path]
  end
  if !mig_id && attachment_path_id_lookup_lower
    mig_id ||= attachment_path_id_lookup_lower[rel_path.downcase]
    mig_id ||= attachment_path_id_lookup_lower[alt_rel_path.downcase]
  end

  # This md5 comparison is here to handle faulty cartridges with the migration_id equivalent of an empty string
  mig_id && mig_id != "gd41d8cd98f00b204e9800998ecf8427e" && @migration_id_converter.lookup_attachment_by_migration_id(mig_id)
end

#media_attachment_iframe_url(file_id, media_type = nil) ⇒ Object



222
223
224
225
226
# File 'lib/canvas_link_migrator/link_resolver.rb', line 222

def media_attachment_iframe_url(file_id, media_type = nil)
  url = "/media_attachments_iframe/#{file_id}"
  url += "?type=#{media_type}" if media_type.present?
  url
end

#media_iframe_url(media_id, media_type = nil) ⇒ Object



216
217
218
219
220
# File 'lib/canvas_link_migrator/link_resolver.rb', line 216

def media_iframe_url(media_id, media_type = nil)
  url = "/media_objects_iframe/#{media_id}"
  url += "?type=#{media_type}" if media_type.present?
  url
end

#missing_relative_file_url(rel_path) ⇒ Object



151
152
153
154
# File 'lib/canvas_link_migrator/link_resolver.rb', line 151

def missing_relative_file_url(rel_path)
  # the rel_path should already be escaped
  File.join(URI::DEFAULT_PARSER.escape("#{context_path}/file_contents/#{@migration_id_converter.root_folder_name}"), rel_path.gsub(" ", "%20"))
end

#resolve_link!(link) ⇒ Object

finds the :new_value to use to replace the placeholder



48
49
50
51
52
53
54
55
56
57
58
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/canvas_link_migrator/link_resolver.rb', line 48

def resolve_link!(link)
  case link[:link_type]
  when :wiki_page
    if (linked_wiki_url = @migration_id_converter.convert_wiki_page_migration_id_to_slug(link[:migration_id]))
      link[:new_value] = "#{context_path}/pages/#{linked_wiki_url}#{link[:query]}"
    end
  when :discussion_topic
    if (linked_topic_id = @migration_id_converter.convert_discussion_topic_migration_id(link[:migration_id]))
      link[:new_value] = "#{context_path}/discussion_topics/#{linked_topic_id}#{link[:query]}"
    end
  when :module_item
    if (tag_id = @migration_id_converter.convert_context_module_tag_migration_id(link[:migration_id]))
      link[:new_value] = "#{context_path}/modules/items/#{tag_id}#{link[:query]}"
    end
  when :object
    type = link[:type]
    migration_id = link[:migration_id]

    type_for_url = type
    type = "context_modules" if type == "modules"
    type = "pages" if type == "wiki"
    if type == "pages"
      query = resolve_module_item_query(nil, link[:query])
      linked_wiki_url = @migration_id_converter.convert_wiki_page_migration_id_to_slug(migration_id) || migration_id
      link[:new_value] = "#{context_path}/pages/#{linked_wiki_url}#{query}"
    elsif type == "attachments"
      att_id = @migration_id_converter.convert_attachment_migration_id(migration_id)
      if att_id
        link[:new_value] = "#{context_path}/files/#{att_id}/preview"
      end
    elsif type == "media_attachments_iframe"
      att_id = @migration_id_converter.convert_attachment_migration_id(migration_id)
      link[:new_value] = att_id ? "/media_attachments_iframe/#{att_id}#{link[:query]}" : link[:old_value]
    else
      object_id = @migration_id_converter.convert_migration_id(type, migration_id)
      if object_id
        query = resolve_module_item_query(nil, link[:query])
        link[:new_value] = "#{context_path}/#{type_for_url}/#{object_id}#{query}"
      end
    end
  when :media_object
    # because we actually might change the node itself
    # this part is a little trickier
    # tl;dr we've replaced the entire node with the placeholder
    # see LinkParser for details
    rel_path = link[:rel_path]
    node = Nokogiri::HTML5.fragment(link[:old_value]).children.first
    new_url = resolve_media_comment_data(node, rel_path, link[:media_attachment])
    new_url ||= resolve_relative_file_url(rel_path)

    unless new_url
      new_url ||= missing_relative_file_url(rel_path)
      link[:missing_url] = new_url
    end
    if ["iframe", "source"].include?(node.name)
      node["src"] = new_url
    else
      node["href"] = new_url
    end
    link[:new_value] = node.to_s
  when :file
    rel_path = link[:rel_path]
    new_url = resolve_relative_file_url(rel_path)
    unless new_url
      new_url = missing_relative_file_url(rel_path)
      link[:missing_url] = new_url
    end
    link[:new_value] = new_url
  when :file_ref
    file_id = @migration_id_converter.convert_attachment_migration_id(link[:migration_id])
    if file_id
      rest = link[:rest].presence || "/preview"

      # Icon Maker files should not have the course
      # context prepended to the URL. This prevents
      # redirects to non cross-origin friendly urls
      # during a file fetch
      if rest.include?("icon_maker_icon=1")
        link[:new_value] = "/files/#{file_id}#{rest}"
      elsif link[:in_media_iframe] && link[:media_attachment]
        link[:new_value] = "/media_attachments_iframe/#{file_id}#{rest}"
      else
        link[:new_value] = "#{context_path}/files/#{file_id}#{rest}"
        link[:new_value] = "/media_objects_iframe?mediahref=#{link[:new_value]}" if link[:in_media_iframe]
      end
    end
  else
    raise "unrecognized link_type (#{link[:link_type]}) in unresolved link"
  end
end

#resolve_links!(link_map) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/canvas_link_migrator/link_resolver.rb', line 33

def resolve_links!(link_map)
  link_map.each_value do |field_links|
    field_links.each_value do |links|
      links.each do |link|
        resolve_link!(link)
      end
    end
  end
end

#resolve_media_comment_data(node, rel_path, media_attachment) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/canvas_link_migrator/link_resolver.rb', line 228

def resolve_media_comment_data(node, rel_path, media_attachment)
  if (file = find_file_in_context(rel_path[/^[^?]+/])) # strip query string for this search
    media_id = file.try(:media_object)&.media_id || file["media_entry_id"]
    if media_id && media_id != "maybe"
      if ["iframe", "source"].include?(node.name)
        node["data-media-id"] = media_id
        if media_attachment
          return media_attachment_iframe_url(file["id"], node["data-media-type"])
        else
          return media_iframe_url(media_id, node["data-media-type"])
        end
      else
        node["id"] = "media_comment_#{media_id}"
        return "/media_objects/#{media_id}"
      end
    end
  end

  if node["id"] && node["id"] =~ /\Amedia_comment_(.+)\z/
    "/media_objects/#{$1}"
  elsif node["data-media-id"].present?
    media_iframe_url(node["data-media-id"], node["data-media-type"])
  else
    node.delete("class")
    node.delete("id")
    node.delete("style")
    nil
  end
end

#resolve_module_item_query(_context, query) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/canvas_link_migrator/link_resolver.rb', line 139

def resolve_module_item_query(_context, query)
  return query unless query&.include?("module_item_id=")

  original_param = query.sub("?", "").split("&").detect { |p| p.include?("module_item_id=") }
  mig_id = original_param.split("=").last
  tag_id = @migration_id_converter.convert_context_module_tag_migration_id(mig_id)
  return query unless tag_id

  new_param = "module_item_id=#{tag_id}"
  query.sub(original_param, new_param)
end

#resolve_relative_file_url(rel_path) ⇒ Object



174
175
176
177
178
179
180
181
182
183
# File 'lib/canvas_link_migrator/link_resolver.rb', line 174

def resolve_relative_file_url(rel_path)
  split = rel_path.split("?")
  qs = split.pop if split.length > 1
  path = split.join("?")

  # since we can't be sure whether a ? is part of a filename or query string, try it both ways
  new_url = resolve_relative_file_url_with_qs(path, qs)
  new_url ||= resolve_relative_file_url_with_qs(rel_path, "") if qs.present?
  new_url
end

#resolve_relative_file_url_with_qs(rel_path, qs) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/canvas_link_migrator/link_resolver.rb', line 185

def resolve_relative_file_url_with_qs(rel_path, qs)
  new_url = nil
  rel_path_parts = Pathname.new(rel_path).each_filename.to_a

  # e.g. start with "a/b/c.txt" then try "b/c.txt" then try "c.txt"
  while new_url.nil? && !rel_path_parts.empty?
    sub_path = File.join(rel_path_parts)
    if (file = find_file_in_context(sub_path))
      new_url = "#{context_path}/files/#{file["id"]}"
      # support other params in the query string, that were exported from the
      # original path components and query string. see
      # CCHelper::file_query_string
      params = Rack::Utils.parse_nested_query(qs.presence || "")
      qs = []
      new_action = ""
      params.each do |k, v|
        case k
        when /canvas_qs_(.*)/
          qs << "#{Rack::Utils.escape($1)}=#{Rack::Utils.escape(v)}"
        when /canvas_(.*)/
          new_action += "/#{$1}"
        end
      end
      new_url += new_action.presence || "/preview"
      new_url += "?#{qs.join("&")}" if qs.present?
    end
    rel_path_parts.shift
  end
  new_url
end