代码之家  ›  专栏  ›  技术社区  ›  user569825

如何通过ActiveResource/RESTAPI将文件上传到Redmine?

  •  2
  • user569825  · 技术社区  · 11 年前

    我正在尝试将图像批量上传到 雷明 并将它们分别链接到特定的wiki页面。

    文档( Rest_api , Using the REST API with Ruby )提到了一些方面,但这些例子在各种方面都失败了。我还试图从 source -没有成功。

    有人能提供一个简短的例子来展示如何从Ruby中上传和链接图像吗?

    1 回复  |  直到 11 年前
        1
  •  3
  •   mechanicalfish    11 年前

    这有点棘手,因为附件和wiki API都是相对较新的,但我过去也做过类似的事情。下面是一个使用 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
      }
    })