fix logic for import for file
This commit is contained in:
parent
007da54954
commit
8879598540
|
@ -162,7 +162,7 @@ end
|
||||||
uploaded_io = params[:import_data]
|
uploaded_io = params[:import_data]
|
||||||
safe_filename = uploaded_io.original_filename.gsub(/[^0-9A-Za-z.\-_]/, '_')
|
safe_filename = uploaded_io.original_filename.gsub(/[^0-9A-Za-z.\-_]/, '_')
|
||||||
table_id = params[:universal_table_id]
|
table_id = params[:universal_table_id]
|
||||||
site_locales = @site_in_use_locales.join(",") # e.g., "en,zh"
|
site_locales = @site_in_use_locales.join(":") # e.g., "en,zh"
|
||||||
|
|
||||||
unless uploaded_io
|
unless uploaded_io
|
||||||
render json: { success: false, msg: "No file uploaded." } and return
|
render json: { success: false, msg: "No file uploaded." } and return
|
||||||
|
@ -173,10 +173,10 @@ end
|
||||||
File.open(tmp_path, 'wb') do |file|
|
File.open(tmp_path, 'wb') do |file|
|
||||||
file.write(uploaded_io.read)
|
file.write(uploaded_io.read)
|
||||||
end
|
end
|
||||||
Rails.logger.info "rake universal_table_tasks:import[#{tmp_path},#{table_id},#{site_locales}]"
|
Rails.logger.info "rake universal_table_tasks:import['#{tmp_path}','#{table_id}','#{site_locales}']"
|
||||||
# Call the Rake task with file path
|
# Call the Rake task with file path
|
||||||
Thread.new do
|
Thread.new do
|
||||||
system("rake universal_table_tasks:import[#{tmp_path},#{table_id},#{site_locales}] >> #{Rails.root}/log/rake.log &")
|
system("rake universal_table_tasks:import['#{tmp_path}','#{table_id}','#{site_locales}'] >> #{Rails.root}/log/rake.log &")
|
||||||
end
|
end
|
||||||
|
|
||||||
render json: {
|
render json: {
|
||||||
|
|
|
@ -45,16 +45,17 @@ wb.add_worksheet(name: "Structure") do |sheet|
|
||||||
|
|
||||||
when "file"
|
when "file"
|
||||||
# 多語系 file_title 欄位
|
# 多語系 file_title 欄位
|
||||||
|
row << "#{column.title}"
|
||||||
|
row1 << column.key
|
||||||
|
row2 << "Separate the files by ;"
|
||||||
|
|
||||||
@site_in_use_locales.sort.each do |locale|
|
@site_in_use_locales.sort.each do |locale|
|
||||||
row << "#{column.title} - #{t(locale.to_s)}"
|
# URL 欄位
|
||||||
|
row << "#{column.title} (註解) - #{t(locale.to_s)}"
|
||||||
row1 << column.key
|
row1 << column.key
|
||||||
row2 << "Separate the files by"
|
row2 << "file_title - #{locale} ;"
|
||||||
end
|
end
|
||||||
|
|
||||||
# URL 欄位
|
|
||||||
row << "#{column.title} (註解)"
|
|
||||||
row1 << column.key
|
|
||||||
row2 << "file_title - #{locale} ;"
|
|
||||||
end # <-- 正確結束 case 區塊
|
end # <-- 正確結束 case 區塊
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,7 @@ namespace :universal_table_tasks do
|
||||||
|
|
||||||
file_path = args[:file_path]
|
file_path = args[:file_path]
|
||||||
table_id = args[:table_id]
|
table_id = args[:table_id]
|
||||||
site_locales = args[:site_locales].to_s.split(",").map(&:to_sym)
|
site_locales = args[:site_locales].to_s.split(":").map(&:to_sym)
|
||||||
|
|
||||||
if !file_path || !File.exist?(file_path)
|
if !file_path || !File.exist?(file_path)
|
||||||
puts "Invalid or missing file: #{file_path}"
|
puts "Invalid or missing file: #{file_path}"
|
||||||
|
@ -122,19 +122,26 @@ namespace :universal_table_tasks do
|
||||||
ce.remote_image_url = val if val.present?
|
ce.remote_image_url = val if val.present?
|
||||||
|
|
||||||
when "file"
|
when "file"
|
||||||
file_urls = val.to_s.split(";").map(&:strip)
|
file_urls = val.to_s.split(";").map(&:strip)
|
||||||
file_titles = row[col_idx + 1]&.value.to_s.split(";").map(&:strip)
|
file_titles = {}
|
||||||
skip = 1
|
skip = site_locales.size
|
||||||
|
site_locales.each_with_index do |locale, locale_idx|
|
||||||
|
file_titles[locale.to_s] = row[col_idx + locale_idx + 1]&.value.to_s.split(";").map(&:strip)
|
||||||
|
end
|
||||||
ce.column_entry_files.destroy_all
|
ce.column_entry_files.destroy_all
|
||||||
ce.column_entry_files = []
|
ce.column_entry_files = []
|
||||||
|
|
||||||
file_urls.each_with_index do |remote_url, file_idx|
|
file_urls.each_with_index do |remote_url, file_idx|
|
||||||
|
if remote_url.blank?
|
||||||
|
skip = site_locales.size
|
||||||
|
end
|
||||||
next if remote_url.blank?
|
next if remote_url.blank?
|
||||||
|
debugger
|
||||||
file = ColumnEntryFile.new
|
file = ColumnEntryFile.new
|
||||||
file.remote_file_url = remote_url
|
file.remote_file_url = remote_url
|
||||||
titles = {}
|
titles = {}
|
||||||
site_locales.each do |locale|
|
site_locales.each_with_index do |locale, locale_idx|
|
||||||
titles[locale.to_s] = file_titles[file_idx] rescue file.file.file.filename
|
titles[locale.to_s] = file_titles[locale.to_s][file_idx] rescue file.file.file.filename
|
||||||
end
|
end
|
||||||
file.file_title_translations = titles
|
file.file_title_translations = titles
|
||||||
file.save!
|
file.save!
|
||||||
|
|
Loading…
Reference in New Issue