From 2291f1012bb45af9bdd47384833e8abdc37bef6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E5=8F=A3=E7=83=88?= Date: Sun, 5 Mar 2017 12:06:20 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=9C=E6=88=90=E3=83=97=E3=83=AD?= =?UTF-8?q?=E3=82=B0=E3=83=A9=E3=83=A0=E3=82=92FTP=E3=81=A7=E8=BB=A2?= =?UTF-8?q?=E9=80=81=E3=81=99=E3=82=8B=E6=A9=9F=E8=83=BD=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/assets/javascripts/models/source_code.js | 4 ++ app/assets/javascripts/msg/en_us.js | 3 ++ app/assets/javascripts/msg/ja.js | 3 ++ .../views/main_menu_view.js.coffee.erb | 39 +++++++++++++++++++ app/controllers/source_codes_controller.rb | 23 +++++++++++ app/views/editor/index.html.haml | 5 +++ config/locales/en.yml | 2 + config/locales/ja.yml | 2 + config/routes.rb | 1 + 9 files changed, 82 insertions(+) diff --git a/app/assets/javascripts/models/source_code.js b/app/assets/javascripts/models/source_code.js index b7b6587..09fde41 100644 --- a/app/assets/javascripts/models/source_code.js +++ b/app/assets/javascripts/models/source_code.js @@ -62,6 +62,10 @@ Smalruby.SourceCode = Backbone.Model.extend({ return this.post_(''); }, + upload: function() { + return this.post_('upload'); + }, + write: function(force) { if (force == null) { force = false; diff --git a/app/assets/javascripts/msg/en_us.js b/app/assets/javascripts/msg/en_us.js index 12c660c..73f4bcb 100644 --- a/app/assets/javascripts/msg/en_us.js +++ b/app/assets/javascripts/msg/en_us.js @@ -61,6 +61,9 @@ Blockly.Msg.VIEWS_MAIN_MENU_VIEW_SAVE_BLOCKUI_NOTICE = 'Your program name is "{$ Blockly.Msg.VIEWS_MAIN_MENU_VIEW_SAVE_ERROR_MESSAGE = "Can't save your program."; Blockly.Msg.VIEWS_MAIN_MENU_VIEW_SAVE_CANCELED = 'Canceled to save.'; Blockly.Msg.VIEWS_MAIN_MENU_VIEW_SAVE_SUCCEEDED = 'Saved.'; +Blockly.Msg.VIEWS_MAIN_MENU_VIEW_UPLOAD_ERROR_MESSAGE = "Can't upload your program."; +Blockly.Msg.VIEWS_MAIN_MENU_VIEW_UPLOAD_CANCELED = 'Canceled to upload.'; +Blockly.Msg.VIEWS_MAIN_MENU_VIEW_UPLOAD_SUCCEEDED = 'Uploaded.'; Blockly.Msg.VIEWS_MAIN_MENU_VIEW_CHECK_BLOCKUI_TITLE = 'Checking your program'; Blockly.Msg.VIEWS_MAIN_MENU_VIEW_CHECK_BLOCKUI_MESSAGE = 'Now, checking syntax of your program.'; diff --git a/app/assets/javascripts/msg/ja.js b/app/assets/javascripts/msg/ja.js index 409ee49..de148c5 100644 --- a/app/assets/javascripts/msg/ja.js +++ b/app/assets/javascripts/msg/ja.js @@ -64,6 +64,9 @@ Blockly.Msg.VIEWS_MAIN_MENU_VIEW_SAVE_BLOCKUI_NOTICE = 'プログラムの名前 Blockly.Msg.VIEWS_MAIN_MENU_VIEW_SAVE_ERROR_MESSAGE = 'セーブできませんでした'; Blockly.Msg.VIEWS_MAIN_MENU_VIEW_SAVE_CANCELED = 'セーブをキャンセルしました'; Blockly.Msg.VIEWS_MAIN_MENU_VIEW_SAVE_SUCCEEDED = 'セーブしました'; +Blockly.Msg.VIEWS_MAIN_MENU_VIEW_UPLOAD_ERROR_MESSAGE = 'アップロードできませんでした'; +Blockly.Msg.VIEWS_MAIN_MENU_VIEW_UPLOAD_CANCELED = 'アップロードをキャンセルしました'; +Blockly.Msg.VIEWS_MAIN_MENU_VIEW_UPLOAD_SUCCEEDED = 'アップロードしました'; Blockly.Msg.VIEWS_MAIN_MENU_VIEW_CHECK_BLOCKUI_TITLE = 'プログラムのチェック中'; Blockly.Msg.VIEWS_MAIN_MENU_VIEW_CHECK_BLOCKUI_MESSAGE = 'プログラムの文法をチェックしています。'; diff --git a/app/assets/javascripts/views/main_menu_view.js.coffee.erb b/app/assets/javascripts/views/main_menu_view.js.coffee.erb index 49e7ce0..102908e 100644 --- a/app/assets/javascripts/views/main_menu_view.js.coffee.erb +++ b/app/assets/javascripts/views/main_menu_view.js.coffee.erb @@ -14,6 +14,7 @@ Smalruby.MainMenuView = Backbone.View.extend 'click #download-button': 'onDownload' 'click #load-local-button': 'onLoadLocal' 'click #load-button': 'onLoad' + 'click #upload-button': 'onUpload' 'click #save-button': 'onSave' 'click #check-button': 'onCheck' 'click #reset-button': 'onReset' @@ -187,6 +188,44 @@ Smalruby.MainMenuView = Backbone.View.extend return unless confirm(<%= bm('.load_confirm') %>) $('input#load-file[name="source_code[file]"]').click() + onUpload: (e) -> + e.preventDefault() + + filename = $.trim($('#filename').val()) + if filename.length <= 0 + window.errorMessage(<%= bm('.save_error_no_name') %>) + $('#filename').focus() + return + + sourceCode = @getSourceCode() + + if window.blockMode + sourceCode = + new Smalruby.SourceCode({ filename: sourceCode.getRbxmlFilename() }) + + clearMessages() + + @blockUI + title: <%= bm('.save_blockui_title') %> + message: <%= bm('.save_blockui_message') %> + notice: goog.getMsg(<%= bm('.save_blockui_notice') %>, + { filename: sourceCode.get('filename') }) + + sourceCode.save2() + .then (data) -> + sourceCode.write() + .then (data) => + @confirmOverwrite_.call @, data, sourceCode, -> + errorMsg = <%= bm('.upload_canceled') %> + .then(@unblockUI, @unblockUI) + .done -> + sourceCode.upload() + Smalruby.savedFilename = sourceCode.get('filename') + window.changed = false + window.successMessage(<%= bm('.upload_succeeded') %>) + .fail -> + window.errorMessage(<%= bm('.upload_error_message') %>) + onSave: (e) -> e.preventDefault() diff --git a/app/controllers/source_codes_controller.rb b/app/controllers/source_codes_controller.rb index d76b064..08765d3 100644 --- a/app/controllers/source_codes_controller.rb +++ b/app/controllers/source_codes_controller.rb @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- require 'nkf' +require 'net/ftp' class SourceCodesController < ApplicationController before_filter :check_whether_standalone, only: [:write, :run, :load_local] @@ -62,6 +63,28 @@ def write render json: res end + def upload + filename = source_code_params[:filename] + program_path = local_program_paths.find { |path| + path.basename.to_s == filename + } + + info = { + filename: filename + } + + begin + Net::FTP.open(ENV["FTP_SERVER"], ENV["FTP_CLIENT"], ENV["FTP_PASSWORD"]) do |client| + client.put(program_path) + end + rescue + info[:error] = I18n.t('.upload_failed', + scope: 'controllers.source_codes') + end + + render json: { source_code: info }, content_type: request.format + end + def load f = params[:source_code][:file] info = get_file_info(f) diff --git a/app/views/editor/index.html.haml b/app/views/editor/index.html.haml index 9b9daf1..00c844d 100644 --- a/app/views/editor/index.html.haml +++ b/app/views/editor/index.html.haml @@ -62,6 +62,11 @@ %h4 %i.icon-folder-open = t('.load') + %li + %a#upload-button + %h4 + %i.icon-upload + = t('.upload') - if standalone? %li %a#save-button diff --git a/config/locales/en.yml b/config/locales/en.yml index 6539a80..cc36846 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -13,6 +13,7 @@ en: not_ruby_program: " isn't ruby program." not_exist: " does not exist." exist: " already exist." + upload_failed: " is failed to upload." toolbox_names: default: "Normal" @@ -39,6 +40,7 @@ en: msg_input_program_name: "Input your program name(ex:01.rb)" menu: "Menu" run: "Run" + upload: "Upload" download: "Download" load: "Load" save: "Save" diff --git a/config/locales/ja.yml b/config/locales/ja.yml index 513506e..79794bf 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -13,6 +13,7 @@ ja: not_ruby_program: "はRubyのプログラムではありません" not_exist: "はありません" exist: "すでに同じ名前のプログラムがあります" + upload_failed: "をアップロードできませんでした" toolbox_names: default: "通常" @@ -40,6 +41,7 @@ ja: msg_input_program_name: "プログラムの名前を入れてね(例:01.rb)" menu: "メニュー" run: "実行" + upload: "アップロード" download: "ダウンロード" load: "ロード" save: "セーブ" diff --git a/config/routes.rb b/config/routes.rb index cc7c860..bec6ced 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -23,6 +23,7 @@ delete 'source_codes/write' post 'source_codes/run' post 'source_codes/to_blocks' + post 'source_codes/upload' get 'preferences', to: 'users#preferences' get 'toolbox', to: 'editor#toolbox' From 19812b0c540b5c2c480fbf0b43b123b7c39e2742 Mon Sep 17 00:00:00 2001 From: a_noguchi Date: Fri, 14 Apr 2017 13:27:29 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Rubocop=E8=AD=A6=E5=91=8A=E5=AF=BE=E7=AD=96?= =?UTF-8?q?=EF=BC=9AFTP=E6=8E=A5=E7=B6=9A=E3=81=AE=E3=83=97=E3=83=AD?= =?UTF-8?q?=E3=82=B0=E3=83=A9=E3=83=A0=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/source_codes_controller.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/source_codes_controller.rb b/app/controllers/source_codes_controller.rb index 08765d3..ab48cea 100644 --- a/app/controllers/source_codes_controller.rb +++ b/app/controllers/source_codes_controller.rb @@ -73,8 +73,12 @@ def upload filename: filename } + srv = ENV["FTP_Srv"] + user = ENV["FTP_User"] + pass = ENV["FTP_Pass"] + begin - Net::FTP.open(ENV["FTP_SERVER"], ENV["FTP_CLIENT"], ENV["FTP_PASSWORD"]) do |client| + Net::FTP.open(srv, user, pass) do |client| client.put(program_path) end rescue