add download student image zip

This commit is contained in:
BOHUNG 2020-02-05 00:37:03 +08:00
parent 991f2dd71f
commit 4e7a7056a5
7 changed files with 149 additions and 6 deletions

View File

@ -3,7 +3,9 @@ class Admin::OlympiamanagementsController < OrbitAdminController
require 'rubyXL' require 'rubyXL'
require 'fileutils' require 'fileutils'
require "axlsx" require "axlsx"
before_action :create_first_fields
helper Admin::OlympiamanagementsHelper helper Admin::OlympiamanagementsHelper
load File.expand_path(__dir__)+'/zipgenerator.rb'
def index def index
end end
def add_school_data def add_school_data
@ -223,6 +225,12 @@ class Admin::OlympiamanagementsController < OrbitAdminController
tmp_filename_data = File.read(dir_path +@filename) tmp_filename_data = File.read(dir_path +@filename)
send_data(tmp_filename_data, type: 'application/xlsx', disposition: 'attachment', filename: @filename) send_data(tmp_filename_data, type: 'application/xlsx', disposition: 'attachment', filename: @filename)
end end
def sign_up_student_data_list
page_num = params[:page] || 1
@field_infos = StudentDataField.first.student_data_fields
@sign_up_setting_id = SignUpSetting.last.id
@student_fields = OlympiaStudentDataField.where(:olympia_school_data_fields_id=>params[:olympia_school_id],:sign_up_setting_id=>@sign_up_setting_id).asc(:id).page(page_num).per(10)
end
def export_sign_up_student_data def export_sign_up_student_data
# @OlympiaSchoolDataFields = OlympiaSchoolDataFields.all.asc(:id) # @OlympiaSchoolDataFields = OlympiaSchoolDataFields.all.asc(:id)
# dir_path = 'tmp/olypiamanagement/' # dir_path = 'tmp/olypiamanagement/'
@ -238,10 +246,47 @@ class Admin::OlympiamanagementsController < OrbitAdminController
# send_data(tmp_filename_data, type: 'application/xlsx', disposition: 'attachment', filename: @filename) # send_data(tmp_filename_data, type: 'application/xlsx', disposition: 'attachment', filename: @filename)
end end
def download_scan_file_of_certificate def download_scan_file_of_certificate
@sign_up_setting_id = SignUpSetting.last.id
@student_data_list = OlympiaStudentDataField.where(:sign_up_setting_id=>@sign_up_setting_id)
@student_data_image_path_arr = @student_data_list.flat_map{|student_data| student_data.olympia_student_images}.map{|student_image| student_image.student_file.file.file}
zip_path = "tmp/student_images/#{@sign_up_setting_id}/"
tmp_filename = ''
FileUtils.rm_r(zip_path, :force => true) if Dir.exist?(zip_path)
FileUtils.mkdir_p zip_path #create dir for storing tmp_file
@student_data_image_path_arr.each do |image_path|
@pathname = Pathname.new(image_path)
@temp_name = @pathname.basename
Dir.chdir(@pathname.dirname.to_s) do
@index = 1
@original_filename =@pathname.basename.to_s
@new_pathname = Pathname.new("#{Rails.root}/#{zip_path}#{@original_filename}")
while @new_pathname.exist? do
@new_pathname = Pathname.new(@new_pathname.to_s.insert(@new_pathname.to_s.rindex('.'),"-#{@index}"))
@index += 1
end
#a[0...b].concat('-1').concat(a[b..-1])
FileUtils.cp(@original_filename,@new_pathname)
end
end
@filename = Pathname.new(zip_path).parent.to_s+"/#{Time.now.strftime('%Y_%m_%d')}_student_images.zip"
zip_file= ZipFileGenerator.new(zip_path,@filename)
begin
zip_file.write
rescue
File.delete(@filename)
zip_file.write
end
tmp_filename_data = File.read(@filename)
send_data(tmp_filename_data, type: 'application/zip', disposition: 'attachment', filename: Pathname.new(@filename).basename.to_s)
end end
def import_editing_number_of_school_class def import_editing_number_of_school_class
@olympia_school_data_field = OlympiaSchoolDataFields.new @olympia_school_data_field = OlympiaSchoolDataFields.new
end end
def create_first_fields
StudentDataField.create if StudentDataField.all.length == 0
SchoolDataFields.create if StudentDataField.all.length == 0
SignUpSetting.create if SignUpSetting.all.length == 0
end
def download_import_file def download_import_file
@OlympiaSchoolDataFields = OlympiaSchoolDataFields.all.asc(:id) @OlympiaSchoolDataFields = OlympiaSchoolDataFields.all.asc(:id)
dir_path = 'tmp/olypiamanagement/' dir_path = 'tmp/olypiamanagement/'

View File

@ -0,0 +1,61 @@
require 'zip'
# This is a simple example which uses rubyzip to
# recursively generate a zip file from the contents of
# a specified directory. The directory itself is not
# included in the archive, rather just its contents.
#
# Usage:
# directory_to_zip = "/tmp/input"
# output_file = "/tmp/out.zip"
# zf = ZipFileGenerator.new(directory_to_zip, output_file)
# zf.write()
class ZipFileGenerator
# Initialize with the directory to zip and the location of the output archive.
def initialize(input_dir, output_file)
@input_dir = input_dir
@output_file = output_file
end
# Zip the input directory.
def write
entries = Dir.entries(@input_dir) - %w[. ..]
::Zip::File.open(@output_file, ::Zip::File::CREATE) do |zipfile|
write_entries entries, '', zipfile
end
end
def extract_zip
FileUtils.mkdir_p(@output_file)
Zip::File.open(@input_dir) do |zip_file|
zip_file.each do |f|
fpath = File.join(@output_file, f.name)
zip_file.extract(f, fpath) unless File.exist?(fpath)
end
end
end
private
# A helper method to make the recursion work.
def write_entries(entries, path, zipfile)
entries.each do |e|
zipfile_path = path == '' ? e : File.join(path, e)
disk_file_path = File.join(@input_dir, zipfile_path)
if File.directory? disk_file_path
recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
else
put_into_archive(disk_file_path, zipfile, zipfile_path)
end
end
end
def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
zipfile.mkdir zipfile_path
subdir = Dir.entries(disk_file_path) - %w[. ..]
write_entries subdir, zipfile_path, zipfile
end
def put_into_archive(disk_file_path, zipfile, zipfile_path)
zipfile.add(zipfile_path, disk_file_path)
end
end

View File

@ -19,7 +19,7 @@ class OlympiaImagesUploader < CarrierWave::Uploader::Base
# Override the directory where uploaded files will be stored. # Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted: # This is a sensible default for uploaders that are meant to be mounted:
def store_dir def store_dir
return "uploads/#{model.class.to_s.underscore}/olympia_students" return "uploads/#{model.class.to_s.underscore}/olympia_students/#{model.id}/"
end end
# override # override

View File

@ -33,7 +33,7 @@
<% else %> <% else %>
<td> <td>
<a href="<%=admin_olympiamanagements_add_school_data_path+"?id=#{field.id}"%>"><img src="/assets/edit.gif" alt="<%= t('olympiamanagement.edit') %>" align="top" width="24"><%= t('olympiamanagement.edit') %></a> <a href="<%=admin_olympiamanagements_add_school_data_path+"?id=#{field.id}"%>"><img src="/assets/edit.gif" alt="<%= t('olympiamanagement.edit') %>" align="top" width="24"><%= t('olympiamanagement.edit') %></a>
<a href="<%=admin_olympiamanagements_add_school_data_path+"?id=#{field.id}"%>"><img src="/assets/view.gif" alt="<%= t('olympiamanagement.sign_up_student_data') %>" align="top" width="24"><%= t('olympiamanagement.sign_up_student_data') %></a> <a href="<%=admin_olympiamanagements_sign_up_student_data_list_path+"?olympia_school_id=#{field.id}"%>"><img src="/assets/view.gif" alt="<%= t('olympiamanagement.sign_up_student_data') %>" align="top" width="24"><%= t('olympiamanagement.sign_up_student_data') %></a>
<a href="<%=admin_olympiamanagements_update_school_data_path+"?id=#{field.id}&type=delete_school_data"%>"><img src="/assets/trash.gif" alt="<%= t('olympiamanagement.delete') %>" align="top" width="24"><%= t('olympiamanagement.delete') %></a> <a href="<%=admin_olympiamanagements_update_school_data_path+"?id=#{field.id}&type=delete_school_data"%>"><img src="/assets/trash.gif" alt="<%= t('olympiamanagement.delete') %>" align="top" width="24"><%= t('olympiamanagement.delete') %></a>
<%if field.approved%> <%if field.approved%>
<a href="<%=admin_olympiamanagements_update_school_data_path+"?id=#{field.id}&type=unapproved"%>"><img src="/assets/close.gif" alt="<%= t('olympiamanagement.unapproved') %>" align="top" width="24"><%= t('olympiamanagement.unapproved') %></a> <a href="<%=admin_olympiamanagements_update_school_data_path+"?id=#{field.id}&type=unapproved"%>"><img src="/assets/close.gif" alt="<%= t('olympiamanagement.unapproved') %>" align="top" width="24"><%= t('olympiamanagement.unapproved') %></a>

View File

@ -0,0 +1,36 @@
<link rel="stylesheet" media="screen" type="text/css" href="/assets/admin/olympiamanagement.scss">
<link rel="stylesheet" media="print" type="text/css" href="/assets/admin/olympiamanagement.scss">
<script src="/assets/admin/olympiamanagement.js"></script>
<% th_name_list = ['StudentIdentity','StudentName','StudentClass','StudentArea','StudentPhone','action']%>
<table cellspacing="0" class="ts_rowsonly" width="100%" id="RSS_Table_page_contest_admin_1">
<thead>
<tr>
<% th_name_list.each do |th_name|%>
<th><%= t('olympiamanagement.'+th_name) %></th>
<% end %>
</tr>
</thead>
<tbody>
<% @student_fields.each do |field| %>
<tr>
<% th_name_list.each do |th_name|%>
<%if th_name != 'action'%>
<% @value = field[th_name] %>
<% @index = @field_infos.map{|hash| hash.keys[0]}.index(th_name) rescue nil%>
<% if !@index.nil? %>
<% (['String','Year','select_num','File'].include?(@field_infos.map{|hash| hash.values[0]}[@index]['type'])) ? (nil) : (@value=(@field_infos.map{|hash| hash.values[0]}[@index]['values'][I18n.locale.to_s][@value.to_i].to_s rescue @value))%>
<% end %>
<td><%= @value %></td>
<% else %>
<td>
<a href="<%=add_sign_up_student_data_olympiamanagements_path+"?id=#{field.id}"%>"><img src="/assets/edit.gif" alt="<%= t('olympiamanagement.edit') %>" align="top" width="24"><%= t('olympiamanagement.edit') %></a>
<a href="<%=updata_student_data_olympiamanagements_path+"?id=#{field.id}&type=delete_student_data"%>"><img src="/assets/trash.gif" alt="<%= t('olympiamanagement.delete') %>" align="top" width="24"><%= t('olympiamanagement.delete') %></a>
</td>
<% end %>
<% end %>
</tr>
<%end%>
</tbody>
</table>
<% @extra_params = request.query_parameters.map{|key,value| key+'='+value}.join('&') %>
<%=create_pagination(params[:page].to_i,@student_fields,@extra_params).html_safe%>

View File

@ -2,17 +2,17 @@
<div class="controls"> <div class="controls">
<!-- if this page editing please add class "fileupload-edit" --> <!-- if this page editing please add class "fileupload-edit" -->
<div class="fileupload fileupload-new clearfix" data-provides="fileupload"> <div class="fileupload fileupload-new clearfix" data-provides="fileupload">
<% if(image_form.send(image_field_name).first.send(field_name).file.present? rescue false) %> <% if(image_form.send(image_field_name).last.send(field_name).file.present? rescue false) %>
<div class="fileupload-new thumbnail pull-left"> <div class="fileupload-new thumbnail pull-left">
<%= image_tag( image_form.send(image_field_name).first.send(field_name), :size=>"120x120") %> <%= image_tag( image_form.send(image_field_name).last.send(field_name), :size=>"120x120") %>
</div> </div>
<% end %> <% end %>
<div class="fileupload-preview fileupload-exists thumbnail pull-left"></div> <div class="fileupload-preview fileupload-exists thumbnail pull-left"></div>
<% if(image_form.send(image_field_name).first.send(field_name).file.present? rescue false)%> <% if(image_form.send(image_field_name).last.send(field_name).file.present? rescue false)%>
<span class="btn btn-file"> <span class="btn btn-file">
<%= f.fields_for image_field_name do |index| %> <%= f.fields_for image_field_name do |index| %>
<%= index.fields_for "0" do |image| %> <%= index.fields_for "0" do |image| %>
<%= image.hidden_field(:_id,{:value=>image_form.send(image_field_name).first.id.to_s}) %> <%= image.hidden_field(:_id,{:value=>image_form.send(image_field_name).last.id.to_s}) %>
<%= image.file_field field_name, {:id => (image_id rescue "input-upload"), :class => ('fileupload-new'+(@extra_class rescue "")), :accept=> 'image/png,image/gif,image/jpeg'} %><%= t('preferences.change') %> <%= image.file_field field_name, {:id => (image_id rescue "input-upload"), :class => ('fileupload-new'+(@extra_class rescue "")), :accept=> 'image/png,image/gif,image/jpeg'} %><%= t('preferences.change') %>
<% end %> <% end %>
<% end %> <% end %>

View File

@ -20,6 +20,7 @@ Rails.application.routes.draw do
patch 'olympiamanagements/update_sign_up_data' , to: 'olympiamanagements#update_sign_up_data' patch 'olympiamanagements/update_sign_up_data' , to: 'olympiamanagements#update_sign_up_data'
get 'olympiamanagements/update_sign_up_data' , to: 'olympiamanagements#update_sign_up_data' get 'olympiamanagements/update_sign_up_data' , to: 'olympiamanagements#update_sign_up_data'
get 'olympiamanagements/download_import_file' , to: 'olympiamanagements#download_import_file' get 'olympiamanagements/download_import_file' , to: 'olympiamanagements#download_import_file'
get 'olympiamanagements/sign_up_student_data_list' , to: 'olympiamanagements#sign_up_student_data_list'
resources :olympiamanagements resources :olympiamanagements
end end
resource 'olympiamanagements' do resource 'olympiamanagements' do