canvas-lms/app/models/zip_file_import.rb

47 lines
1.9 KiB
Ruby
Raw Normal View History

2011-02-01 09:57:29 +08:00
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
class ZipFileImport < Tableless
attr_accessor :zip_file, :context, :folder_id, :batch_id
attr_reader :unzip_attachment, :root_directory, :unzip_attachment
attr_accessible :zip_file, :context, :folder_id, :batch_id
validates_presence_of :zip_file, :context, :folder_id
validates_each :zip_file do |record, attr, value|
record.errors.add attr, t('errors.must_upload_file', 'Must Upload A file') unless record.zip_file.class == Tempfile
record.errors.add attr, t('errors.file_must_be_zip', 'The file must be a valid .zip archive') unless record.zip_file.respond_to?(:content_type) && record.zip_file.content_type.to_s.strip.match(/application\/(x-)?zip/)
2011-02-01 09:57:29 +08:00
end
def process!
return false unless valid?
@root_directory = context.folders.find(folder_id)
begin
@unzip_attachment = UnzipAttachment.process(
:batch_id => batch_id,
:course => context,
:root_directory => @root_directory,
:filename => zip_file.path
)
rescue => e
@error = ErrorReport.log_exception(:default, e)
self.errors.add :zip_file, t('errors.unexpected', "Unexpected Error (%{error_id}) while processing zipped files", :error_id => @error.id)
2011-02-01 09:57:29 +08:00
return false
end
end
end