Skip to content

作成プログラムをFTPで転送する機能を追加 #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/assets/javascripts/models/source_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/msg/en_us.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/msg/ja.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 'プログラムの文法をチェックしています。';
Expand Down
39 changes: 39 additions & 0 deletions app/assets/javascripts/views/main_menu_view.js.coffee.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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()

Expand Down
27 changes: 27 additions & 0 deletions app/controllers/source_codes_controller.rb
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -62,6 +63,32 @@ 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
}

srv = ENV["FTP_Srv"]
user = ENV["FTP_User"]
pass = ENV["FTP_Pass"]

begin
Net::FTP.open(srv, user, pass) 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)
Expand Down
5 changes: 5 additions & 0 deletions app/views/editor/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ja:
not_ruby_program: "はRubyのプログラムではありません"
not_exist: "はありません"
exist: "すでに同じ名前のプログラムがあります"
upload_failed: "をアップロードできませんでした"

toolbox_names:
default: "通常"
Expand Down Expand Up @@ -40,6 +41,7 @@ ja:
msg_input_program_name: "プログラムの名前を入れてね(例:01.rb)"
menu: "メニュー"
run: "実行"
upload: "アップロード"
download: "ダウンロード"
load: "ロード"
save: "セーブ"
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down