Commit d2ebdf66 authored by Douwe Maan's avatar Douwe Maan

Refactor.

parent 192e7306
...@@ -36,6 +36,7 @@ nohup.out ...@@ -36,6 +36,7 @@ nohup.out
public/assets/ public/assets/
public/uploads.* public/uploads.*
public/uploads/ public/uploads/
uploads/
rails_best_practices_output.html rails_best_practices_output.html
tags tags
tmp/ tmp/
......
...@@ -205,6 +205,7 @@ group :development do ...@@ -205,6 +205,7 @@ group :development do
gem "letter_opener" gem "letter_opener"
gem 'quiet_assets', '~> 1.0.1' gem 'quiet_assets', '~> 1.0.1'
gem 'rack-mini-profiler', require: false gem 'rack-mini-profiler', require: false
gem "byebug"
# Better errors handler # Better errors handler
gem 'better_errors' gem 'better_errors'
......
...@@ -61,6 +61,9 @@ GEM ...@@ -61,6 +61,9 @@ GEM
sass (~> 3.2) sass (~> 3.2)
browser (0.7.2) browser (0.7.2)
builder (3.2.2) builder (3.2.2)
byebug (3.2.0)
columnize (~> 0.8)
debugger-linecache (~> 1.2)
cal-heatmap-rails (0.0.1) cal-heatmap-rails (0.0.1)
capybara (2.2.1) capybara (2.2.1)
mime-types (>= 1.16) mime-types (>= 1.16)
...@@ -88,6 +91,7 @@ GEM ...@@ -88,6 +91,7 @@ GEM
coffee-script-source (1.6.3) coffee-script-source (1.6.3)
colored (1.2) colored (1.2)
colorize (0.5.8) colorize (0.5.8)
columnize (0.9.0)
connection_pool (2.1.0) connection_pool (2.1.0)
coveralls (0.7.0) coveralls (0.7.0)
multi_json (~> 1.3) multi_json (~> 1.3)
...@@ -103,6 +107,7 @@ GEM ...@@ -103,6 +107,7 @@ GEM
daemons (1.1.9) daemons (1.1.9)
database_cleaner (1.3.0) database_cleaner (1.3.0)
debug_inspector (0.0.2) debug_inspector (0.0.2)
debugger-linecache (1.2.0)
default_value_for (3.0.0) default_value_for (3.0.0)
activerecord (>= 3.2.0, < 5.0) activerecord (>= 3.2.0, < 5.0)
descendants_tracker (0.0.3) descendants_tracker (0.0.3)
...@@ -646,6 +651,7 @@ DEPENDENCIES ...@@ -646,6 +651,7 @@ DEPENDENCIES
binding_of_caller binding_of_caller
bootstrap-sass (~> 3.0) bootstrap-sass (~> 3.0)
browser browser
byebug
cal-heatmap-rails (~> 0.0.1) cal-heatmap-rails (~> 0.0.1)
capybara (~> 2.2.1) capybara (~> 2.2.1)
carrierwave carrierwave
......
...@@ -9,7 +9,7 @@ class @DropzoneInput ...@@ -9,7 +9,7 @@ class @DropzoneInput
iconPicture = "<i class=\"fa fa-picture-o div-dropzone-icon\"></i>" iconPicture = "<i class=\"fa fa-picture-o div-dropzone-icon\"></i>"
iconSpinner = "<i class=\"fa fa-spinner fa-spin div-dropzone-icon\"></i>" iconSpinner = "<i class=\"fa fa-spinner fa-spin div-dropzone-icon\"></i>"
btnAlert = "<button type=\"button\"" + alertAttr + ">&times;</button>" btnAlert = "<button type=\"button\"" + alertAttr + ">&times;</button>"
project_file_path_upload = window.project_file_path_upload or null project_uploads_path = window.project_uploads_path or null
form_textarea = $(form).find("textarea.markdown-area") form_textarea = $(form).find("textarea.markdown-area")
form_textarea.wrap "<div class=\"div-dropzone\"></div>" form_textarea.wrap "<div class=\"div-dropzone\"></div>"
...@@ -72,10 +72,10 @@ class @DropzoneInput ...@@ -72,10 +72,10 @@ class @DropzoneInput
form.find(".md-preview-holder").hide() form.find(".md-preview-holder").hide()
dropzone = form_dropzone.dropzone( dropzone = form_dropzone.dropzone(
url: project_file_path_upload url: project_uploads_path
dictDefaultMessage: "" dictDefaultMessage: ""
clickable: true clickable: true
paramName: "markdown_file" paramName: "file"
maxFilesize: 10 maxFilesize: 10
uploadMultiple: false uploadMultiple: false
headers: headers:
...@@ -131,10 +131,9 @@ class @DropzoneInput ...@@ -131,10 +131,9 @@ class @DropzoneInput
child = $(dropzone[0]).children("textarea") child = $(dropzone[0]).children("textarea")
formatLink = (str) -> formatLink = (link) ->
text = "[" + str.alt + "](" + str.url + ")" text = "[#{link.alt}](#{link.url})"
if str.is_image is true text = "!#{text}" if link.is_image
text = "!" + text
text text
handlePaste = (event) -> handlePaste = (event) ->
...@@ -179,9 +178,9 @@ class @DropzoneInput ...@@ -179,9 +178,9 @@ class @DropzoneInput
uploadFile = (item, filename) -> uploadFile = (item, filename) ->
formData = new FormData() formData = new FormData()
formData.append "markdown_file", item, filename formData.append "file", item, filename
$.ajax $.ajax
url: project_file_path_upload url: project_uploads_path
type: "POST" type: "POST"
data: formData data: formData
dataType: "json" dataType: "json"
...@@ -235,8 +234,7 @@ class @DropzoneInput ...@@ -235,8 +234,7 @@ class @DropzoneInput
$(@).closest('.gfm-form').find('.div-dropzone').click() $(@).closest('.gfm-form').find('.div-dropzone').click()
return return
formatLink: (str) -> formatLink: (link) ->
text = "[" + str.alt + "](" + str.url + ")" text = "[#{link.alt}](#{link.url})"
if str.is_image is true text = "!#{text}" if link.is_image
text = "!" + text
text text
\ No newline at end of file
...@@ -3,11 +3,11 @@ class FilesController < ApplicationController ...@@ -3,11 +3,11 @@ class FilesController < ApplicationController
note = Note.find(params[:id]) note = Note.find(params[:id])
uploader = note.attachment uploader = note.attachment
if uploader.file_storage?
if can?(current_user, :read_project, note.project) if can?(current_user, :read_project, note.project)
# Replace old notes location in /public with the new one in / and send the file if uploader.file_storage?
path = uploader.file.path.gsub("#{Rails.root}/public", Rails.root.to_s) path = uploader.file.path.gsub("#{Rails.root}/public", Rails.root.to_s)
if File.exist?(path)
disposition = uploader.image? ? 'inline' : 'attachment' disposition = uploader.image? ? 'inline' : 'attachment'
send_file path, disposition: disposition send_file path, disposition: disposition
else else
...@@ -16,5 +16,8 @@ class FilesController < ApplicationController ...@@ -16,5 +16,8 @@ class FilesController < ApplicationController
else else
redirect_to uploader.url redirect_to uploader.url
end end
else
not_found!
end
end end
end end
...@@ -3,14 +3,37 @@ class Projects::UploadsController < Projects::ApplicationController ...@@ -3,14 +3,37 @@ class Projects::UploadsController < Projects::ApplicationController
before_filter :project before_filter :project
def create
link_to_file = ::Projects::UploadService.new(repository, params[:file]).
execute
respond_to do |format|
if link_to_file
format.json do
render json: { link: link_to_file }
end
else
format.json do
render json: 'Invalid file.', status: :unprocessable_entity
end
end
end
end
def show def show
folder_id = params[:folder_id] uploader = FileUploader.new(project, params[:secret])
filename = params[:filename]
uploader = FileUploader.new("#{Rails.root}/uploads","#{@project.path_with_namespace}/#{folder_id}") if uploader.file_storage?
uploader.retrieve_from_store!(filename) uploader.retrieve_from_store!(params[:filename])
if uploader.file.exists?
disposition = uploader.image? ? 'inline' : 'attachment' disposition = uploader.image? ? 'inline' : 'attachment'
send_file uploader.file.path, disposition: disposition send_file uploader.file.path, disposition: disposition
else
not_found!
end
else
redirect_to uploader.url
end
end end
end end
...@@ -134,19 +134,6 @@ class ProjectsController < ApplicationController ...@@ -134,19 +134,6 @@ class ProjectsController < ApplicationController
end end
end end
def upload_file
link_to_file = ::Projects::FileService.new(repository, params, root_url).
execute
respond_to do |format|
if link_to_file
format.json { render json: { link: link_to_file } }
else
format.json { render json: 'Invalid file.', status: :unprocessable_entity }
end
end
end
def toggle_star def toggle_star
current_user.toggle_star(@project) current_user.toggle_star(@project)
@project.reload @project.reload
...@@ -159,10 +146,6 @@ class ProjectsController < ApplicationController ...@@ -159,10 +146,6 @@ class ProjectsController < ApplicationController
private private
def invalid_file(error)
render json: { message: error.message }, status: :internal_server_error
end
def set_title def set_title
@title = 'New Project' @title = 'New Project'
end end
......
module Projects
class FileService < 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("#{Rails.root}/uploads", upload_path, accepted_files)
file = @params['markdown_file']
if file
alt = file.original_filename
uploader.store!(file)
filename = nil
if image?(file)
filename=File.basename(alt, '.*')
else
filename=File.basename(alt)
end
link = {
'alt' => filename,
'url' => uploader.secure_url,
'is_image' => image?(file)
}
else
link = nil
end
end
protected
def accepted_files
# insert accepted mime types here (e.g %w(jpg jpeg gif png))
nil
end
def accepted_images
%w(jpg jpeg gif png)
end
def image?(file)
accepted_images.map { |format| file.content_type.include? format }.any?
end
def upload_path
base_dir = FileUploader.generate_dir
File.join(@repository.path_with_namespace, base_dir)
end
def correct_mime_type?(file)
accepted_files.map { |format| image.content_type.include? format }.any?
end
end
end
module Projects
class UploadService < BaseService
def initialize(project, file)
@project, @file = project, file
end
def execute
return nil unless @file
uploader = FileUploader.new(@project)
uploader.store!(@file)
filename = uploader.image? ? uploader.file.basename : uploader.file.filename
{
'alt' => filename,
'url' => uploader.secure_url,
'is_image' => uploader.image?
}
end
end
end
...@@ -21,7 +21,7 @@ class AttachmentUploader < CarrierWave::Uploader::Base ...@@ -21,7 +21,7 @@ class AttachmentUploader < CarrierWave::Uploader::Base
end end
def secure_url def secure_url
Gitlab.config.gitlab.relative_url_root + "/files/#{model.class.to_s.underscore}/#{model.id}/#{file.filename}" File.join(Gitlab.config.gitlab.relative_url_root, "files", model.class.to_s.underscore, model.id.to_s, file.filename)
end end
def file_storage? def file_storage?
......
...@@ -2,47 +2,33 @@ ...@@ -2,47 +2,33 @@
class FileUploader < CarrierWave::Uploader::Base class FileUploader < CarrierWave::Uploader::Base
storage :file storage :file
def initialize(base_dir, path = '', allowed_extensions = nil) def initialize(project, secret = self.class.generate_secret)
@base_dir = base_dir @project = project
@path = path @secret = secret
@allowed_extensions = allowed_extensions
end end
def base_dir def base_dir
@base_dir "#{Rails.root}/uploads"
end end
def store_dir def store_dir
File.join(@base_dir, @path) File.join(base_dir, @project.path_with_namespace, @secret)
end end
def cache_dir def cache_dir
File.join(@base_dir, 'tmp', @path) File.join(base_dir, 'tmp', @project.path_with_namespace, @secret)
end end
def extension_white_list def self.generate_secret
@allowed_extensions || super
end
def store!(file)
@filename = self.class.generate_filename(file)
super
end
def self.generate_filename(file)
original_filename = File.basename(file.original_filename, '.*')
extension = File.extname(file.original_filename)
new_filename = Digest::MD5.hexdigest(original_filename) + extension
end
def self.generate_dir
SecureRandom.hex(5) SecureRandom.hex(5)
end end
def secure_url def secure_url
path_array = @path.split('/') File.join(Gitlab.config.gitlab.relative_url_root, @project.path_with_namespace, "uploads", @secret, file.filename)
path = File.join(path_array[0],path_array[1],'uploads',path_array[2]) end
Gitlab.config.gitlab.relative_url_root + "/#{path}/#{@filename}"
def file_storage?
self.class.storage == CarrierWave::Storage::File
end end
def image? def image?
......
...@@ -11,4 +11,4 @@ ...@@ -11,4 +11,4 @@
e.preventDefault(); e.preventDefault();
}); });
window.project_file_path_upload = "#{upload_file_project_path @project}"; window.project_uploads_path = "#{project_uploads_path @project}";
...@@ -9,4 +9,4 @@ ...@@ -9,4 +9,4 @@
e.preventDefault(); e.preventDefault();
}); });
window.project_file_path_upload = "#{upload_file_project_path @project}"; window.project_uploads_path = "#{project_uploads_path @project}";
...@@ -113,7 +113,7 @@ ...@@ -113,7 +113,7 @@
e.preventDefault(); e.preventDefault();
}); });
window.project_file_path_upload = "#{upload_file_project_path @project}"; window.project_uploads_path = "#{project_uploads_path @project}";
:javascript :javascript
var merge_request var merge_request
......
...@@ -51,4 +51,4 @@ ...@@ -51,4 +51,4 @@
onSelect: function(dateText, inst) { $("#milestone_due_date").val(dateText) } onSelect: function(dateText, inst) { $("#milestone_due_date").val(dateText) }
}).datepicker("setDate", $.datepicker.parseDate('yy-mm-dd', $('#milestone_due_date').val())); }).datepicker("setDate", $.datepicker.parseDate('yy-mm-dd', $('#milestone_due_date').val()));
window.project_file_path_upload = "#{upload_file_project_path @project}"; window.project_uploads_path = "#{project_uploads_path @project}";
...@@ -29,4 +29,4 @@ ...@@ -29,4 +29,4 @@
= f.file_field :attachment, class: "js-note-attachment-input hidden" = f.file_field :attachment, class: "js-note-attachment-input hidden"
:javascript :javascript
window.project_file_path_upload = "#{upload_file_project_path @project}"; window.project_uploads_path = "#{project_uploads_path @project}";
...@@ -43,6 +43,6 @@ ...@@ -43,6 +43,6 @@
= link_to "Cancel", project_wiki_path(@project, :home), class: "btn btn-cancel" = link_to "Cancel", project_wiki_path(@project, :home), class: "btn btn-cancel"
:javascript :javascript
window.project_file_path_upload = "#{upload_file_project_path @project}"; window.project_uploads_path = "#{project_uploads_path @project}";
...@@ -220,7 +220,6 @@ Gitlab::Application.routes.draw do ...@@ -220,7 +220,6 @@ Gitlab::Application.routes.draw do
put :transfer put :transfer
post :archive post :archive
post :unarchive post :unarchive
post :upload_file
post :toggle_star post :toggle_star
post :markdown_preview post :markdown_preview
get :autocomplete_sources get :autocomplete_sources
...@@ -256,7 +255,11 @@ Gitlab::Application.routes.draw do ...@@ -256,7 +255,11 @@ Gitlab::Application.routes.draw do
end end
end end
get '/uploads/:folder_id/:filename' => 'uploads#show', constraints: { filename: /.+/ } resources :uploads, only: [:create] do
collection do
get ":secret/:filename", action: :show, constraints: { filename: /.+/ }
end
end
get '/compare/:from...:to' => 'compare#show', :as => 'compare', get '/compare/:from...:to' => 'compare#show', :as => 'compare',
:constraints => { from: /.+/, to: /.+/ } :constraints => { from: /.+/, to: /.+/ }
......
...@@ -26,7 +26,6 @@ ActiveRecord::Schema.define(version: 20150213121042) do ...@@ -26,7 +26,6 @@ ActiveRecord::Schema.define(version: 20150213121042) do
t.datetime "updated_at" t.datetime "updated_at"
t.string "home_page_url" t.string "home_page_url"
t.integer "default_branch_protection", default: 2 t.integer "default_branch_protection", default: 2
t.boolean "twitter_sharing_enabled", default: true
end end
create_table "broadcast_messages", force: true do |t| create_table "broadcast_messages", force: true do |t|
......
require('spec_helper')
describe Projects::UploadsController do
let(:project) { create(:project) }
let(:user) { create(:user) }
let(:jpg) { fixture_file_upload(Rails.root + 'spec/fixtures/rails_sample.jpg', 'image/jpg') }
let(:txt) { fixture_file_upload(Rails.root + 'spec/fixtures/doc_sample.txt', 'text/plain') }
describe 'POST #create' do
before do
sign_in(user)
project.team << [user, :developer]
end
context "without params['file']" do
it 'returns an error' do
post :create, project_id: project.to_param, format: :json
expect(response.status).to eq(422)
end
end
context 'with valid image' do
before do
post :create,
project_id: project.to_param,
file: jpg,
format: :json
end
it 'returns a content with original filename, new link, and correct type.' do
expect(response.body).to match '\"alt\":\"rails_sample\"'
expect(response.body).to match "\"url\":\"/#{project.path_with_namespace}/uploads"
expect(response.body).to match '\"is_image\":true'
end
end
context 'with valid non-image file' do
before do
post :create, project_id: project.to_param, file: txt, format: :json
end
it 'returns a content with original filename, new link, and correct type.' do
expect(response.body).to match '\"alt\":\"doc_sample.txt\"'
expect(response.body).to match "\"url\":\"/#{project.path_with_namespace}/uploads"
expect(response.body).to match '\"is_image\":false'
end
end
end
end
...@@ -4,49 +4,6 @@ describe ProjectsController do ...@@ -4,49 +4,6 @@ describe ProjectsController do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:public_project) { create(:project, :public) } let(:public_project) { create(:project, :public) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:jpg) { fixture_file_upload(Rails.root + 'spec/fixtures/rails_sample.jpg', 'image/jpg') }
let(:txt) { fixture_file_upload(Rails.root + 'spec/fixtures/doc_sample.txt', 'text/plain') }
describe 'POST #upload_file' do
before do
sign_in(user)
project.team << [user, :developer]
end
context "without params['markdown_file']" do
it 'returns an error' do
post :upload_file, id: project.to_param, format: :json
expect(response.status).to eq(422)
end
end
context 'with valid image' do
before do
post :upload_file,
id: project.to_param,
markdown_file: jpg,
format: :json
end
it 'returns a content with original filename, new link, and correct type.' do
expect(response.body).to match '\"alt\":\"rails_sample\"'
expect(response.body).to match "\"url\":\"/#{project.path_with_namespace}/uploads"
expect(response.body).to match '\"is_image\":true'
end
end
context 'with valid non-image file' do
before do
post :upload_file, id: project.to_param, markdown_file: txt, format: :json
end
it 'returns a content with original filename, new link, and correct type.' do
expect(response.body).to match '\"alt\":\"doc_sample.txt\"'
expect(response.body).to match "\"url\":\"/#{project.path_with_namespace}/uploads"
expect(response.body).to match '\"is_image\":false'
end
end
end
describe 'POST #toggle_star' do describe 'POST #toggle_star' do
it 'toggles star if user is signed in' do it 'toggles star if user is signed in' do
......
require 'spec_helper' require 'spec_helper'
describe Projects::FileService do describe Projects::UploadService do
describe 'File service' do describe 'File service' do
before do before do
@user = create :user @user = create :user
...@@ -10,9 +10,7 @@ describe Projects::FileService do ...@@ -10,9 +10,7 @@ describe Projects::FileService do
context 'for valid gif file' do context 'for valid gif file' do
before do before do
gif = fixture_file_upload(Rails.root + 'spec/fixtures/banana_sample.gif', 'image/gif') gif = fixture_file_upload(Rails.root + 'spec/fixtures/banana_sample.gif', 'image/gif')
@link_to_file = upload_file(@project.repository, @link_to_file = upload_file(@project.repository, gif)
{ 'markdown_file' => gif },
'http://test.example/')
end end
it { expect(@link_to_file).to have_key('alt') } it { expect(@link_to_file).to have_key('alt') }
...@@ -28,9 +26,7 @@ describe Projects::FileService do ...@@ -28,9 +26,7 @@ describe Projects::FileService do
before do before do
png = fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', png = fixture_file_upload(Rails.root + 'spec/fixtures/dk.png',
'image/png') 'image/png')
@link_to_file = upload_file(@project.repository, @link_to_file = upload_file(@project.repository, png)
{ 'markdown_file' => png },
'http://test.example/')
end end
it { expect(@link_to_file).to have_key('alt') } it { expect(@link_to_file).to have_key('alt') }
...@@ -45,7 +41,7 @@ describe Projects::FileService do ...@@ -45,7 +41,7 @@ describe Projects::FileService do
context 'for valid jpg file' do context 'for valid jpg file' do
before do before do
jpg = fixture_file_upload(Rails.root + 'spec/fixtures/rails_sample.jpg', 'image/jpg') jpg = fixture_file_upload(Rails.root + 'spec/fixtures/rails_sample.jpg', 'image/jpg')
@link_to_file = upload_file(@project.repository, { 'markdown_file' => jpg }, 'http://test.example/') @link_to_file = upload_file(@project.repository, jpg)
end end
it { expect(@link_to_file).to have_key('alt') } it { expect(@link_to_file).to have_key('alt') }
...@@ -60,9 +56,7 @@ describe Projects::FileService do ...@@ -60,9 +56,7 @@ describe Projects::FileService do
context 'for txt file' do context 'for txt file' do
before do before do
txt = fixture_file_upload(Rails.root + 'spec/fixtures/doc_sample.txt', 'text/plain') txt = fixture_file_upload(Rails.root + 'spec/fixtures/doc_sample.txt', 'text/plain')
@link_to_file = upload_file(@project.repository, @link_to_file = upload_file(@project.repository, txt)
{ 'markdown_file' => txt },
'http://test.example/')
end end
it { expect(@link_to_file).to have_key('alt') } it { expect(@link_to_file).to have_key('alt') }
...@@ -75,7 +69,7 @@ describe Projects::FileService do ...@@ -75,7 +69,7 @@ describe Projects::FileService do
end end
end end
def upload_file(repository, params, root_url) def upload_file(repository, file)
Projects::FileService.new(repository, params, root_url).execute Projects::UploadService.new(repository, file).execute
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment