Class: DiscourseEmojis::ZipProcessor
- Inherits:
-
Object
- Object
- DiscourseEmojis::ZipProcessor
show all
- Defined in:
- lib/discourse_emojis/zip_processor.rb
Defined Under Namespace
Classes: DownloadError, ExtractionError
Class Method Summary
collapse
Class Method Details
.cleanup(paths) ⇒ Object
80
81
82
|
# File 'lib/discourse_emojis/zip_processor.rb', line 80
def self.cleanup(paths)
paths.each { |path| FileUtils.remove_entry(path) if path && File.exist?(path) }
end
|
.download_remote_file(url, destination) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/discourse_emojis/zip_processor.rb', line 44
def self.download_remote_file(url, destination)
uri = URI(url)
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
http.request(Net::HTTP::Get.new(uri)) do |response|
case response
when Net::HTTPRedirection
return download_remote_file(response["location"], destination)
when Net::HTTPSuccess
File.binwrite(destination, response.body)
else
raise DownloadError, "Failed to download: #{response.code} #{response.message}"
end
end
end
end
|
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/discourse_emojis/zip_processor.rb', line 60
def self.(zip_path, destination)
FileUtils.mkdir_p(destination)
Zip::File.open(zip_path) do |zip_file|
zip_file.each { |entry| (entry, destination) }
end
rescue Zip::Error => e
raise ExtractionError, "Failed to extract zip file: #{e.message}"
rescue StandardError => e
raise ExtractionError, "Error extracting zip: #{e.message}"
end
|
72
73
74
75
76
77
78
|
# File 'lib/discourse_emojis/zip_processor.rb', line 72
def self.(entry, destination)
entry_path = File.join(destination, entry.name)
return if File.exist?(entry_path)
FileUtils.mkdir_p(File.dirname(entry_path))
entry.(entry_path)
end
|
.remote_url?(url) ⇒ Boolean
40
41
42
|
# File 'lib/discourse_emojis/zip_processor.rb', line 40
def self.remote_url?(url)
url.start_with?("http", "https")
end
|
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/discourse_emojis/zip_processor.rb', line 15
def self.(url, &block)
zip_path = File.join(Dir.tmpdir, File.basename(url))
= Dir.mktmpdir
begin
download(url, zip_path)
(zip_path, )
yield()
ensure
cleanup([, zip_path])
end
end
|