2017-06-19 20:05:37 +08:00
|
|
|
module ImportRepository
|
|
|
|
class ParseRepository
|
|
|
|
include ActionView::Helpers::TextHelper
|
|
|
|
def initialize(options)
|
|
|
|
@file = options.fetch(:file)
|
|
|
|
@repository = options.fetch(:repository)
|
|
|
|
@session = options.fetch(:session)
|
2017-10-17 20:42:06 +08:00
|
|
|
@sheet = SpreadsheetParser.open_spreadsheet(@file)
|
2017-06-19 20:05:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
2017-10-17 20:42:06 +08:00
|
|
|
header, columns = SpreadsheetParser.first_two_rows(@sheet)
|
2017-06-19 20:05:37 +08:00
|
|
|
# Fill in fields for dropdown
|
2018-03-13 18:36:05 +08:00
|
|
|
@repository.importable_repository_fields.transform_values! do |name|
|
2017-06-19 20:05:37 +08:00
|
|
|
truncate(name, length: Constants::NAME_TRUNCATION_LENGTH_DROPDOWN)
|
|
|
|
end
|
|
|
|
Data.new(header,
|
2017-07-18 20:54:35 +08:00
|
|
|
columns,
|
2018-03-13 18:36:05 +08:00
|
|
|
@repository.importable_repository_fields,
|
2017-10-12 20:43:25 +08:00
|
|
|
@repository)
|
2017-06-19 20:05:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def too_large?
|
2018-06-19 20:15:14 +08:00
|
|
|
@file.size > Rails.configuration.x.file_max_size_mb.megabytes
|
2017-06-19 20:05:37 +08:00
|
|
|
end
|
|
|
|
|
2018-05-31 16:45:31 +08:00
|
|
|
def has_too_many_rows?
|
|
|
|
@sheet.last_row > Constants::IMPORT_REPOSITORY_ITEMS_LIMIT
|
|
|
|
end
|
|
|
|
|
2017-10-12 20:43:25 +08:00
|
|
|
def generate_temp_file
|
2017-06-19 20:05:37 +08:00
|
|
|
# Save file for next step (importing)
|
2017-10-12 20:43:25 +08:00
|
|
|
temp_file = TempFile.new(
|
2017-06-19 20:05:37 +08:00
|
|
|
session_id: @session.id,
|
|
|
|
file: @file
|
|
|
|
)
|
|
|
|
|
2017-10-12 20:43:25 +08:00
|
|
|
if temp_file.save
|
2018-09-21 23:46:18 +08:00
|
|
|
TempFile.destroy_obsolete(temp_file.id)
|
2017-10-12 20:43:25 +08:00
|
|
|
return temp_file
|
2017-06-19 20:05:37 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Data = Struct.new(
|
2017-10-12 20:43:25 +08:00
|
|
|
:header, :columns, :available_fields, :repository
|
2017-06-19 20:05:37 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|