原田です。季節は秋になっているはずですけど、まだまだ暑いですね。今も外履きはサンダルで過ごしている私ですけど、そろそろ靴を履くべきか悩んでいます。できればもう少しこの暑さを楽しんでいたいですね。
今更何を言っているのかと思われる方もいらっしゃるでしょうけど、添付ファイル機能は地味ですがとても便利です。短い文章だけでは伝えることが難しい内容も、画像や各種ファイルを組み合わせることで自分の思いを伝えることができます。
今回は添付ファイルを管理している Attachment についてご紹介します。
Enter ".help" for usage hints. sqlite> .header on sqlite> .mode column sqlite> .width 10 20 sqlite> pragma table_info('attachments'); cid name type notnull dflt_value pk ---------- -------------------- ---------- ---------- ---------- ---------- 0 id integer 1 1 1 container_id integer 0 0 2 container_type varchar(30 0 0 3 filename varchar 1 '' 0 4 disk_filename varchar 1 '' 0 5 filesize integer(8) 1 0 0 6 content_type varchar 0 '' 0 7 digest varchar(64 1 '' 0 8 downloads integer 1 0 0 9 author_id integer 1 0 0 10 created_on datetime 0 0 11 description varchar 0 0 12 disk_directory varchar 0 0
attachmentsテーブルの構造
Attachment は、Redmineの公式サイト上では2006年6月29日の リビジョン 4 が初登場ですけど、2006年6月25日にリリースされたRedmine 0.1.0には既に含まれていたのではないかと思われます。当時のAttachmentは80行程度のシンプルなものでしたが、2019年9月時点の 最新版 では500行を超え多機能になっていると想像できます。
Redmineは チケット・Wiki・文書・ニュース・フォーラム で添付ファイルを扱うことができます。Attachmentとこれらの画面で使われているモデルとの連携はAttachmentに定義されている belongs_to :container, :polymorphic => true と acts_as_attachable を利用し実現しています(acts_as_attachableに関しては別の機会にご紹介したいと思います)。
class Attachment < ActiveRecord::Base include Redmine::SafeAttributes belongs_to :container, :polymorphic => true belongs_to :author, :class_name => "User"
Attachment#container 宣言部(app/models/attachment.rb)
class Issue < ActiveRecord::Base ... has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all acts_as_attachable :after_add => :attachment_added, :after_remove => :attachment_removed
acts_as_attachable 宣言部(app/models/issue.rb)
添付ファイルは各画面上からアップロードすると[Redmineインストールディレクトリ] / filesディレクトリ下に保存されます。
Redmineを使用していると同じファイルを上記のさまざまな画面から複数回アップロード(添付)することもあり得ると思います。Attachmentではafter_commitコールバック実行時にAttachment#reuse_existing_file_if_possible を呼び出し同じファイルは同一のものとして扱い複数保存することを防いでいます。これは同一ファイルを再利用しハードディスクの使用量を減らすためにRedmine 3.4.0で追加された機能(Redmine.org - Patch #25215: Re-use existing identical disk files for new attachments)です。
class Attachment < ActiveRecord::Base ... after_commit :reuse_existing_file_if_possible, :on => :create ... private def reuse_existing_file_if_possible original_diskfile = nil reused = with_lock do if existing = Attachment .where(digest: self.digest, filesize: self.filesize) .where('id <> ? and disk_filename <> ?', self.id, self.disk_filename) .first existing.with_lock do original_diskfile = self.diskfile existing_diskfile = existing.diskfile if File.readable?(original_diskfile) && File.readable?(existing_diskfile) && FileUtils.identical?(original_diskfile, existing_diskfile) self.update_columns disk_directory: existing.disk_directory, disk_filename: existing.disk_filename end end end end if reused File.delete(original_diskfile) end rescue ActiveRecord::StatementInvalid, ActiveRecord::RecordNotFound # Catch and ignore lock errors. It is not critical if deduplication does # not happen, therefore we do not retry. # with_lock throws ActiveRecord::RecordNotFound if the record isnt there # anymore, thats why this is caught and ignored as well. end
同一ファイルの再利用処理(再利用時はアップロードしたファイルを削除)
画像ファイルのサムネイルを表示する機能(Redmine.org - Feature #3510: Inserting image thumbnails inside the wiki)とPDFファイルのサムネイル表示する機能(Redmine.org - Feature #22481: Show thumbnails for PDF attachments)がRedmineに組み込まれたことにより、添付ファイルが画像またはPDFであればサムネイルの作成が可能になりました。
クラウドを利用してRedmineを動かしているという記事を見る機会が増えました。クラウド環境としてはAmazon Web Services(AWS)を利用されている方が多いようです。Redmineはオンプレで稼働するけど、ファイルの損失を防ぐため「添付ファイルはクラウドに保存したい」という要望もあるかと思います。
そこでRedmineのプラグインを探してみたところ、 Redmine S3 であればクラウドストレージサービス(Amazon S3)に添付ファイルの保存ができるようです。

|
Redmineの中で定義されているモデル「UserPreference」のようなデータの持ち方は、Redmineをもっと便利にすると思います。 |
|
IT先進国台湾で開催されたオープンソースソフトウェアのイベントに参加してきました。 |
|
中国の深圳大学に語学留学して、中国語を4週間学んできました。授業の様子と滞在中の生活を紹介。 |
|
7月にファーエンドテクノロジーに入社したサポート担当の福田です。よろしくお願いします。 |
|
Redmineのwiki、フォーラム、ニュース、バージョンなどの機能を活用して、社員旅行の段取りを行いました。 |
|
オープンソースカンファレンス2026 Shimane(7/11開催)登壇・ブース出展 2026年7月11日(土)に松江テルサ(島根県松江市)で開催される「オープンソースカンファレンス2026 Shimane」に登壇・ブース出展します。 |
|
プロジェクト管理SaaS「My Redmine」の「IPアドレスフィルター」で登録可能な「IPアドレス数」の上限を大幅に引き上げ IPアドレスによる制限の上限が、エンタープライズプランでは従来の5倍となる2,000個まで、スタンダード・ミディアムプランでは2倍の200個に引き上げ |
|
2026年7月8日 オライリー本の全冊公開日のお知らせ(もくもく勉強会も同時開催) ファーエンドテクノロジーが所蔵するオライリー本(全冊)公開日のご案内です。公開日には「もくもく勉強会」も同時開催します。 |
|
Redmineのクラウド版「My Redmine」に大容量1.5TBの「エンタープライズ1.5TBプラン」が新登場! オープンソースのプロジェクト管理ソフトウェアRedmineのクラウド版「My Redmine」に、月々40,000円(税別)の定額で1.5TB、2,000ユーザーまで利用できる大容量プランが登場。 |
|
Redmineの最新情報をメールでお知らせする「Redmine News」配信中 新バージョンやセキュリティ修正のリリース情報、そのほか最新情報を迅速にお届け |