Class: ThemeStore::BaseImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/theme_store/base_importer.rb

Direct Known Subclasses

DirectoryImporter, GitImporter, ZipImporter

Instance Method Summary collapse

Instance Method Details

#[](value) ⇒ Object



9
10
11
12
13
# File 'lib/theme_store/base_importer.rb', line 9

def [](value)
  fullpath = real_path(value)
  return nil unless fullpath
  File.read(fullpath)
end

#all_filesObject



35
36
37
# File 'lib/theme_store/base_importer.rb', line 35

def all_files
  Dir.glob("**/**", base: temp_folder).reject { |f| File.directory?(File.join(temp_folder, f)) }
end

#cleanup!Object



39
40
41
# File 'lib/theme_store/base_importer.rb', line 39

def cleanup!
  FileUtils.rm_rf(temp_folder)
end

#file_size(path) ⇒ Object



29
30
31
32
33
# File 'lib/theme_store/base_importer.rb', line 29

def file_size(path)
  fullpath = real_path(path)
  return -1 unless fullpath
  File.size(fullpath)
end

#import!Object



5
6
7
# File 'lib/theme_store/base_importer.rb', line 5

def import!
  raise "Not implemented"
end

#real_path(relative) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/theme_store/base_importer.rb', line 15

def real_path(relative)
  fullpath = "#{temp_folder}/#{relative}"
  return nil unless File.exist?(fullpath)

  # careful to handle symlinks here, don't want to expose random data
  fullpath = Pathname.new(fullpath).realpath.to_s

  if fullpath && fullpath.start_with?(temp_folder)
    fullpath
  else
    nil
  end
end

#temp_folderObject



43
44
45
# File 'lib/theme_store/base_importer.rb', line 43

def temp_folder
  @temp_folder ||= "#{Pathname.new(Dir.tmpdir).realpath}/discourse_theme_#{SecureRandom.hex}"
end