Question

I am trying to batch-upload images to Redmine and link them each to a certain wiki pages.

The docs (Rest_api, Using the REST API with Ruby) mention some aspects, but the examples fail in various ways. I also tried to derive ideas from the source - without success.

Can anyone provide a short example that shows how to upload and link an image from within Ruby?

Was it helpful?

Solution

This is a bit tricky as both attachments and wiki APIs are relatively new, but I have done something similar in the past. Here is a minimal working example using rest-client:

require 'rest_client'
require 'json'

key = '5daf2e447336bad7ed3993a6ebde8310ffa263bf'
upload_url = "http://localhost:3000/uploads.json?key=#{key}"
wiki_url = "http://localhost:3000/projects/some_project/wiki/some_wiki.json?key=#{key}"
img = File.new('/some/image.png')

# First we upload the image to get attachment token
response = RestClient.post(upload_url, img, {
  :multipart => true,
  :content_type => 'application/octet-stream'
})
token = JSON.parse(response)['upload']['token']

# Redmine will throw validation errors if you do not
# send a wiki content when attaching the image. So
# we just get the current content and send that
wiki_text = JSON.parse(RestClient.get(wiki_url))['wiki_page']['text']

response = RestClient.put(wiki_url, {
  :attachments => {
    :attachment1 => { # the hash key gets thrown away - name doesn't matter
      :token => token,
      :filename => 'image.png',
      :description => 'Awesome!' # optional
    }
  },
  :wiki_page => {
    :text => wiki_text # original wiki text
  }
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top