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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
74ddc805
Commit
74ddc805
authored
Jan 29, 2018
by
Micaël Bergeron
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add the uploader context to the upload model
parent
54a575f1
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
85 additions
and
30 deletions
+85
-30
app/controllers/concerns/uploads_actions.rb
app/controllers/concerns/uploads_actions.rb
+1
-1
app/models/upload.rb
app/models/upload.rb
+15
-3
app/uploaders/file_mover.rb
app/uploaders/file_mover.rb
+2
-2
app/uploaders/file_uploader.rb
app/uploaders/file_uploader.rb
+21
-7
app/uploaders/gitlab_uploader.rb
app/uploaders/gitlab_uploader.rb
+4
-0
app/uploaders/records_uploads.rb
app/uploaders/records_uploads.rb
+7
-6
db/migrate/20180129193323_add_uploads_builder_context.rb
db/migrate/20180129193323_add_uploads_builder_context.rb
+14
-0
lib/gitlab/gfm/uploads_rewriter.rb
lib/gitlab/gfm/uploads_rewriter.rb
+1
-1
spec/factories/uploads.rb
spec/factories/uploads.rb
+12
-8
spec/models/upload_spec.rb
spec/models/upload_spec.rb
+6
-0
spec/uploaders/file_uploader_spec.rb
spec/uploaders/file_uploader_spec.rb
+1
-1
spec/uploaders/gitlab_uploader_spec.rb
spec/uploaders/gitlab_uploader_spec.rb
+1
-1
No files found.
app/controllers/concerns/uploads_actions.rb
View file @
74ddc805
...
@@ -70,7 +70,7 @@ module UploadsActions
...
@@ -70,7 +70,7 @@ module UploadsActions
end
end
def
build_uploader_from_params
def
build_uploader_from_params
uploader
=
uploader_class
.
new
(
model
,
params
[
:secret
])
uploader
=
uploader_class
.
new
(
model
,
secret:
params
[
:secret
])
uploader
.
retrieve_from_store!
(
params
[
:filename
])
uploader
.
retrieve_from_store!
(
params
[
:filename
])
uploader
uploader
end
end
...
...
app/models/upload.rb
View file @
74ddc805
...
@@ -30,7 +30,8 @@ class Upload < ActiveRecord::Base
...
@@ -30,7 +30,8 @@ class Upload < ActiveRecord::Base
end
end
def
build_uploader
def
build_uploader
uploader_class
.
new
(
model
).
tap
do
|
uploader
|
uploader_class
.
new
(
model
,
mount_point
,
**
uploader_context
)
.
tap
do
|
uploader
|
uploader
.
upload
=
self
uploader
.
upload
=
self
uploader
.
retrieve_from_store!
(
identifier
)
uploader
.
retrieve_from_store!
(
identifier
)
end
end
...
@@ -40,6 +41,13 @@ class Upload < ActiveRecord::Base
...
@@ -40,6 +41,13 @@ class Upload < ActiveRecord::Base
File
.
exist?
(
absolute_path
)
File
.
exist?
(
absolute_path
)
end
end
def
uploader_context
{
identifier:
identifier
,
secret:
secret
}.
compact
end
private
private
def
checksummable?
def
checksummable?
...
@@ -62,11 +70,15 @@ class Upload < ActiveRecord::Base
...
@@ -62,11 +70,15 @@ class Upload < ActiveRecord::Base
!
path
.
start_with?
(
'/'
)
!
path
.
start_with?
(
'/'
)
end
end
def
uploader_class
Object
.
const_get
(
uploader
)
end
def
identifier
def
identifier
File
.
basename
(
path
)
File
.
basename
(
path
)
end
end
def
uploader_class
def
mount_point
Object
.
const_get
(
uploader
)
super
&
.
to_sym
end
end
end
end
app/uploaders/file_mover.rb
View file @
74ddc805
...
@@ -49,11 +49,11 @@ class FileMover
...
@@ -49,11 +49,11 @@ class FileMover
end
end
def
uploader
def
uploader
@uploader
||=
PersonalFileUploader
.
new
(
model
,
secret
)
@uploader
||=
PersonalFileUploader
.
new
(
model
,
secret
:
secret
)
end
end
def
temp_file_uploader
def
temp_file_uploader
@temp_file_uploader
||=
PersonalFileUploader
.
new
(
nil
,
secret
)
@temp_file_uploader
||=
PersonalFileUploader
.
new
(
nil
,
secret
:
secret
)
end
end
def
revert
def
revert
...
...
app/uploaders/file_uploader.rb
View file @
74ddc805
...
@@ -62,9 +62,11 @@ class FileUploader < GitlabUploader
...
@@ -62,9 +62,11 @@ class FileUploader < GitlabUploader
attr_accessor
:model
attr_accessor
:model
def
initialize
(
model
,
secret
=
nil
)
def
initialize
(
model
,
mounted_as
=
nil
,
**
uploader_context
)
super
(
model
,
nil
,
**
uploader_context
)
@model
=
model
@model
=
model
@secret
=
secret
apply_context!
(
uploader_context
)
end
end
def
base_dir
def
base_dir
...
@@ -107,12 +109,12 @@ class FileUploader < GitlabUploader
...
@@ -107,12 +109,12 @@ class FileUploader < GitlabUploader
self
.
file
.
filename
self
.
file
.
filename
end
end
# the upload does not hold the secret, but holds the path
# which contains the secret: extract it
def
upload
=
(
value
)
def
upload
=
(
value
)
if
matches
=
DYNAMIC_PATH_PATTERN
.
match
(
value
.
path
)
unless
apply_context!
(
value
.
uploader_context
)
@secret
=
matches
[
:secret
]
if
matches
=
DYNAMIC_PATH_PATTERN
.
match
(
value
.
path
)
@identifier
=
matches
[
:identifier
]
@secret
=
matches
[
:secret
]
@identifier
=
matches
[
:identifier
]
end
end
end
super
super
...
@@ -124,6 +126,18 @@ class FileUploader < GitlabUploader
...
@@ -124,6 +126,18 @@ class FileUploader < GitlabUploader
private
private
def
apply_context!
(
uploader_context
)
@secret
,
@identifier
=
uploader_context
.
values_at
(
:secret
,
:identifier
)
!!
(
@secret
&&
@identifier
)
end
def
build_upload
super
.
tap
do
|
upload
|
upload
.
secret
=
secret
end
end
def
markdown_name
def
markdown_name
(
image_or_video?
?
File
.
basename
(
filename
,
File
.
extname
(
filename
))
:
filename
).
gsub
(
"]"
,
"
\\
]"
)
(
image_or_video?
?
File
.
basename
(
filename
,
File
.
extname
(
filename
))
:
filename
).
gsub
(
"]"
,
"
\\
]"
)
end
end
...
...
app/uploaders/gitlab_uploader.rb
View file @
74ddc805
...
@@ -29,6 +29,10 @@ class GitlabUploader < CarrierWave::Uploader::Base
...
@@ -29,6 +29,10 @@ class GitlabUploader < CarrierWave::Uploader::Base
delegate
:base_dir
,
:file_storage?
,
to: :class
delegate
:base_dir
,
:file_storage?
,
to: :class
def
initialize
(
model
,
mounted_as
=
nil
,
**
uploader_context
)
super
(
model
,
mounted_as
)
end
def
file_cache_storage?
def
file_cache_storage?
cache_storage
.
is_a?
(
CarrierWave
::
Storage
::
File
)
cache_storage
.
is_a?
(
CarrierWave
::
Storage
::
File
)
end
end
...
...
app/uploaders/records_uploads.rb
View file @
74ddc805
...
@@ -24,7 +24,7 @@ module RecordsUploads
...
@@ -24,7 +24,7 @@ module RecordsUploads
uploads
.
where
(
path:
upload_path
).
delete_all
uploads
.
where
(
path:
upload_path
).
delete_all
upload
.
destroy!
if
upload
upload
.
destroy!
if
upload
self
.
upload
=
build_upload
_from_uploader
(
self
)
self
.
upload
=
build_upload
upload
.
save!
upload
.
save!
end
end
end
end
...
@@ -39,12 +39,13 @@ module RecordsUploads
...
@@ -39,12 +39,13 @@ module RecordsUploads
Upload
.
order
(
id: :desc
).
where
(
uploader:
self
.
class
.
to_s
)
Upload
.
order
(
id: :desc
).
where
(
uploader:
self
.
class
.
to_s
)
end
end
def
build_upload
_from_uploader
(
uploader
)
def
build_upload
Upload
.
new
(
Upload
.
new
(
size:
uploader
.
file
.
size
,
uploader:
self
.
class
.
to_s
,
path:
uploader
.
upload_path
,
size:
file
.
size
,
model:
uploader
.
model
,
path:
upload_path
,
uploader:
uploader
.
class
.
to_s
model:
model
,
mount_point:
mounted_as
)
)
end
end
...
...
db/migrate/20180129193323_add_uploads_builder_context.rb
0 → 100644
View file @
74ddc805
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class
AddUploadsBuilderContext
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
# Set this constant to true if this migration requires downtime.
DOWNTIME
=
false
def
change
add_column
:uploads
,
:mount_point
,
:string
add_column
:uploads
,
:secret
,
:string
end
end
lib/gitlab/gfm/uploads_rewriter.rb
View file @
74ddc805
...
@@ -46,7 +46,7 @@ module Gitlab
...
@@ -46,7 +46,7 @@ module Gitlab
private
private
def
find_file
(
project
,
secret
,
file
)
def
find_file
(
project
,
secret
,
file
)
uploader
=
FileUploader
.
new
(
project
,
secret
)
uploader
=
FileUploader
.
new
(
project
,
secret
:
secret
)
uploader
.
retrieve_from_store!
(
file
)
uploader
.
retrieve_from_store!
(
file
)
uploader
.
file
uploader
.
file
end
end
...
...
spec/factories/uploads.rb
View file @
74ddc805
...
@@ -3,36 +3,40 @@ FactoryBot.define do
...
@@ -3,36 +3,40 @@ FactoryBot.define do
model
{
build
(
:project
)
}
model
{
build
(
:project
)
}
size
100
.
kilobytes
size
100
.
kilobytes
uploader
"AvatarUploader"
uploader
"AvatarUploader"
mount_point
:avatar
secret
nil
# we should build a mount agnostic upload by default
# we should build a mount agnostic upload by default
transient
do
transient
do
mounted_as
:avatar
filename
'myfile.jpg'
secret
SecureRandom
.
hex
end
end
# this needs to comply with RecordsUpload::Concern#upload_path
# this needs to comply with RecordsUpload::Concern#upload_path
path
{
File
.
join
(
"uploads/-/system"
,
model
.
class
.
to_s
.
underscore
,
mount
ed_as
.
to_s
,
'avatar.jpg'
)
}
path
{
File
.
join
(
"uploads/-/system"
,
model
.
class
.
to_s
.
underscore
,
mount
_point
.
to_s
,
'avatar.jpg'
)
}
trait
:personal_snippet_upload
do
trait
:personal_snippet_upload
do
model
{
build
(
:personal_snippet
)
}
path
{
File
.
join
(
secret
,
'myfile.jpg'
)
}
uploader
"PersonalFileUploader"
uploader
"PersonalFileUploader"
path
{
File
.
join
(
secret
,
filename
)
}
model
{
build
(
:personal_snippet
)
}
secret
SecureRandom
.
hex
end
end
trait
:issuable_upload
do
trait
:issuable_upload
do
path
{
File
.
join
(
secret
,
'myfile.jpg'
)
}
uploader
"FileUploader"
uploader
"FileUploader"
path
{
File
.
join
(
secret
,
filename
)
}
secret
SecureRandom
.
hex
end
end
trait
:namespace_upload
do
trait
:namespace_upload
do
model
{
build
(
:group
)
}
model
{
build
(
:group
)
}
path
{
File
.
join
(
secret
,
'myfile.jpg'
)
}
path
{
File
.
join
(
secret
,
filename
)
}
uploader
"NamespaceFileUploader"
uploader
"NamespaceFileUploader"
secret
SecureRandom
.
hex
end
end
trait
:attachment_upload
do
trait
:attachment_upload
do
transient
do
transient
do
mount
ed_as
:attachment
mount
_point
:attachment
end
end
model
{
build
(
:note
)
}
model
{
build
(
:note
)
}
...
...
spec/models/upload_spec.rb
View file @
74ddc805
...
@@ -103,4 +103,10 @@ describe Upload do
...
@@ -103,4 +103,10 @@ describe Upload do
expect
(
upload
).
not_to
exist
expect
(
upload
).
not_to
exist
end
end
end
end
describe
"#uploader_context"
do
subject
{
create
(
:upload
,
:issuable_upload
,
secret:
'secret'
,
filename:
'file.txt'
)
}
it
{
expect
(
subject
.
uploader_context
).
to
match
(
a_hash_including
(
secret:
'secret'
,
identifier:
'file.txt'
))
}
end
end
end
spec/uploaders/file_uploader_spec.rb
View file @
74ddc805
...
@@ -40,7 +40,7 @@ describe FileUploader do
...
@@ -40,7 +40,7 @@ describe FileUploader do
end
end
describe
'initialize'
do
describe
'initialize'
do
let
(
:uploader
)
{
described_class
.
new
(
double
,
'secret'
)
}
let
(
:uploader
)
{
described_class
.
new
(
double
,
secret:
'secret'
)
}
it
'accepts a secret parameter'
do
it
'accepts a secret parameter'
do
expect
(
described_class
).
not_to
receive
(
:generate_secret
)
expect
(
described_class
).
not_to
receive
(
:generate_secret
)
...
...
spec/uploaders/gitlab_uploader_spec.rb
View file @
74ddc805
...
@@ -4,7 +4,7 @@ require 'carrierwave/storage/fog'
...
@@ -4,7 +4,7 @@ require 'carrierwave/storage/fog'
describe
GitlabUploader
do
describe
GitlabUploader
do
let
(
:uploader_class
)
{
Class
.
new
(
described_class
)
}
let
(
:uploader_class
)
{
Class
.
new
(
described_class
)
}
subject
{
uploader_class
.
new
}
subject
{
uploader_class
.
new
(
double
)
}
describe
'#file_storage?'
do
describe
'#file_storage?'
do
context
'when file storage is used'
do
context
'when file storage is used'
do
...
...
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