Friday, 23 August 2013

How can I programmatically register a file with Redmine?

How can I programmatically register a file with Redmine?

I'd like to add a file to a Redmine server without going through the
graphical interface. I'm making the files available to the Redmine server
by a separate svn checkout process, so I just need to be able to add the
files to Redmine's own database.
Ideally, I'd like a solution that could be run like this:
./redmine-register-file /path/to/my/file.ext "with optional description"
I believe the relevant part of the interface is found in
redmine/apps/views/files/new.html.erb. It's accessed by index.html.erb,
which has a small portion I believe is relevant:
<div class="contextual">
<%= link_to(l(:label_attachment_new), new_project_file_path(@project),
:class => 'icon icon-add') if User.current.allowed_to?(:manage_files,
@project) %>
</div>
Here is the complete contents of new.html.erb:
<h2><%=l(:label_attachment_new)%></h2>
<%= error_messages_for 'attachment' %>
<%= form_tag(project_files_path(@project), :multipart => true, :class =>
"tabular") do %>
<div class="box">
<% if @versions.any? %>
<p><label for="version_id"><%=l(:field_version)%></label>
<%= select_tag "version_id", content_tag('option', '') +
options_from_collection_for_select(@versions,
"id", "name") %></p>
<% end %>
<p><label><%=l(:label_attachment_plural)%></label><%= render :partial =>
'attachments/form' %></p>
</div>
<%= submit_tag l(:button_add) %>
<% end %>
I don't know Ruby that well at all (anything beyond print name.reverse is
beyond me), but I know that all of those colons indicate selectors. What
information can I glean from the standard interface that will help me in
my task, and what might a complete solution look like?



Closer to a solution:
Redmine uses a MySQL database to store its file registrations. The
database is called redmine_production, and uses the following schema:
mysql> SHOW COLUMNS FROM redmine_production.attachments;
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| container_id | int(11) | YES | MUL | NULL | |
| container_type | varchar(30) | YES | | NULL | |
| filename | varchar(255) | NO | | | |
| disk_filename | varchar(255) | NO | | | |
| filesize | int(11) | NO | | 0 | |
| content_type | varchar(255) | YES | | | |
| digest | varchar(40) | NO | | | |
| downloads | int(11) | NO | | 0 | |
| author_id | int(11) | NO | MUL | 0 | |
| created_on | datetime | YES | MUL | NULL | |
| description | varchar(255) | YES | | NULL | |
+----------------+--------------+------+-----+---------+----------------+
12 rows in set (0.00 sec)
Some more Ruby source
Perhaps this will be of use: attachment.rb

No comments:

Post a Comment