Project

General

Profile

How can I programmatically register files?

Added by S Allred over 11 years ago

I've posted this on SuperUser (http://superuser.com/questions/634799), but a conversation over chat suggested I ask here instead. (I'll transfer the answer if one is found.)

First off, as universally requested, the output of scripts/about:

root@redmine www/redmine# RAILS_ENV=production script/about

Environment:
  Redmine version                          2.2.3.stable
  Ruby version                             1.8.7 (x86_64-linux)
  Rails version                            3.2.12
  Environment                              production
  Database adapter                         MySQL
Redmine plugins:
  redmine_evm                              0.9.1
  redmine_wktime                           1.5

I've got a task to have very specific files from our SVN repository available to Redmine through its 'Files' tab. Eventually, I will need to convert these files to PDF, so simply connecting Redmine to the SVN repository doesn't help much. (I also need just an exact subset of files from SVN, so the whole repository is overkill.)

I need this to be an automated process, so that I can stick a script in a crontab and forget about it. It should be able to update the 'Files' tab all by itself.

I can manage retrieving the files from SVN and stuffing them all in a directory, but as we know simply moving them to redmine/files does not make them available to the web interface. In order for them to be available, they must be registered with the database Redmine uses for files, redmine_production.attachments. I've included the columns used below for convenience:

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)

There is a module of sorts within the Redmine sources called Attachment; it can be viewed from the official GitHub mirror (https://github.com/redmine/redmine/blob/master/app/models/attachment.rb).

I would like to know either

  • How I can mimic the functionality of Attachment.rb to perform all the steps necessary to add a file to Redmine, or
  • simply using Attachment.rb (if this module is all that is necessary?) to do the same. I apologize, but my knowledge of Ruby hardly extends beyond print "Hello, World!".reverse; I'm not sure what to make of Attachment.rb.

Thanks for your time :)