2
我正在嘗試批量上傳圖片到Redmine,並將它們分別鏈接到某個wiki頁面。如何通過ActiveResource/REST API將文件上傳到Redmine?
文檔(Rest_api,Using the REST API with Ruby)提到了一些方面,但這些示例以各種方式失敗。我也嘗試從source中得出想法 - 沒有成功。
任何人都可以提供一個簡短的例子,顯示如何上傳和鏈接從Ruby內的圖像?
我正在嘗試批量上傳圖片到Redmine,並將它們分別鏈接到某個wiki頁面。如何通過ActiveResource/REST API將文件上傳到Redmine?
文檔(Rest_api,Using the REST API with Ruby)提到了一些方面,但這些示例以各種方式失敗。我也嘗試從source中得出想法 - 沒有成功。
任何人都可以提供一個簡短的例子,顯示如何上傳和鏈接從Ruby內的圖像?
由於附件和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
}
})
基於這樣的理念:https://github.com/GSI/mrmt/blob/master/lib/upload_files.rb – user569825