app/controllers/editor_controller.rb in smalruby-editor-0.0.1 vs app/controllers/editor_controller.rb in smalruby-editor-0.0.2

- old
+ new

@@ -1,5 +1,6 @@ +# -*- coding: utf-8 -*- require 'nkf' class EditorController < ApplicationController def index end @@ -9,14 +10,20 @@ disposition: 'attachment', type: 'text/plain; charset=utf-8') end def load_file f = params['load_file'] + mime_type = MIME.check(f.path) + content_type = mime_type.try(:content_type) || f.content_type res = { name: f.original_filename, - type: f.content_type, + type: content_type, size: f.size, - source: NKF.nkf('-w', f.read), } + if /\Atext\/plain/ =~ content_type + res[:source] = NKF.nkf('-w', f.read) + else + res[:error] = 'Rubyのプログラムではありません' + end render json: res, content_type: request.format end end