-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathproject_importer.rb
More file actions
139 lines (112 loc) · 3.57 KB
/
Copy pathproject_importer.rb
File metadata and controls
139 lines (112 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# frozen_string_literal: true
class ProjectImporter
attr_reader :name, :identifier, :images, :videos, :audio, :media, :components, :type, :locale
def initialize(**kwargs)
@name = kwargs[:name]
@identifier = kwargs[:identifier]
@components = kwargs[:components]
@images = kwargs[:images]
@videos = kwargs[:videos]
@audio = kwargs[:audio]
@media = Array(images) + Array(videos) + Array(audio)
@type = kwargs[:type]
@locale = kwargs[:locale]
end
def import!
Project.transaction do
setup_project
delete_components
create_components
create_scratch_component
create_scratch_assets
delete_removed_media
attach_media_if_needed
project.save!
end
end
private
def project
@project ||= Project.find_or_initialize_by(identifier:, locale:)
end
def setup_project
project.name = name
project.project_type = type
end
def delete_components
return unless project.project_type != 'code_editor_scratch'
project.components.each(&:destroy)
end
def create_components
return unless project.project_type != 'code_editor_scratch'
components.each do |component|
project_component = Component.new(**component)
project.components << project_component
end
end
def create_scratch_component
return unless project.project_type == 'code_editor_scratch'
components.each do |component|
next unless component[:extension] == 'sb3'
parsed_content = Sb3Parser.new(component: component).parse.fetch(:scratch_component).fetch(:content)
project.scratch_component = ScratchComponent.new(content: parsed_content) if parsed_content
end
end
def create_scratch_assets
return unless project.project_type == 'code_editor_scratch'
components.each do |component|
next unless component[:extension] == 'sb3'
parsed_assets = Sb3Parser.new(component: component).parse.fetch(:assets)
ScratchAssetImporter.import_all_from_sb3(parsed_assets)
end
end
def delete_removed_media
return if removed_media_names.empty?
removed_media_names.each do |filename|
media_file = project.media.find { |i| i.blob.filename == filename }
media_file.purge
end
end
def removed_media_names
existing_media = project.media.map { |x| x.blob.filename.to_s }
existing_media - media.pluck(:filename)
end
def attach_media_if_needed
media.each do |media_file|
existing_media_file = find_existing_media_file(media_file[:filename])
if existing_media_file
next if existing_media_file.blob.checksum == media_checksum(media_file[:io])
existing_media_file.purge
end
if images.include?(media_file)
blob = create_blob(media_file)
project.images.attach(blob)
elsif videos.include?(media_file)
blob = create_blob(media_file)
project.videos.attach(blob)
elsif audio.include?(media_file)
blob = create_blob(media_file)
project.audio.attach(blob)
else
raise "Unsupported media file: #{media_file[:filename]}"
end
end
end
def create_blob(media_file)
ActiveStorage::Blob.create_and_upload!(
io: media_file[:io],
filename: media_file[:filename],
content_type: media_file[:content_type]
)
end
def find_existing_media_file(filename)
project.media.find { |i| i.blob.filename == filename }
end
def media_checksum(io)
OpenSSL::Digest.new('MD5').tap do |checksum|
while (chunk = io.read(5.megabytes))
checksum << chunk
end
io.rewind
end.base64digest
end
end