Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tatuya Kamada
gitlab-ce
Commits
3a0be1c5
Commit
3a0be1c5
authored
Feb 15, 2017
by
Robert Speicher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `RecordsUploads` module to record Upload records via callbacks
parent
4c622b71
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
151 additions
and
6 deletions
+151
-6
app/uploaders/attachment_uploader.rb
app/uploaders/attachment_uploader.rb
+1
-0
app/uploaders/avatar_uploader.rb
app/uploaders/avatar_uploader.rb
+1
-0
app/uploaders/file_uploader.rb
app/uploaders/file_uploader.rb
+6
-0
app/uploaders/records_uploads.rb
app/uploaders/records_uploads.rb
+38
-0
spec/uploaders/file_uploader_spec.rb
spec/uploaders/file_uploader_spec.rb
+1
-1
spec/uploaders/records_uploads_spec.rb
spec/uploaders/records_uploads_spec.rb
+97
-0
spec/uploaders/uploader_helper_spec.rb
spec/uploaders/uploader_helper_spec.rb
+7
-5
No files found.
app/uploaders/attachment_uploader.rb
View file @
3a0be1c5
class
AttachmentUploader
<
GitlabUploader
include
RecordsUploads
include
UploaderHelper
storage
:file
...
...
app/uploaders/avatar_uploader.rb
View file @
3a0be1c5
class
AvatarUploader
<
GitlabUploader
include
RecordsUploads
include
UploaderHelper
storage
:file
...
...
app/uploaders/file_uploader.rb
View file @
3a0be1c5
class
FileUploader
<
GitlabUploader
include
RecordsUploads
include
UploaderHelper
MARKDOWN_PATTERN
=
%r{
\!
?
\[
.*?
\]\(
/uploads/(?<secret>[0-9a-f]{32})/(?<file>.*?)
\)
}
storage
:file
...
...
@@ -20,6 +22,10 @@ class FileUploader < GitlabUploader
File
.
join
(
base_dir
,
'tmp'
,
@project
.
path_with_namespace
,
@secret
)
end
def
model
project
end
def
to_markdown
to_h
[
:markdown
]
end
...
...
app/uploaders/records_uploads.rb
0 → 100644
View file @
3a0be1c5
module
RecordsUploads
extend
ActiveSupport
::
Concern
included
do
after
:store
,
:record_upload
before
:remove
,
:destroy_upload
end
private
# After storing an attachment, create a corresponding Upload record
#
# NOTE: We're ignoring the argument passed to this callback because we want
# the `SanitizedFile` object from `CarrierWave::Uploader::Base#file`, not the
# `Tempfile` object the callback gets.
#
# Called `after :store`
def
record_upload
(
_tempfile
)
return
unless
file_storage?
return
unless
file
.
exists?
Upload
.
record
(
self
)
end
# When removing an attachment, destroy any Upload records at the same path
#
# Note: this _will not work_ for Uploaders which relativize paths, such as
# `FileUploader`, but because that uploader uses different paths for every
# upload, that's an acceptable caveat.
#
# Called `before :remove`
def
destroy_upload
(
*
args
)
return
unless
file_storage?
return
unless
file
Upload
.
remove_path
(
relative_path
)
end
end
spec/uploaders/file_uploader_spec.rb
View file @
3a0be1c5
require
'spec_helper'
describe
FileUploader
do
let
(
:uploader
)
{
described_class
.
new
(
build_stubbed
(
:project
))
}
let
(
:uploader
)
{
described_class
.
new
(
build_stubbed
(
:
empty_
project
))
}
describe
'initialize'
do
it
'generates a secret if none is provided'
do
...
...
spec/uploaders/records_uploads_spec.rb
0 → 100644
View file @
3a0be1c5
require
'rails_helper'
describe
RecordsUploads
do
let
(
:uploader
)
do
example_uploader
=
Class
.
new
(
GitlabUploader
)
do
include
RecordsUploads
storage
:file
def
model
FactoryGirl
.
build_stubbed
(
:user
)
end
end
example_uploader
.
new
end
def
upload_fixture
(
filename
)
fixture_file_upload
(
Rails
.
root
.
join
(
'spec'
,
'fixtures'
,
filename
))
end
describe
'callbacks'
do
it
'calls `record_upload` after `store`'
do
expect
(
uploader
).
to
receive
(
:record_upload
).
once
uploader
.
store!
(
upload_fixture
(
'doc_sample.txt'
))
end
it
'calls `destroy_upload` after `remove`'
do
expect
(
uploader
).
to
receive
(
:destroy_upload
).
once
uploader
.
store!
(
upload_fixture
(
'doc_sample.txt'
))
uploader
.
remove!
end
end
describe
'#record_upload callback'
do
it
'returns early when not using file storage'
do
allow
(
uploader
).
to
receive
(
:file_storage?
).
and_return
(
false
)
expect
(
Upload
).
not_to
receive
(
:record
)
uploader
.
store!
(
upload_fixture
(
'rails_sample.jpg'
))
end
it
"returns early when the file doesn't exist"
do
allow
(
uploader
).
to
receive
(
:file
).
and_return
(
double
(
exists?:
false
))
expect
(
Upload
).
not_to
receive
(
:record
)
uploader
.
store!
(
upload_fixture
(
'rails_sample.jpg'
))
end
it
'creates an Upload record after store'
do
expect
(
Upload
).
to
receive
(
:record
)
.
with
(
uploader
)
uploader
.
store!
(
upload_fixture
(
'rails_sample.jpg'
))
end
it
'it destroys Upload records at the same path before recording'
do
existing
=
Upload
.
create!
(
path:
File
.
join
(
CarrierWave
.
root
,
'uploads'
,
'rails_sample.jpg'
),
size:
512
.
kilobytes
,
model:
build_stubbed
(
:user
),
uploader:
described_class
.
to_s
)
uploader
.
store!
(
upload_fixture
(
'rails_sample.jpg'
))
expect
{
existing
.
reload
}.
to
raise_error
(
ActiveRecord
::
RecordNotFound
)
expect
(
Upload
.
count
).
to
eq
1
end
end
describe
'#destroy_upload callback'
do
it
'returns early when not using file storage'
do
uploader
.
store!
(
upload_fixture
(
'rails_sample.jpg'
))
allow
(
uploader
).
to
receive
(
:file_storage?
).
and_return
(
false
)
expect
(
Upload
).
not_to
receive
(
:remove_path
)
uploader
.
remove!
end
it
'returns early when file is nil'
do
expect
(
Upload
).
not_to
receive
(
:remove_path
)
uploader
.
remove!
end
it
'it destroys Upload records at the same path after removal'
do
uploader
.
store!
(
upload_fixture
(
'rails_sample.jpg'
))
expect
{
uploader
.
remove!
}.
to
change
{
Upload
.
count
}.
from
(
1
).
to
(
0
)
end
end
end
spec/uploaders/uploader_helper_spec.rb
View file @
3a0be1c5
require
'rails_helper'
describe
UploaderHelper
do
class
ExampleUploader
<
CarrierWave
::
Uploader
::
Base
let
(
:uploader
)
do
example_uploader
=
Class
.
new
(
CarrierWave
::
Uploader
::
Base
)
do
include
UploaderHelper
storage
:file
end
example_uploader
.
new
end
def
upload_fixture
(
filename
)
fixture_file_upload
(
Rails
.
root
.
join
(
'spec'
,
'fixtures'
,
filename
))
end
describe
'#image_or_video?'
do
let
(
:uploader
)
{
ExampleUploader
.
new
}
it
'returns true for an image file'
do
uploader
.
store!
(
upload_fixture
(
'dk.png'
))
...
...
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