diff --git a/app/controllers/equation_images_controller.rb b/app/controllers/equation_images_controller.rb index 4f5fba8153a..ed77aaa6b4d 100644 --- a/app/controllers/equation_images_controller.rb +++ b/app/controllers/equation_images_controller.rb @@ -1,12 +1,16 @@ class EquationImagesController < ApplicationController - # Facade to codecogs API so we retain control + # Facade to codecogs API for gif generation or microservice MathMan for svg def show - # TODO: escape id here, and stop double escaping it in - # public/javascripts/tinymce/jscripts/tiny_mce/plugins/instructure_equation/editor_plugin.js - # this will require a corresponding data migration to fix - base_url = Setting.get('codecogs.equation_image_link', 'http://latex.codecogs.com/gif.latex') - redirect_to base_url + '?' + params[:id] + base_url = Setting.get('equation_image_url', 'http://latex.codecogs.com/gif.latex?') + + # At the moment, the latex string is stored in the db double escaped. By + # the time the value gets here as `params[:id]` it has been unescaped once. + # This is nearly how we want it to pass it on to the next service, except + # `+` signs are in tact. Since normally the `+` signifies a space and we + # want the `+` signs for real, we need to encode them. + latex = params[:id].gsub('+', '%2B') + redirect_to base_url + latex end end diff --git a/db/migrate/20160601192206_update_setting_equation_svg_url_default.rb b/db/migrate/20160601192206_update_setting_equation_svg_url_default.rb new file mode 100644 index 00000000000..6ef4586ddfd --- /dev/null +++ b/db/migrate/20160601192206_update_setting_equation_svg_url_default.rb @@ -0,0 +1,21 @@ +class UpdateSettingEquationSvgUrlDefault < ActiveRecord::Migration + tag :predeploy + + def up + return unless Shard.current.default? + + setting = Setting.where(name: 'codecogs.equation_image_link').take + if setting.present? + Setting.set('equation_image_url', "#{setting.value}?") + end + end + + def down + return unless Shard.current.default? + + setting = Setting.where(name: 'equation_image_url').take + if setting.present? + Setting.set('codecogs.equation_image_link', setting.value.sub(/\?$/, '')) + end + end +end diff --git a/db/migrate/20160601195833_remove_codecogs_equation_image_setting_key.rb b/db/migrate/20160601195833_remove_codecogs_equation_image_setting_key.rb new file mode 100644 index 00000000000..3d29b63e66b --- /dev/null +++ b/db/migrate/20160601195833_remove_codecogs_equation_image_setting_key.rb @@ -0,0 +1,15 @@ +class RemoveCodecogsEquationImageSettingKey < ActiveRecord::Migration + tag :postdeploy + + def up + return unless Shard.current.default? + + Setting.remove('codecogs.equation_image_link') + end + + def down + return unless Shard.current.default? + + Setting.remove('equation_image_url') + end +end diff --git a/spec/controllers/equation_images_controller_spec.rb b/spec/controllers/equation_images_controller_spec.rb index 88ef3a84cfd..060e06526ca 100644 --- a/spec/controllers/equation_images_controller_spec.rb +++ b/spec/controllers/equation_images_controller_spec.rb @@ -7,4 +7,9 @@ describe EquationImagesController do expect(response).to redirect_to('http://latex.codecogs.com/gif.latex?foo') end + it 'encodes `+` signs properly' do + latex = '5%5E5%5C%3A+%5C%3A%5Csqrt%7B9%7D' + get :show, id: latex + expect(response).to redirect_to(/\%2B/) + end end