Adds action to upload files using valum's file upload

1.0
Fernando Espinosa 14 years ago
parent fe556f27d9
commit afa6197566

1
.gitignore vendored

@ -5,6 +5,7 @@ log/*.log
tmp/*
tmp/**/*
public/system
public/uploads
db/schema.rb
doc/
config/app_config.yml

@ -2,6 +2,26 @@
class UploadController < ApplicationController
skip_before_filter :verify_authenticity_token
before_filter :login_required
def create
render :nothing => true and return if params[:qqfile].blank? || request.body.blank?
file_name = params[:qqfile]
user_id = current_user.id
upload_path = Rails.root.join('public', 'uploads', user_id.to_s)
file_path = upload_path.join(file_name)
FileUtils.mkdir_p(upload_path) unless File.directory?(upload_path)
File.open(file_path, 'w+') do |file|
file.write(request.body.read.force_encoding('utf-8'))
end
render :json => {:file_uri => "/uploads/#{user_id}/" + file_name}
end
def progress
return if params["X-Progress-ID".to_sym].nil?
if $progress[params["X-Progress-ID".to_sym]].nil?

@ -1,5 +1,5 @@
<%= content_for(:js) do %>
<%= headjs_include_tag "plugins/jquery.min", "plugins/general_functions", "plugins/tumblr", "admin/tables/dashboard", "plugins/jquery.color", "admin/tables/import" %>
<%= headjs_include_tag "plugins/jquery.min", "plugins/general_functions", "plugins/tumblr", "plugins/file_uploader", "admin/tables/dashboard", "plugins/jquery.color", "admin/tables/import" %>
<% end %>
<%= content_for(:head) do %>
<%= stylesheet_link_tag('reset.css','admin/layout','admin/tables/dashboard') %>
@ -86,7 +86,7 @@
<%= content_for(:mamufas) do %>
<div class="mamufas">
<div class="create_window">
<a href="#close_window" class="close_create"></a>
<div class="inner_">
@ -104,9 +104,8 @@
<a href="#">I want to start with some imported data...</a>
<span class="file">
<div class="select_file">
<a href="#select_file">Select files...</a>
<div id="uploader"></div>
<p>You can import .csv and .xls files</p>
<input type="file" value="select your file" name="file"/>
</div>
<div class="progress">
<p>Uploading your file...</p>
@ -140,7 +139,7 @@
</ul>
</span>
</li>
</ul>
</ul>
</span>
<span class="bottom">
<a href="#" class="cancel">cancel</a>
@ -149,7 +148,7 @@
</form>
</div>
</div>
<div class="delete_window">
<a href="#close_window" class="close_delete"></a>
<div class="inner_">

@ -15,6 +15,7 @@ CartoDB::Application.routes.draw do
root :to => "home#index"
get '/progress' => 'upload#progress', :format => :json
post '/upload' => 'upload#create', :format => :json
get '/login' => 'sessions#new', :as => :login
get '/logout' => 'sessions#destroy', :as => :logout
@ -51,7 +52,7 @@ CartoDB::Application.routes.draw do
namespace CartoDB::API::VERSION_1, :format => :json, :module => "api/json" do
get '/' => 'queries#run'
get '/column_types' => 'meta#column_types'
get '/column_types' => 'meta#column_types'
get '/tables' => 'tables#index'
post '/tables' => 'tables#create'
get '/tables/:id' => 'tables#show'
@ -72,6 +73,6 @@ CartoDB::Application.routes.draw do
put '/tables/:table_id/records/:record_id/columns/:id' => 'records#update_column'
end
end
# Subdomain "developers." is served by rack application ApiDocumentationServer
end

@ -1,10 +1,10 @@
var create_type = 0;
var interval = null;
head(function(){
//Create new table
$('a.new_table').click(function(ev){
ev.preventDefault();
@ -13,8 +13,8 @@
$('div.mamufas').fadeIn();
bindESC();
});
$('div.create_window ul li a').click(function(ev){
ev.stopPropagation();
ev.preventDefault();
@ -24,8 +24,8 @@
(create_type==0)?create_type++:create_type--;
}
});
$('span.file input').hover(function(ev){
$('span.file a').addClass('hover');
$(document).css('cursor','pointer');
@ -33,56 +33,30 @@
$('span.file a').removeClass('hover');
$(document).css('cursor','default');
});
$('input[type="file"]').change(function(ev){
$('div.select_file p').text($(this).attr('value').split('\\')[2]);
if ($('form input[type="file"]').attr('value')!='' && !$('form input[type="submit"]').hasClass('disabled')) {
$('div.create_window ul li:eq(0)').addClass('disabled');
$('form input[type="submit"]').addClass('disabled');
$('span.file').addClass('uploading');
var uuid= '';
for (i = 0; i < 32; i++) {
uuid += Math.floor(Math.random() * 16).toString(16);
}
$.ajax({
type: "POST",
url: '/api/json/tables/?X-Progress-ID='+uuid,
success: function(data, textStatus, XMLHttpRequest) {
var uploader = new qq.FileUploader({
element: document.getElementById('uploader'),
action: '/upload',
params: {},
allowedExtensions: ['csv', 'xls', 'zip'],
sizeLimit: 0, // max size
minSizeLimit: 0, // min size
debug: false,
function fetch(uuid) {
$.ajax({
type: "GET",
headers: {'cartodbclient':true},
url: '/progress?X-Progress-ID='+uuid,
success: function(result, textStatus, XMLHttpRequest) {
var percentage = result.received / result.size;
$('span.progress').width((346*percentage)/1);
if (result.state == 'done') {
window.clearTimeout(interval);
georeferenceImport();
}
},
error: function(e) {
console.debug(e);
}
});
}
interval = window.setInterval(function () {fetch(uuid);},200);
},
error: function(e) {
console.debug(e);
}
});
}
onSubmit: function(id, fileName){},
onProgress: function(id, fileName, loaded, total){
console.debug(id, fileName, loaded, total);
},
onComplete: function(id, fileName, responseJSON){
console.debug(responseJSON);
},
onCancel: function(id, fileName){},
messages: {
// error messages, see qq.FileUploaderBasic for content
},
showMessage: function(message){ alert(message); }
});
$('form#import_file').submit(function(ev){
ev.stopPropagation();
ev.preventDefault();
@ -91,8 +65,8 @@
}
});
});
function resetUploadFile() {
window.clearTimeout(interval);
create_type = 0;
@ -115,12 +89,12 @@
$('div.create_window ul li:eq(1)').addClass('finished');
$('form input[type="submit"]').removeClass('disabled');
$('span.file div.progress p').html('<strong>69 rows</strong> correctly imported!');
}
function createNewToFinish () {
$('div.create_window div.inner_').animate({borderColor:'#FFC209', height:'68px'},500);
$('div.create_window div.inner_ form').animate({opacity:0},300,function(){

File diff suppressed because it is too large Load Diff

@ -78,9 +78,9 @@ div.mamufas div.create_window div.inner_ span.top ul li.selected span.file div.s
div.mamufas div.create_window div.inner_ span.top ul li.selected span.file div.progress {display:none;}
div.mamufas div.create_window div.inner_ span.top ul li.selected span.file.uploading div.select_file {display:none;}
div.mamufas div.create_window div.inner_ span.top ul li.selected span.file.uploading div.progress {display:block;}
div.mamufas div.create_window div.inner_ span.top ul li.selected span.file div.select_file a {float:left; height:12px; margin:3px 0 0 2px; padding:4px 7px; border:1px solid #999; font:bold 11px "Helvetica",Arial; color:#333333; text-align:center;
div.mamufas div.create_window div.inner_ span.top ul li.selected span.file div.select_file div#uploader .qq-upload-button{float:left; height:12px; margin:3px 0 0 2px; padding:4px 7px; border:1px solid #999; font:bold 11px "Helvetica",Arial; color:#333333; text-align:center;
text-decoration:none; -webkit-border-radius:3px; -moz-border-radius:3px; border-radius:3px; text-shadow:0 1px white; background-image:-moz-linear-gradient(100% 100% 90deg, #CACBCE, #FFFFFF); background-image:-webkit-gradient(linear, 0% 0%, 0% 100%, from(#FFFFFF), to(#CACBCE));}
div.mamufas div.create_window div.inner_ span.top ul li.selected span.file div.select_file a.hover {background-image:-moz-linear-gradient(100% 100% 90deg, #FFFFFF, #CACBCE); background-image:-webkit-gradient(linear, 0% 0%, 0% 100%, from(#CACBCE), to(#FFFFFF)); cursor:pointer;}
div.mamufas div.create_window div.inner_ span.top ul li.selected span.file div.select_file div#uploader .qq-upload-button.hover {background-image:-moz-linear-gradient(100% 100% 90deg, #FFFFFF, #CACBCE); background-image:-webkit-gradient(linear, 0% 0%, 0% 100%, from(#CACBCE), to(#FFFFFF)); cursor:pointer;}
div.mamufas div.create_window div.inner_ span.top ul li.selected span.file div.select_file p {float:left; margin:1px 0 0 10px; font:normal 13px "Helvetica",Arial; color:#B3B3B3;}
div.mamufas div.create_window div.inner_ span.top ul li.selected span.file div.select_file input {position:absolute; top:0; left:0; width:350px; height:30px; opacity:0;}
div.mamufas div.create_window div.inner_ span.top ul li.selected span.file div.select_file input:hover {cursor:pointer;}

Loading…
Cancel
Save