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
98cd66ee
Commit
98cd66ee
authored
Apr 03, 2017
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor Geo file uploader to use a Uploader subclass
parent
8111df89
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
81 additions
and
51 deletions
+81
-51
app/services/geo/file_upload_service.rb
app/services/geo/file_upload_service.rb
+14
-51
lib/gitlab/geo/avatar_uploader.rb
lib/gitlab/geo/avatar_uploader.rb
+25
-0
lib/gitlab/geo/file_uploader.rb
lib/gitlab/geo/file_uploader.rb
+24
-0
lib/gitlab/geo/lfs_uploader.rb
lib/gitlab/geo/lfs_uploader.rb
+18
-0
No files found.
app/services/geo/file_upload_service.rb
View file @
98cd66ee
...
...
@@ -2,72 +2,35 @@ module Geo
class
FileUploadService
IAT_LEEWAY
=
60
.
seconds
.
to_i
attr_reader
:
params
,
:auth_header
attr_reader
:
object_type
,
:object_db_id
,
:auth_header
def
initialize
(
params
,
auth_header
)
@params
=
params
@object_type
=
params
[
:type
]
@object_db_id
=
params
[
:id
]
@auth_header
=
auth_header
end
def
execute
# Returns { code: :ok, file: CarrierWave File object } upon success
data
=
::
Gitlab
::
Geo
::
JwtRequestDecoder
.
new
(
auth_header
).
decode
return
unless
data
.
present?
response
=
case
params
[
:type
]
when
'avatar'
handle_avatar_geo_request
(
params
[
:id
],
data
)
when
'lfs'
handle_lfs_geo_request
(
params
[
:id
],
data
)
else
{}
end
response
end
def
handle_avatar_geo_request
(
id
,
message
)
status
=
{
code: :not_found
,
message:
'Avatar file not found'
}
upload
=
Upload
.
find
(
id
)
return
status
unless
upload
.
present?
if
message
[
:id
]
!=
upload
.
model_id
||
message
[
:type
]
!=
upload
.
model_type
||
message
[
:checksum
]
!=
upload
.
checksum
return
status
end
unless
upload
.
model
.
avatar
&
.
exists?
status
[
:message
]
=
"
#{
upload
.
model_type
}
does not have a avatar"
return
status
begin
uploader_class
.
new
(
object_db_id
,
data
).
execute
rescue
NameError
log
(
"unknown file type:
#{
object_type
}
"
)
{}
end
status
[
:code
]
=
:ok
status
[
:message
]
=
'Success'
status
[
:file
]
=
upload
.
model
.
avatar
status
end
def
handle_lfs_geo_request
(
id
,
message
)
status
=
{
code: :not_found
,
message:
'LFS object not found'
}
lfs_object
=
LfsObject
.
find
(
id
)
private
return
status
unless
lfs_object
.
present?
if
message
[
:sha256
]
!=
lfs_object
.
oid
return
status
end
unless
lfs_object
.
file
.
present?
&&
lfs_object
.
file
.
exists?
status
[
:message
]
=
"LFS object does not have a file"
return
status
end
def
uploader_class
"Gitlab::Geo::
#{
object_type
.
camelize
}
Uploader"
.
constantize
end
status
[
:code
]
=
:ok
status
[
:message
]
=
"Success"
status
[
:file
]
=
lfs_object
.
file
status
def
log
(
message
)
Rails
.
logger
.
info
"
#{
self
.
class
.
name
}
:
#{
message
}
"
end
end
end
lib/gitlab/geo/avatar_uploader.rb
0 → 100644
View file @
98cd66ee
module
Gitlab
module
Geo
class
AvatarUploader
<
FileUploader
def
execute
upload
=
Upload
.
find
(
object_db_id
)
return
error
unless
upload
.
present?
return
error
unless
valid?
(
upload
)
unless
upload
.
model
.
avatar
&
.
exists?
return
error
(
"
#{
upload
.
model_type
}
does not have a avatar"
)
end
success
(
upload
.
model
.
avatar
)
end
private
def
valid?
(
upload
)
message
[
:id
]
==
upload
.
model_id
&&
message
[
:type
]
==
upload
.
model_type
&&
message
[
:checksum
]
==
upload
.
checksum
end
end
end
end
lib/gitlab/geo/file_uploader.rb
0 → 100644
View file @
98cd66ee
module
Gitlab
module
Geo
class
FileUploader
attr_reader
:object_db_id
,
:message
def
initialize
(
object_db_id
,
message
)
@object_db_id
=
object_db_id
@message
=
message
end
def
execute
raise
NotImplementedError
end
def
success
(
file
)
{
code: :ok
,
message:
'Success'
,
file:
file
}
end
def
error
(
message
=
'File not found'
)
{
code: :not_found
,
message:
message
}
end
end
end
end
lib/gitlab/geo/lfs_uploader.rb
0 → 100644
View file @
98cd66ee
module
Gitlab
module
Geo
class
LfsUploader
<
FileUploader
def
execute
lfs_object
=
LfsObject
.
find
(
object_db_id
)
return
error
unless
lfs_object
.
present?
return
error
if
message
[
:sha256
]
!=
lfs_object
.
oid
unless
lfs_object
.
file
.
present?
&&
lfs_object
.
file
.
exists?
return
error
(
'LFS object does not have a file'
)
end
success
(
lfs_object
.
file
)
end
end
end
end
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