0

I am trying to automate filling out a form (which includes a file upload) using Mechanize. I have gone through the process in the GUI interface and the file uploads fine, so I know the file isn't corrupt, but when I run my mechanize script it fails. The script executes correctly, and according to the debug it uploaded the file, but Canvas (the service I'm uploading to) says the file could not be read. I have contacted Canvas support but they are unable to help since it's a non-standard use of their system.

Here is the script (which has been anonymized):

 09 mech = Mechanize.new
 10 mech.log = Logger.new(STDOUT)
 11 mech.user_agent_alias = 'Mac Mozilla'
 12 mech.get("https://ucdenver.test.instructure.com") do |page|
 13   page.form_with(:action => "/login") do |f|
 14     user_field = f.field_with(:name => "pseudonym_session[unique_id]")
 15     user_field.value = user
 16     pwd_field = f.field_with(:name => "pseudonym_session[password]")
 17     pwd_field.value  = pwd
 18   end.submit
 19 end
 20
 21 mech.get("https://ucdenver.test.instructure.com/accounts/1") do |page|
 22   page.form_with(:action => "/accounts/1/courses") do |f|
 23     course_field = f.field_with(:name => "course[name]")
 24     course_field.value = "38492"
 25   end.submit
 26 end
 27
 28 mech.page.link_with(:href  => %r/settings/,
 29                     :class => "settings").click
 30
 31 mech.page.link_with(:href => %r/import/,
 32                     :text => %r/Import Content/).click
 33
 34 mech.page.link_with(:href => %r/imports\/migrate/,
 35                     :text => %r/Import content from a content package/).click
 36
 37 mech.page.form_with(:action => %r/imports\/migrate/) do |f|
 38   export_system = "migration_settings[migration_type]"
 39   f[export_system] = "blackboard_exporter"
 40   f.file_uploads.first.mime_type = "application/zip"
 41   f.file_uploads.first.file_name = bkb_export
 42 end.submit

Canvas support said the server is returning:

Could not unzip archive file, exit status 9, message:     
[/mnt/var/web/migration_tool/data/attachment_420130214-13303-sv6ue5-0.jpg] End-of-central-
directory signature not found. Either this file is not a zipfile, or it constitutes one 
disk of a multi-part archive. In the latter case the central directory and zipfile comment 
will be found on the last disk(s) of this archive. unzip: cannot find zipfile directory in 
one of /mnt/var/web/migration_tool/data/attachment_420130214-13303-sv6ue5-0.jpg or 
/mnt/var/web/migration_tool/data/attachment_420130214-13303-sv6ue5-0.jpg.zip, and cannot 
find /mnt/var/web/migration_tool/data/attachment_420130214-13303-sv6ue5-0.jpg.ZIP, period.

Unfortunately that error makes no sense to me, but it seems odd that it references a couple of .jpg's when the script is uploading a .zip file. Any ideas or assistance would be greatly appreciated, and I'm happy to provide additional information if I've left anything useful out.

If you're really feeling like a go getter you can sign up for a free account at http://canvas.instructure.com and see the code/network activity for yourself.

lyonsinbeta
  • 919
  • 6
  • 25
  • are those files that it is complaining about part of the zip you are trying to upload? Also for giggles have you tried setting the mime_type on f.file_uploads.first to `application/zip` – Doon Feb 20 '13 at 16:06
  • The .zip I am trying to upload is an export from the Blackboard learning management system. I think I tested the mim_type, but I'm going to test it again to be sure. – lyonsinbeta Feb 20 '13 at 20:08
  • Updated my question; setting the `mime_type` doesn't seem to have an effect. – lyonsinbeta Feb 20 '13 at 20:20
  • i realize the zip is an export from blackboard, if you unzip it manually do you get the contents you see in the error log above? – Doon Feb 21 '13 at 01:57
  • Those files do not appear in the contents of the zip. In fact, this particular zip doesn't have any jpg files from what I can see. – lyonsinbeta Feb 21 '13 at 05:44

1 Answers1

0

Make sure you are uploading file with correct type afaik ruby-mechanize specify

Content-Type: application/octet-stream

for every type of file, chances are server is checking ContentType and is not matching with required content type correctly and I don't remember the exact version of mechanize that i used but for me it was hardcoded in mechanize gem so you have to go through the source code of mechanize and fix where it is hardcoding content type for multipart/form-data file element.

user2009750
  • 3,169
  • 5
  • 35
  • 58