Commit de61bff9 by Ivan Lan

Fix ChunkFile

parent 70cd4f75
class CreateChunkFiles < ActiveRecord::Migration[5.1]
class CreateWhalebackChunkFiles < ActiveRecord::Migration[5.1]
def change
create_table :chunk_files do |t|
create_table :whaleback_chunk_files do |t|
t.integer :chunk_number, comment: '文件块数,从1开始'
t.integer :chunk_size, comment: '文件块的大小'
t.integer :current_chunk_size, comment: '当前文件块的大小'
......
......@@ -14,4 +14,10 @@ module Whaleback
autoload :Obj, 'whaleback/media/obj'
autoload :Attachment, 'whaleback/media/attachment'
end
def self.table_name_prefix
'whaleback_'
end
autoload :ChunkFile, 'whaleback/chunk_file'
end
......@@ -16,8 +16,10 @@
module Whaleback
class ChunkFile < ApplicationRecord
LocalPath = './public/chunk_file'.freeze
attr_accessor :io
attr_accessor :file, :creator, :media
after_save :write_file
after_destroy :destroy_file
def folder
File.join(ChunkFile::LocalPath, identifier)
......@@ -32,12 +34,12 @@ module Whaleback
end
def uploading_file_path
File.join(floer, "#{filename}.part#{chunk_number}.loading")
File.join(folder, "#{filename}.part#{chunk_number}.loading")
end
def all_finish?
finish_indexs = Dir['./*.part?'].map { |path| /.part(\d+)/.match(path)[1] }.sort
finish_indexs == (1..total_chunks)
finish_indexs = Dir["#{folder}/*.part?"].map { |path| /.part(\d+)/.match(path)[1].to_i }.sort
finish_indexs == (1..total_chunks).to_a
end
def finish_loading
......@@ -49,7 +51,7 @@ module Whaleback
def write_file
FileUtils.mkdir_p folder
File.open(uploading_file_path, 'wb') do |file|
file.write(@io)
file.write(@file&.read)
end
finish_loading
merge_file if all_finish?
......@@ -57,27 +59,37 @@ module Whaleback
def merge_file
FileUtils.remove file_path, force: true
File.open(file_path, 'ab') do |file|
(1..chunk_number).each do |index|
File.open(filepath, 'ab') do |file|
file.write File.open("#{file_path}.part#{index}", 'rb').read
end
end
save_attachment
save_media
FileUtils.remove_dir(folder, force: true)
end
def save_media
mime_type = Rack::Mime.mime_type(File.extname(file_path))
@media = assign_media_class(mime_type).create!(
file: File.open(file_path, 'rb'),
creator: @creator
)
end
def save_attachment filepath
mime_type = Rack::Mime.mime_type(File.extname(filepath))
# 可覆盖,Obj class 继承有 STI type 问题,也可以细化分类
def assign_media_class mime_type
case mime_type
when /^image/
attachment = Image.new
Media::Obj::Image
when /^video/
attachment = Video.new
Media::Obj::Video
else
attachment = Document.new
Media::Obj::Document
end
attachment.file = File.open(filepath, 'rb')
attachment.creator = current_auth
attachment.save!
end
def destroy_file
FileUtils.rm([file_path, chunk_file_path, uploading_file_path], force: true)
end
end
end
......@@ -31,6 +31,7 @@ module Whaleback
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-powerpoint",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/wps-writer",
"text/plain",
"application/zip",
"application/json"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment