Skip to content

Commit

Permalink
Support .json.zip and .json formats
Browse files Browse the repository at this point in the history
  • Loading branch information
epugh committed Oct 28, 2024
1 parent 1712e7f commit a7880de
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
33 changes: 26 additions & 7 deletions app/controllers/books/import_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'open-uri'
require 'json'
require 'zip'

module Books
class ImportController < ApplicationController
Expand All @@ -10,8 +11,8 @@ def new
end

# rubocop:disable Metrics/MethodLength
# rubocop:disable Security/Open
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/PerceivedComplexity
def create
@book = Book.new
@book.owner = current_user
Expand All @@ -20,13 +21,25 @@ def create
force_create_users = bool.deserialize params[:book][:force_create_users]

uploaded_file = params[:book][:import_file]
json_file = uploaded_file.tempfile
json_data = URI.open(json_file) do |file|
JSON.parse(file.read)
end
tempfile = uploaded_file.tempfile
json_data = if 'application/zip' == uploaded_file.content_type || uploaded_file.path.end_with?('.zip')
# Handle zip file
Zip::File.open(tempfile.path) do |zip_file|
# Assuming the zip contains only one JSON file
json_file_entry = zip_file.entries.find { |e| e.name.end_with?('.json') }
if json_file_entry
json_file_entry.get_input_stream { |io| read_json(io) }
else
raise 'No JSON file found in the zip.'
end
end
else
# Handle normal JSON file
read_json(tempfile)
end

begin
params_to_use = JSON.parse(json_data.read).deep_symbolize_keys
params_to_use = json_data.deep_symbolize_keys

@book.name = params_to_use[:name]

Expand All @@ -49,7 +62,13 @@ def create
end
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Security/Open
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/PerceivedComplexity

private

def read_json file
JSON.parse(file.read)
end
end
end
55 changes: 55 additions & 0 deletions test/controllers/books/import_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

# test/controllers/books/import_controller_test.rb
require 'test_helper'
require 'zip'

module Books
class BooksControllerTest < ActionDispatch::IntegrationTest
let(:user) { users(:random) }

def setup
@valid_json = { 'scorer' => { 'name' =>'AP@10' }, selection_strategy: { 'name'=>'Single Rater' } }
@json_file = Tempfile.new([ 'test', '.json' ])
@json_file.write(@valid_json.to_json)
@json_file.rewind

@zip_file = Tempfile.new([ 'test', '.zip' ])
Zip::File.open(@zip_file, Zip::File::CREATE) do |zipfile|
zipfile.add(File.basename(@json_file), @json_file)
end

# get the login page
get '/books'
assert_equal 302, status
follow_redirect!

login_user_for_integration_test user
end

def teardown
@json_file.close
@json_file.unlink
@zip_file.close
@zip_file.unlink
end

test 'should import a valid JSON file' do
json_upload = Rack::Test::UploadedFile.new(@json_file.path, 'application/json')

post books_import_index_url, params: { book: { import_file: json_upload } }

assert_response :redirect
assert_redirected_to book_path(Book.last)
end

test 'should import a valid ZIP file containing a JSON file' do
zip_upload = Rack::Test::UploadedFile.new(@zip_file.path, 'application/zip')

post books_import_index_url, params: { book: { import_file: zip_upload } }

assert_response :redirect
assert_redirected_to book_path(Book.last)
end
end
end

0 comments on commit a7880de

Please sign in to comment.