Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
whaleback
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
whaleback
Commits
de61bff9
Commit
de61bff9
authored
Nov 06, 2019
by
Ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix ChunkFile
parent
70cd4f75
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
36 additions
and
17 deletions
+36
-17
2_create_whaleback_chunk_files.rb
db/migrate/2_create_whaleback_chunk_files.rb
+2
-2
whaleback.rb
lib/whaleback.rb
+6
-0
chunk_file.rb
lib/whaleback/chunk_file.rb
+27
-15
document.rb
lib/whaleback/media/obj/document.rb
+1
-0
media_column.rb
lib/whaleback/media_column.rb
+0
-0
No files found.
db/migrate/2_create_chunk_files.rb
→
db/migrate/2_create_
whaleback_
chunk_files.rb
View file @
de61bff9
class
CreateChunkFiles
<
ActiveRecord
::
Migration
[
5.1
]
class
Create
Whaleback
ChunkFiles
<
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:
'当前文件块的大小'
...
...
lib/whaleback.rb
View file @
de61bff9
...
...
@@ -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
lib/whaleback/chunk_file.rb
View file @
de61bff9
...
...
@@ -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
(
f
lo
er
,
"
#{
filename
}
.part
#{
chunk_number
}
.loading"
)
File
.
join
(
f
old
er
,
"
#{
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
lib/whaleback/media/obj/document.rb
View file @
de61bff9
...
...
@@ -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"
...
...
lib/whaleback/media_column.rb
View file @
de61bff9
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment