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.



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

def initialize(zip_entry)
  begin
    entry_name = zip_entry.name.encode("UTF-8")
  rescue Encoding::UndefinedConversionError
    entry_name = zip_entry.name.force_encoding("UTF-8")
  end

  @path = strip_xid entry_name
  @location = extract_file(zip_entry) # Location of file on local filesystem

  base_name = File.basename(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.



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

def location
  @location
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#xidObject

Returns the value of attribute xid.



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

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)


101
102
103
104
105
# File 'lib/senkyoshi/models/file.rb', line 101

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)


77
78
79
# File 'lib/senkyoshi/models/file.rb', line 77

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)


84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/senkyoshi/models/file.rb', line 84

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)


110
111
112
113
114
115
# File 'lib/senkyoshi/models/file.rb', line 110

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



56
57
58
59
60
61
62
63
64
65
# File 'lib/senkyoshi/models/file.rb', line 56

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



70
71
72
# File 'lib/senkyoshi/models/file.rb', line 70

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

#extract_file(entry) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/senkyoshi/models/file.rb', line 46

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)


42
43
44
# File 'lib/senkyoshi/models/file.rb', line 42

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