view · edit · sidebar · attach · print · history

Index>

20170329-davaz-upload-image

Summary

  • Davaz.com: Cannot upload an image for links
  • rapflag: Add currency support in config file
  • Fix dosing links for Niereninssuffizienz in oddb.org
  • Keep in Mind

Commits

Index

Cannot upload an image for links

Now returning to the problem why we cannot add a new image. The images are now correctly stored into /var/www/davaz.com/doc/resources/uploads/tmp/images/, but as there is no valid art_object present when the method init of AdminAjaxUploadImage is called, nothing is stored.

Tried to upload an image for an existing movie art object and got the following stack-trace

NoMethodError: undefined method `read' for #<Hash:0x007fbe7049cf10>
        /var/www/davaz.com/src/util/image_helper.rb:92:in `store_upload_image'
        /var/www/davaz.com/src/state/works/movies.rb:54:in `init'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/sbsm-1.3.6/lib/sbsm/session.rb:223:in `block in process_rack'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/sbsm-1.3.6/lib/sbsm/session.rb:208:in `synchronize'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/sbsm-1.3.6/lib/sbsm/session.rb:208:in `process_rack'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/sbsm-1.3.6/lib/sbsm/app.rb:119:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/lint.rb:49:in `_call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/lint.rb:37:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/show_exceptions.rb:23:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/content_length.rb:15:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/static.rb:149:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/common_logger.rb:33:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/tempfile_reaper.rb:15:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/lint.rb:49:in `_call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/lint.rb:37:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/show_exceptions.rb:23:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/common_logger.rb:33:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/chunked.rb:54:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/content_length.rb:15:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/handler/webrick.rb:86:in `service'
        /usr/local/ruby-2.4.0/lib/ruby/2.4.0/webrick/httpserver.rb:140:in `service'
        /usr/local/ruby-2.4.0/lib/ruby/2.4.0/webrick/httpserver.rb:96:in `run'
        /usr/local/ruby-2.4.0/lib/ruby/2.4.0/webrick/server.rb:290:in `block in start_thread'

When uploading a image for a new movie I see at my pry backtrace the following

rom: /var/www/davaz.com/src/state/_partial/image.rb @ line 46 DaVaz::State::AdminAjaxUploadImage#init:

    41:     VOLATILE = true
    42: 
    43:     def init
    44:       string_io = @session.user_input(:image_file)
    45:       require 'pry'; binding.pry
 => 46:       obj = @model.artobject ? @model.artobject : @model
    47:       if obj.is_a?(OpenStruct) && !obj.artobject_id && defined?(obj.links) && obj.links.size > 0
    48:         obj = @session.app.db_manager.load_artobject(obj.links.first.artobject_id)
    49:       end
    50:       if string_io
    51:         artobject_id = obj.artobject_id

[1] pry(#<DaVaz::State::AdminAjaxUploadImage>)> @model.class
=> OpenStruct
[2] pry(#<DaVaz::State::AdminAjaxUploadImage>)> @model.artobject
=> #<DaVaz::Model::ArtObject:0x007fbe703ef338 @links=[], @tags=[]>

I am here in the state AdminAjaxUploadImage.

Adding an image with a new artobject saves the image. Eg. in my case as /var/www/davaz.com/doc/resources/uploads/images/1/1811.png, where 1811 is the new artobject_id (aka insert_id). But the new artobject is invalid and cannot be display. I see the following backtrace when visiting (logged in) http://davaz.ngiger.ch/en/works/movies/ again.

/var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/htmlgrid-1.1.3/lib/htmlgrid/composite.rb:157: warning: Hash#index is deprecated; use Hash#key
NoMethodError: undefined method `artobject_id' for nil:NilClass
        /var/www/davaz.com/src/view/_partial/art_object.rb:578:in `init'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/htmlgrid-1.1.3/lib/htmlgrid/component.rb:139:in `initialize'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/htmlgrid-1.1.3/lib/htmlgrid/composite.rb:142:in `new'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/htmlgrid-1.1.3/lib/htmlgrid/composite.rb:142:in `create'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/htmlgrid-1.1.3/lib/htmlgrid/divcomposite.rb:33:in `block in compose'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/htmlgrid-1.1.3/lib/htmlgrid/divcomposite.rb:15:in `each'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/htmlgrid-1.1.3/lib/htmlgrid/divcomposite.rb:15:in `compose'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/htmlgrid-1.1.3/lib/htmlgrid/composite.rb:72:in `init'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/htmlgrid-1.1.3/lib/htmlgrid/component.rb:139:in `initialize'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/sbsm-1.3.6/lib/sbsm/state.rb:240:in `new'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/sbsm-1.3.6/lib/sbsm/state.rb:240:in `view'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/sbsm-1.3.6/lib/sbsm/state.rb:173:in `to_html'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/sbsm-1.3.6/lib/sbsm/session.rb:478:in `to_html'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/sbsm-1.3.6/lib/sbsm/session.rb:238:in `block in process_rack'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/sbsm-1.3.6/lib/sbsm/session.rb:208:in `synchronize'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/sbsm-1.3.6/lib/sbsm/session.rb:208:in `process_rack'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/sbsm-1.3.6/lib/sbsm/app.rb:119:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/lint.rb:49:in `_call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/lint.rb:37:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/show_exceptions.rb:23:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/content_length.rb:15:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/static.rb:149:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/common_logger.rb:33:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/tempfile_reaper.rb:15:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/lint.rb:49:in `_call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/lint.rb:37:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/show_exceptions.rb:23:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/common_logger.rb:33:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/chunked.rb:54:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/content_length.rb:15:in `call'
        /var/www/davaz.com/vendor/ruby240/ruby/2.4.0/gems/rack-2.0.1/lib/rack/handler/webrick.rb:86:in `service'
        /usr/local/ruby-2.4.0/lib/ruby/2.4.0/webrick/httpserver.rb:140:in `service'
        /usr/local/ruby-2.4.0/lib/ruby/2.4.0/webrick/httpserver.rb:96:in `run'
        /usr/local/ruby-2.4.0/lib/ruby/2.4.0/webrick/server.rb:290:in `block in start_thread'
        DaVaz::View::AdminMoviesArtObjectComposite::COMPONENTS[[0, 1]] in create(DaVaz::View::AdminArtObjectInnerComposite)
172.25.1.75 - - [29/Mar/2017:08:21:09 +0200] "GET /en/gallery/ajax_movie_gallery/artobject_id/1811 HTTP/1.1" 500 105389 0.0264

Fixed the problem for uploading a new image for an existing movie, by changing the calculation of the image in the method store_upload_image of src/util/image_helper.rb to

        if string_io[:tempfile] && File.exist?(string_io[:tempfile])
          image = Magick::Image.from_blob(IO.read( string_io[:tempfile])).first
        elsif defined?(string_io.read)
          image = Magick::Image.from_blob(string_io.read).first
        else
          raise "Unable to store_upload_image #{string_io}"
        end

Probably only the the first if statement is always true, but you never know.

When storing the tmp image we have the following situation

From: /var/www/davaz.com/src/util/image_helper.rb @ line 111 DaVaz::Util::ImageHelper.store_tmp_image:

    109: def store_tmp_image(tmp_path, artobject_id)
    110:   require 'pry'; binding.pry
 => 111:   image = Magick::Image.read(tmp_path).first
    112:   extension = image.format.downcase
    113:   dir = File.join(image_dir, artobject_id.to_s[-1,1])
    114:   FileUtils.mkdir_p(dir)
    115:   path = File.join(dir, artobject_id.to_s + '.' + extension)
    116:   image.write(path)
    117:   resize_image(artobject_id.to_s, image)
    118:   File.delete(tmp_path)
    119: end

[1] pry(DaVaz::Util::ImageHelper)> image = Magick::Image.read(tmp_path).first
=> /var/www/davaz.com/doc/resources/uploads/tmp/images/1490770431.png PNG 737x661 3840x1080+598+335 DirectClass 8-bit 29kb
[2] pry(DaVaz::Util::ImageHelper)> continue
172.25.1.75 - - [29/Mar/2017:08:54:16 +0200] "POST /en/gallery HTTP/1.1" 303 - 16.6865
172.25.1.75 - - [29/Mar/2017:08:54:16 +0200] "GET /en/gallery/art_object/artobject_id/1813/artgroup_id/DES HTTP/1.1" 200 34293 0.0176

Compared the situation when adding an image to a news and found that we did not find the correct artobject_id. Debugging and breaking if it is nil.

After replacing the method init of the class AdminAjaxUploadImage in src/state/_partial/image.rb

    def init
      string_io = @session.user_input(:image_file)
      artobject_id = @session.user_input(:artobject_id)
      if string_io && artobject_id
        DaVaz::Util::ImageHelper.store_upload_image(
          string_io, artobject_id)
        @model.artobject = @session.app.load_artobject(artobject_id)
      # no 'else' => src/state/_partial/art_object handles new artobjects
      end
    end

I can upload a new image in links and news. The procedure DaVaz::State::AdminAjaxUploadImage#init is even getting called twice and the new image is not shown. But after a reload of the page it displays correctly.

Pushed commit Fix adding an image to links, news and movies. After uploading an image one has to reload the page to see the new image. Did not find an easy way to fix this problem.

Creating a unit test was not so easy, as handling the model dialog to select a local file needed google searches. Found a solution. Running it showed another problem, that new directories where not created when saving the image.

rapflag: Add currency support in config file

See Issue 3

Changed the following stuff

  • Fix typo LOAD_ERROR -> LoadError when require 'pry'
  • Renamed bin/rapflag.rb -> bin/rapflag
  • Added support for reading desired currencies from config.yml
  • Search first for etc/config.yml in current directory
  • Bumped version to 0.0.2

Pushed commit Added support for more currencies. Rename bin/rapflag.rb -> bin/rapflag

Pushed a small fix Fixed default values

  • Fix dosing links for Niereninssuffizienz in oddb.org

The dosing links must have now the format http://www.dosing.de/popup_niere.php?monoid=422. Also the monoid have changed. Therefore I must change first the parser. Fortunately it has unit tests. But its unit test never test parsing the html, as instead of parsing a HTML page, it just returns a mock of the generated links.

Things were fairly easy to fix. Running sudo -u apache /usr/local/bin/bundle-240 exec ruby-240 jobs/update_atc_dosing_link to see, whether it works correctly.

Looking at the similar problem for the drugbank updater.

Unfortunately my changes for dosing did not work. After the update job I see in bin/admin

ch.oddb> atc_class('M01AG01')
-> Mefenaminsäure
ch.oddb> atc_class('M01AG01').ni_id
-> 
ch.oddb> atc_class('M01AG01').ni_id.class
-> NilClass

Added a pry debug statement and I see

[28] pry(#<ODDB::DosingPlugin>)> x.collect{|x| [x.code, x.to_s]}
=> [["A01AA01", "Natriumfluorid"], ["A01AA51", "Natriumfluorid, Kombinationen"], ["A12CD01", "Natriumfluorid"], ["A12CD51", "Natriumfluorid, Kombinationen"]]
[30] pry(#<ODDB::DosingPlugin>)> @links.size
=> 823
[31] pry(#<ODDB::DosingPlugin>)> @app.atc_classes.size
=> 7872

Also I think it would be far more efficient to iterate over the 823 links instead of the 7872 atc_classes. Also we have several ambiguities, eg. for Natriumflorid.

Will continue next week.

view · edit · sidebar · attach · print · history
Page last modified on March 29, 2017, at 08:18 PM