From f4ce6c9994003dac4e3308fd01d3831c346619f1 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Mon, 3 Jun 2013 23:13:22 -0700 Subject: [PATCH] file saving --- app.rb | 19 ++++++++++++++++ views/site_files/text_editor.slim | 38 ++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/app.rb b/app.rb index 78e85b99..cd828040 100644 --- a/app.rb +++ b/app.rb @@ -150,9 +150,28 @@ end get '/site_files/text_editor/:filename' do |filename| @file_url = "http://#{current_site.username}.neocities.org/#{filename}" + slim :'site_files/text_editor' end +post '/site_files/save/:filename' do |filename| + tmpfile = Tempfile.new 'neocities_saving_file' + + if (tmpfile.size + current_site.total_space) > Site::MAX_SPACE + halt 'File is too large, it has NOT been saved. Please make a local copy and then try to reduce the size.' + end + + tmpfile.write request.body.read + tmpfile.close + + sanitized_filename = filename.gsub(/[^a-zA-Z_\-.]/, '') + dest_path = File.join site_base_path(current_site.username), sanitized_filename + + FileUtils.mv tmpfile.path, dest_path + File.chmod(0640, dest_path) if self.class.production? + 'ok' +end + get '/terms' do slim :'terms' end diff --git a/views/site_files/text_editor.slim b/views/site_files/text_editor.slim index f1a614b3..f7657e2b 100644 --- a/views/site_files/text_editor.slim +++ b/views/site_files/text_editor.slim @@ -7,6 +7,11 @@ css: left: 0; } +.row + .span6.offset3 + div id="editorUpdates" class="alert alert-success hidden" + button type="button" class="close" onclick="$('#editorUpdates').addClass('hidden');" × + span .row .span6 font style="font-size: 27pt" Editing #{params[:filename]} @@ -54,11 +59,11 @@ css: .row.text-center style="margin-top: 10px" .span4 - a.btn.btn-large.btn-warning href="" Cancel + a.btn.btn-large.btn-warning href="" Finish Without Saving .span4 - a.btn.btn-large.btn-success href="" Save Changes + a.btn.btn-large.btn-success href="#" onclick="saveTextFile()" Save Changes .span4 - a.btn.btn-large.btn-info href="" Save and Finish Editing + a.btn.btn-large.btn-info href="" Save and Finish script src="http://rawgithub.com/ajaxorg/ace-builds/master/src-noconflict/ace.js" type="text/javascript" charset="utf-8" @@ -67,6 +72,33 @@ javascript: function setTheme(name) { editor.setTheme($('#theme').val()); } + + function saveTextFile() { + /* + $.post('/site_files/save/#{params[:filename]}', editor.getValue(), function(resp) { + if(resp == 'ok') { + $('#editorUpdates span').text('Your file has been saved.'); + $('#editorUpdates').removeClass('hidden'); + } + }); + */ + + $.ajax({ + url: '/site_files/save/#{params[:filename]}', + data: editor.getValue(), + processData: false, + contentType: false, + type: 'POST', + success: function(response){ + if(response == 'ok') { + $('#editorUpdates span').text('Your file has been saved.'); + } else { + $('#editorUpdates span').text(response); + } + $('#editorUpdates').removeClass('hidden'); + } + }); + } var editor = {};