BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
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
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
d8465091
Commit
d8465091
authored
Jan 30, 2018
by
Micaël Bergeron
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
remove file after `Upload#destroy`
it will also automatically prune empty directories for `FileUploader`-based uploaders.
parent
b9d547b1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
0 deletions
+37
-0
upload.rb
app/models/upload.rb
+8
-0
file_uploader.rb
app/uploaders/file_uploader.rb
+6
-0
file_uploader_spec.rb
spec/uploaders/file_uploader_spec.rb
+23
-0
No files found.
app/models/upload.rb
View file @
d8465091
...
...
@@ -12,6 +12,10 @@ class Upload < ActiveRecord::Base
before_save
:calculate_checksum!
,
if: :foreground_checksummable?
after_commit
:schedule_checksum
,
if: :checksummable?
# as the FileUploader is not mounted, the default CarrierWave ActiveRecord
# hooks are not executed and the file will not be deleted
after_commit
:delete_file!
,
on: :destroy
,
if:
->
{
uploader_class
<=
FileUploader
}
def
self
.
hexdigest
(
path
)
Digest
::
SHA256
.
file
(
path
).
hexdigest
end
...
...
@@ -49,6 +53,10 @@ class Upload < ActiveRecord::Base
private
def
delete_file!
build_uploader
.
remove!
end
def
checksummable?
checksum
.
nil?
&&
local?
&&
exist?
end
...
...
app/uploaders/file_uploader.rb
View file @
d8465091
...
...
@@ -15,6 +15,8 @@ class FileUploader < GitlabUploader
storage
:file
after
:remove
,
:prune_store_dir
def
self
.
root
File
.
join
(
options
.
storage_path
,
'uploads'
)
end
...
...
@@ -140,6 +142,10 @@ class FileUploader < GitlabUploader
end
end
def
prune_store_dir
storage
.
delete_dir!
(
store_dir
)
# only remove when empty
end
def
markdown_name
(
image_or_video?
?
File
.
basename
(
filename
,
File
.
extname
(
filename
))
:
filename
).
gsub
(
"]"
,
"
\\
]"
)
end
...
...
spec/uploaders/file_uploader_spec.rb
View file @
d8465091
...
...
@@ -48,6 +48,29 @@ describe FileUploader do
end
end
describe
'callbacks'
do
describe
'#prune_store_dir after :remove'
do
before
do
uploader
.
store!
(
fixture_file_upload
(
'spec/fixtures/doc_sample.txt'
))
end
def
store_dir
File
.
expand_path
(
uploader
.
store_dir
,
uploader
.
root
)
end
it
'is called'
do
expect
(
uploader
).
to
receive
(
:prune_store_dir
).
once
uploader
.
remove!
end
it
'prune the store directory'
do
expect
{
uploader
.
remove!
}
.
to
change
{
File
.
exists?
(
store_dir
)
}.
from
(
true
).
to
(
false
)
end
end
end
describe
'#secret'
do
it
'generates a secret if none is provided'
do
expect
(
described_class
).
to
receive
(
:generate_secret
).
and_return
(
'secret'
)
...
...
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