Skip to content

Commit bed2545

Browse files
committed
export segments and audio files
1 parent ea6d8c9 commit bed2545

File tree

4 files changed

+78
-57
lines changed

4 files changed

+78
-57
lines changed

app/views/admin/_export_audio_segments_db.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<li>
1515
<label for="export_gapless">Export gapless audio segments</label>
16-
<%= form.check_box :export_gapless %>
16+
<%= form.check_box :export_gapless, checked: true %>
1717
</li>
1818

1919
<li>

lib/audio_segment/tarteel.rb

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ def export_json_files
5252
reciter_dir = File.join(root_tmp, file_label, recitation.id.to_s)
5353
FileUtils.mkdir_p(reciter_dir)
5454

55-
export_gapless_recitation_jsons(recitation, reciter_dir)
55+
export_gapless_recitation_segments(recitation, reciter_dir)
56+
export_gapless_recitation_audio_files(recitation, reciter_dir)
5657
end
5758

5859
master_zip = "#{file_path}.zip"
@@ -122,11 +123,9 @@ def get_segments_data(recitation)
122123
end
123124

124125
def get_gapless_segments_data(recitation)
125-
recitation_id = get_cloned_id(recitation.id)
126-
127126
segments = Audio::Segment
128127
.where(
129-
audio_recitation_id: recitation_id
128+
audio_recitation_id: recitation.id
130129
)
131130
.order('chapter_id ASC, verse_number ASC')
132131
.includes(:verse)
@@ -156,34 +155,9 @@ def get_gapless_segments_data(recitation)
156155
end
157156
end
158157

159-
CLONED_IDS = {
160-
1 => 186,
161-
2 => 187,
162-
3 => 188,
163-
4 => 189,
164-
5 => 192,
165-
6 => 191,
166-
7 => 192,
167-
8 => 193,
168-
9 => 194,
169-
10 => 195,
170-
12 => 196,
171-
13 => 197,
172-
173-
65 => 198,
174-
161 => 199,
175-
164 => 201,
176-
174 => 202,
177-
175 => 203,
178-
}
179-
def get_cloned_id(id)
180-
CLONED_IDS[id] || id
181-
end
182-
183-
def export_gapless_recitation_jsons(recitation, target_dir)
184-
recitation_id = get_cloned_id(recitation.id)
158+
def export_gapless_recitation_segments(recitation, target_dir)
185159
segments = Audio::Segment
186-
.where(audio_recitation_id: recitation_id)
160+
.where(audio_recitation_id: recitation.id)
187161
.order('chapter_id ASC, verse_number ASC')
188162
.includes(:verse)
189163

@@ -229,7 +203,38 @@ def export_gapless_recitation_jsons(recitation, target_dir)
229203
File.open(json_path, "w") do |f|
230204
f.write(JSON.dump(json_ayahs))
231205
end
206+
207+
uploader = UploadToCdn.new
208+
key = "audio/gapless/#{recitation.get_resource_content.id}/segments.json"
209+
uploader.upload(json_path, key)
210+
211+
json_path
212+
end
213+
end
214+
215+
def export_gapless_recitation_audio_files(recitation, target_dir)
216+
audio_files = recitation.chapter_audio_files.order('chapter_id ASC')
217+
218+
json_data = audio_files.map do |audio_file|
219+
{
220+
surah: audio_file.chapter_id,
221+
version: audio_file.updated_at.to_i,
222+
url: audio_file.audio_url
223+
}
232224
end
225+
226+
json_filename = "audio_files.json"
227+
json_path = File.join(target_dir, json_filename)
228+
229+
File.open(json_path, "w") do |f|
230+
f.write(JSON.dump(json_data))
231+
end
232+
233+
uploader = UploadToCdn.new
234+
key = "audio/gapless/#{recitation.get_resource_content.id}/audio_files.json"
235+
uploader.upload(json_path, key)
236+
237+
json_path
233238
end
234239

235240
def prepare_db

lib/export_assets_manifest.rb

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,8 @@ def upload(manifest_path: nil)
2626
manifest_path ||= export
2727
object_key = "asset-manifests/#{File.basename(manifest_path)}"
2828

29-
bucket.object(object_key)
30-
.upload_file(
31-
manifest_path,
32-
content_type: 'application/json'
33-
)
34-
35-
"#{content_cnd_host}/#{object_key}"
29+
uploader = UploadToCdn.new
30+
uploader.upload(manifest_path, object_key)
3631
end
3732

3833
def export_and_upload
@@ -137,25 +132,7 @@ def build_cdn_url(resource, resource_type:)
137132
end
138133
end
139134

140-
def s3_client
141-
@s3_client ||= Aws::S3::Resource.new(
142-
access_key_id: ENV['QUL_STORAGE_ACCESS_KEY'],
143-
secret_access_key: ENV['QUL_STORAGE_ACCESS_KEY_SECRET'],
144-
region: ENV['QUL_STORAGE_REGION'],
145-
endpoint: ENV['QUL_STORAGE_ENDPOINT'],
146-
force_path_style: true
147-
)
148-
end
149-
150-
def bucket
151-
@bucket ||= s3_client.bucket(ENV['QUL_STORAGE_BUCKET'])
152-
end
153-
154135
def content_cnd_host
155136
ENV['CDN_HOST']
156137
end
157-
158-
def audio_cdn_host
159-
ENV['AUDIO_CDN_HOST']
160-
end
161138
end

lib/upload_to_cdn.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class UploadToCdn
2+
def upload(file_path, object_key, content_type: 'application/json')
3+
bucket.object(object_key)
4+
.upload_file(
5+
file_path,
6+
content_type
7+
)
8+
9+
url = "#{content_cnd_host}/#{object_key}"
10+
clear_cache(url)
11+
12+
url
13+
end
14+
15+
protected
16+
def clear_cache(url)
17+
CloudflareCacheClearer.new.clear_cache(urls: [url])
18+
rescue => e
19+
Sentry.capture_exception(e, extra: { url: url })
20+
end
21+
22+
def bucket
23+
@bucket ||= s3_client.bucket(ENV['QUL_STORAGE_BUCKET'])
24+
end
25+
26+
def s3_client
27+
@s3_client ||= Aws::S3::Resource.new(
28+
access_key_id: ENV['QUL_STORAGE_ACCESS_KEY'],
29+
secret_access_key: ENV['QUL_STORAGE_ACCESS_KEY_SECRET'],
30+
region: ENV['QUL_STORAGE_REGION'],
31+
endpoint: ENV['QUL_STORAGE_ENDPOINT'],
32+
force_path_style: true
33+
)
34+
end
35+
36+
def content_cnd_host
37+
ENV['CDN_HOST']
38+
end
39+
end

0 commit comments

Comments
 (0)