原田です。季節は秋になっているはずですけど、まだまだ暑いですね。今も外履きはサンダルで過ごしている私ですけど、そろそろ靴を履くべきか悩んでいます。できればもう少しこの暑さを楽しんでいたいですね。
今更何を言っているのかと思われる方もいらっしゃるでしょうけど、添付ファイル機能は地味ですがとても便利です。短い文章だけでは伝えることが難しい内容も、画像や各種ファイルを組み合わせることで自分の思いを伝えることができます。
今回は添付ファイルを管理している 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のデータ利用方法(UserPrefernce 編) Redmineの中で定義されているモデル「UserPreference」のようなデータの持ち方は、Redmineをもっと便利にすると思います。 |
|
台湾最大オープンソースイベントCOSCUPは熱かった! IT先進国台湾で開催されたオープンソースソフトウェアのイベントに参加してきました。 |
|
仕事をしながら深圳大学に1ヶ月留学してきた記録 中国の深圳大学に語学留学して、中国語を4週間学んできました。授業の様子と滞在中の生活を紹介。 |
|
ファーエンドテクノロジーに入社して2ヶ月が経ちました 7月にファーエンドテクノロジーに入社したサポート担当の福田です。よろしくお願いします。 |
|
社員旅行の段取りをRedmineで行いました Redmineのwiki、フォーラム、ニュース、バージョンなどの機能を活用して、社員旅行の段取りを行いました。 |
入門Redmine 第6版 出版記念セミナー「Redmine 8年分の新機能ふりかえり」【2024/4/18開催】 入門Redmine 第6版(2024年3月23日発売)に掲載された新機能に関する内容を執筆者・監修者が紹介します。 |
|
My Redmine 初回ご契約で「入門Redmine 第6版」プレゼントのお知らせ Redmineのクラウドサービス「My Redmine」を初めてご契約いただいたお客様にRedmine解説書「入門Redmine 第6版」を進呈いたします。 |
|
2024年度ブランドパートナーに島根県在住のモデル ユイさんを継続起用 ユイさん(モデルスタジオミューズ所属)をファーエンドテクノロジーの2024年度ブランドパートナーとして継続して起用します。 |
|
My Redmine スタンダードプランおよびAdminサポートデスクプランの料金改定のお知らせ【2024年4月ご利用分より】 2024年4月ご利用分より、My Redmine スタンダードプラン(民間企業・個人向け及び官公庁向け)とAdminサポートデスクプランの料金を改定いたします。 |
|
Redmineの最新情報をメールでお知らせする「Redmine News」配信中 新バージョンやセキュリティ修正のリリース情報、そのほか最新情報を迅速にお届け |