Class: Senkyoshi::SenkyoshiFile

Inherits:
Resource
  • Object
show all
Defined in:
lib/senkyoshi/models/file.rb

Constant Summary collapse

FILE_BLACKLIST =
[
  "*.dat",
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#_search_and_replace, #fix_html

Constructor Details

#initialize(zip_entry) ⇒ SenkyoshiFile

Returns a new instance of SenkyoshiFile.



12
13
14
15
16
17
18
19
# File 'lib/senkyoshi/models/file.rb', line 12

def initialize(zip_entry)
  @path = strip_xid zip_entry.name
  @location = extract_file(zip_entry) # Location of file on local filesystem

  base_name = File.basename(zip_entry.name)
  @xid = base_name[/__(xid-[0-9]+_[0-9]+)/, 1] ||
    Senkyoshi.create_random_hex
end

Instance Attribute Details

#locationObject

Returns the value of attribute location.



6
7
8
# File 'lib/senkyoshi/models/file.rb', line 6

def location
  @location
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/senkyoshi/models/file.rb', line 6

def path
  @path
end

#xidObject

Returns the value of attribute xid.



6
7
8
# File 'lib/senkyoshi/models/file.rb', line 6

def xid
  @xid
end

Class Method Details

.belongs_to_scorm_package?(package_paths, file) ⇒ Boolean

Determine whether or not a file is a part of a scorm package

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/senkyoshi/models/file.rb', line 84

def self.belongs_to_scorm_package?(package_paths, file)
  package_paths.any? do |path|
    File.dirname(file.name).start_with? path
  end
end

.blacklisted?(file) ⇒ Boolean

Determine if a file is on the blacklist

Returns:

  • (Boolean)


60
61
62
# File 'lib/senkyoshi/models/file.rb', line 60

def self.blacklisted?(file)
  FILE_BLACKLIST.any? { |list_item| File.fnmatch?(list_item, file.name) }
end

.metadata_file?(entry_names, file) ⇒ Boolean

Determine whether or not a file is a metadata file or not

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/senkyoshi/models/file.rb', line 67

def self.(entry_names, file)
  if File.extname(file.name) == ".xml"
    # Detect and skip metadata files.
    non_meta_file = File.join(
      File.dirname(file.name),
      File.basename(file.name, ".xml"),
    )

    entry_names.include?(non_meta_file)
  else
    false
  end
end

.valid_file?(entry_names, scorm_paths, file) ⇒ Boolean

Determine if a file should be included in course files or not

Returns:

  • (Boolean)


93
94
95
96
97
98
# File 'lib/senkyoshi/models/file.rb', line 93

def self.valid_file?(entry_names, scorm_paths, file)
  return false if SenkyoshiFile.blacklisted? file
  return false if SenkyoshiFile. entry_names, file
  return false if SenkyoshiFile.belongs_to_scorm_package? scorm_paths, file
  true
end

Instance Method Details

#canvas_conversion(course, _resources = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/senkyoshi/models/file.rb', line 39

def canvas_conversion(course, _resources = nil)
  file = CanvasCc::CanvasCC::Models::CanvasFile.new
  file.identifier = @xid
  file.file_location = @location
  file.file_path = @path
  file.hidden = false

  course.files << file
  course
end

#cleanupObject

Remove temporary files



53
54
55
# File 'lib/senkyoshi/models/file.rb', line 53

def cleanup
  FileUtils.rm_r @dir unless @dir.nil?
end

#extract_file(entry) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/senkyoshi/models/file.rb', line 29

def extract_file(entry)
  @dir ||= Dir.mktmpdir

  name = "#{@dir}/#{entry.name}"
  path = File.dirname(name)
  FileUtils.mkdir_p path unless Dir.exist? path
  entry.extract(name)
  name
end

#matches_xid?(xid) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/senkyoshi/models/file.rb', line 25

def matches_xid?(xid)
  @xid == xid
end

#strip_xid(name) ⇒ Object



21
22
23
# File 'lib/senkyoshi/models/file.rb', line 21

def strip_xid(name)
  name.gsub(/__xid-[0-9]+_[0-9]+/, "")
end