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

#_find_directories, #_fix_path, #_matches_directory_xid?, #_search_and_replace, #fix_html, get_pre_data, #strip_xid

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)


80
81
82
83
84
# File 'lib/senkyoshi/models/file.rb', line 80

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)


56
57
58
# File 'lib/senkyoshi/models/file.rb', line 56

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)


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/senkyoshi/models/file.rb', line 63

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)


89
90
91
92
93
94
# File 'lib/senkyoshi/models/file.rb', line 89

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



35
36
37
38
39
40
41
42
43
44
# File 'lib/senkyoshi/models/file.rb', line 35

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



49
50
51
# File 'lib/senkyoshi/models/file.rb', line 49

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

#extract_file(entry) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/senkyoshi/models/file.rb', line 25

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)


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

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