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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gitlab-ce
Commits
21649cab
Commit
21649cab
authored
May 26, 2014
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'fix/drag_and_drop_image_tests' into 'master'
Fix drag and drop image tests
parents
e03a2fa2
9b2a1349
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
122 additions
and
18 deletions
+122
-18
app/controllers/projects_controller.rb
app/controllers/projects_controller.rb
+7
-6
app/services/projects/image_service.rb
app/services/projects/image_service.rb
+39
-0
app/uploaders/file_uploader.rb
app/uploaders/file_uploader.rb
+1
-1
spec/controllers/commits_controller_spec.rb
spec/controllers/commits_controller_spec.rb
+1
-1
spec/controllers/projects_controller_spec.rb
spec/controllers/projects_controller_spec.rb
+9
-10
spec/services/projects/image_service_spec.rb
spec/services/projects/image_service_spec.rb
+65
-0
No files found.
app/controllers/projects_controller.rb
View file @
21649cab
...
@@ -163,13 +163,14 @@ class ProjectsController < ApplicationController
...
@@ -163,13 +163,14 @@ class ProjectsController < ApplicationController
end
end
def
upload_image
def
upload_image
uploader
=
FileUploader
.
new
(
'uploads'
,
upload_path
,
accepted_images
)
link_to_image
=
::
Projects
::
ImageService
.
new
(
repository
,
params
,
root_url
).
execute
alt
=
params
[
'markdown_img'
].
original_filename
uploader
.
store!
(
params
[
'markdown_img'
])
link
=
{
'alt'
=>
File
.
basename
(
alt
,
'.*'
),
'url'
=>
File
.
join
(
root_url
,
uploader
.
url
)
}
respond_to
do
|
format
|
respond_to
do
|
format
|
format
.
json
{
render
json:
{
link:
link
}
}
if
link_to_image
format
.
json
{
render
json:
{
link:
link_to_image
}
}
else
format
.
json
{
render
json:
"Invalid file."
,
status: :unprocessable_entity
}
end
end
end
end
end
...
...
app/services/projects/image_service.rb
0 → 100644
View file @
21649cab
module
Projects
class
ImageService
<
BaseService
include
Rails
.
application
.
routes
.
url_helpers
def
initialize
(
repository
,
params
,
root_url
)
@repository
,
@params
,
@root_url
=
repository
,
params
.
dup
,
root_url
end
def
execute
uploader
=
FileUploader
.
new
(
'uploads'
,
upload_path
,
accepted_images
)
image
=
@params
[
'markdown_img'
]
if
image
&&
correct_mime_type?
(
image
)
alt
=
image
.
original_filename
uploader
.
store!
(
image
)
link
=
{
'alt'
=>
File
.
basename
(
alt
,
'.*'
),
'url'
=>
File
.
join
(
@root_url
,
uploader
.
url
)
}
else
link
=
nil
end
end
protected
def
upload_path
base_dir
=
FileUploader
.
generate_dir
File
.
join
(
@repository
.
path_with_namespace
,
base_dir
)
end
def
accepted_images
%w(png jpg jpeg gif)
end
def
correct_mime_type?
(
image
)
accepted_images
.
map
{
|
format
|
image
.
content_type
.
include?
format
}.
any?
end
end
end
app/uploaders/file_uploader.rb
View file @
21649cab
...
@@ -25,7 +25,7 @@ class FileUploader < CarrierWave::Uploader::Base
...
@@ -25,7 +25,7 @@ class FileUploader < CarrierWave::Uploader::Base
end
end
def
store!
(
file
)
def
store!
(
file
)
file
.
original_
filename
=
self
.
class
.
generate_filename
(
file
)
@
filename
=
self
.
class
.
generate_filename
(
file
)
super
super
end
end
...
...
spec/controllers/commits_controller_spec.rb
View file @
21649cab
...
@@ -6,7 +6,7 @@ describe Projects::CommitsController do
...
@@ -6,7 +6,7 @@ describe Projects::CommitsController do
before
do
before
do
sign_in
(
user
)
sign_in
(
user
)
project
.
creator
=
user
project
.
team
<<
[
user
,
:master
]
end
end
describe
"GET show"
do
describe
"GET show"
do
...
...
spec/controllers/projects_controller_spec.rb
View file @
21649cab
...
@@ -3,41 +3,40 @@ require('spec_helper')
...
@@ -3,41 +3,40 @@ require('spec_helper')
describe
ProjectsController
do
describe
ProjectsController
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:project
)
{
create
(
:project
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:png
)
{
fixture_file_upload
(
Rails
.
root
+
'spec/fixtures/dk.png'
,
'image/png'
)
}
let
(
:jpg
)
{
fixture_file_upload
(
Rails
.
root
+
'spec/fixtures/rails_sample.jpg'
,
'image/jpg'
)
}
let
(
:jpg
)
{
fixture_file_upload
(
Rails
.
root
+
'spec/fixtures/rails_sample.jpg'
,
'image/jpg'
)
}
let
(
:gif
)
{
fixture_file_upload
(
Rails
.
root
+
'spec/fixtures/banana_sample.gif'
,
'image/gif'
)
}
let
(
:txt
)
{
fixture_file_upload
(
Rails
.
root
+
'spec/fixtures/doc_sample.txt'
,
'text/plain'
)
}
let
(
:txt
)
{
fixture_file_upload
(
Rails
.
root
+
'spec/fixtures/doc_sample.txt'
,
'text/plain'
)
}
describe
"POST #upload_image"
do
describe
"POST #upload_image"
do
before
do
before
do
sign_in
(
user
)
sign_in
(
user
)
project
.
team
<<
[
user
,
:developer
]
end
end
context
"without params['markdown_img']"
do
context
"without params['markdown_img']"
do
it
"returns an error"
do
it
"returns an error"
do
post
:upload_image
,
id:
project
.
to_param
post
:upload_image
,
id:
project
.
to_param
,
format: :json
expect
(
response
.
status
).
to
eq
(
4
04
)
expect
(
response
.
status
).
to
eq
(
4
22
)
end
end
end
end
context
"with invalid file"
do
context
"with invalid file"
do
before
do
before
do
post
:upload_image
,
id:
project
.
to_param
,
markdown_img:
@img
post
:upload_image
,
id:
project
.
to_param
,
markdown_img:
txt
,
format: :json
end
end
it
"returns an error"
do
it
"returns an error"
do
expect
(
response
.
status
).
to
eq
(
4
04
)
expect
(
response
.
status
).
to
eq
(
4
22
)
end
end
end
end
context
"with valid file"
do
context
"with valid file"
do
before
do
before
do
post
:upload_image
,
id:
project
.
to_param
,
markdown_img:
@img
post
:upload_image
,
id:
project
.
to_param
,
markdown_img:
jpg
,
format: :json
end
end
it
"returns a content with original filename and new link."
do
it
"returns a content with original filename and new link."
do
link
=
{
alt:
'rails_sample'
,
link:
''
}.
to_json
expect
(
response
.
body
).
to
match
"
\"
alt
\"
:
\"
rails_sample
\"
"
expect
(
response
.
body
).
to
have_content
link
expect
(
response
.
body
).
to
match
"
\"
url
\"
:
\"
http://test.host/uploads/
#{
project
.
path_with_namespace
}
"
end
end
end
end
end
end
...
...
spec/services/projects/image_service_spec.rb
0 → 100644
View file @
21649cab
require
'spec_helper'
describe
Projects
::
ImageService
do
before
(
:each
)
{
enable_observers
}
after
(
:each
)
{
disable_observers
}
describe
'Image service'
do
before
do
@user
=
create
:user
@project
=
create
:project
,
creator_id:
@user
.
id
,
namespace:
@user
.
namespace
end
context
'for valid gif file'
do
before
do
gif
=
fixture_file_upload
(
Rails
.
root
+
'spec/fixtures/banana_sample.gif'
,
'image/gif'
)
@link_to_image
=
upload_image
(
@project
.
repository
,
{
'markdown_img'
=>
gif
},
"http://test.example/"
)
end
it
{
expect
(
@link_to_image
).
to
have_key
(
"alt"
)
}
it
{
expect
(
@link_to_image
).
to
have_key
(
"url"
)
}
it
{
expect
(
@link_to_image
).
to
have_value
(
"banana_sample"
)
}
it
{
expect
(
@link_to_image
[
"url"
]).
to
match
(
"http://test.example/uploads/
#{
@project
.
path_with_namespace
}
"
)
}
it
{
expect
(
@link_to_image
[
"url"
]).
to
match
(
"banana_sample.gif"
)
}
end
context
'for valid png file'
do
before
do
png
=
fixture_file_upload
(
Rails
.
root
+
'spec/fixtures/dk.png'
,
'image/png'
)
@link_to_image
=
upload_image
(
@project
.
repository
,
{
'markdown_img'
=>
png
},
"http://test.example/"
)
end
it
{
expect
(
@link_to_image
).
to
have_key
(
"alt"
)
}
it
{
expect
(
@link_to_image
).
to
have_key
(
"url"
)
}
it
{
expect
(
@link_to_image
).
to
have_value
(
"dk"
)
}
it
{
expect
(
@link_to_image
[
"url"
]).
to
match
(
"http://test.example/uploads/
#{
@project
.
path_with_namespace
}
"
)
}
it
{
expect
(
@link_to_image
[
"url"
]).
to
match
(
"dk.png"
)
}
end
context
'for valid jpg file'
do
before
do
jpg
=
fixture_file_upload
(
Rails
.
root
+
'spec/fixtures/rails_sample.jpg'
,
'image/jpg'
)
@link_to_image
=
upload_image
(
@project
.
repository
,
{
'markdown_img'
=>
jpg
},
"http://test.example/"
)
end
it
{
expect
(
@link_to_image
).
to
have_key
(
"alt"
)
}
it
{
expect
(
@link_to_image
).
to
have_key
(
"url"
)
}
it
{
expect
(
@link_to_image
).
to
have_value
(
"rails_sample"
)
}
it
{
expect
(
@link_to_image
[
"url"
]).
to
match
(
"http://test.example/uploads/
#{
@project
.
path_with_namespace
}
"
)
}
it
{
expect
(
@link_to_image
[
"url"
]).
to
match
(
"rails_sample.jpg"
)
}
end
context
'for txt file'
do
before
do
txt
=
fixture_file_upload
(
Rails
.
root
+
'spec/fixtures/doc_sample.txt'
,
'text/plain'
)
@link_to_image
=
upload_image
(
@project
.
repository
,
{
'markdown_img'
=>
txt
},
"http://test.example/"
)
end
it
{
expect
(
@link_to_image
).
to
be_nil
}
end
end
def
upload_image
(
repository
,
params
,
root_url
)
Projects
::
ImageService
.
new
(
repository
,
params
,
root_url
).
execute
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