Read about our upcoming Code of Conduct on this issue

lfs_object.rb 1.25 KB
Newer Older
1
2
# frozen_string_literal: true

3
class LfsObject < ApplicationRecord
4
  include AfterCommitQueue
5
  include ObjectStorage::BackgroundMove
6

7
  has_many :lfs_objects_projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
8
  has_many :projects, -> { distinct }, through: :lfs_objects_projects
Marin Jankovski's avatar
Marin Jankovski committed
9

10
  scope :with_files_stored_locally, -> { where(file_store: LfsObjectUploader::Store::LOCAL) }
11

Marin Jankovski's avatar
Marin Jankovski committed
12
13
14
  validates :oid, presence: true, uniqueness: true

  mount_uploader :file, LfsObjectUploader
15

Jasper Maes's avatar
Jasper Maes committed
16
  after_save :update_file_store, if: :saved_change_to_file?
17
18

  def update_file_store
19
20
21
    # The file.object_store is set during `uploader.store!`
    # which happens after object is inserted/updated
    self.update_column(:file_store, file.object_store)
22
23
  end

24
  def project_allowed_access?(project)
25
    projects.exists?(project.lfs_storage_project.id)
26
  end
27

28
  def local_store?
29
    file_store == LfsObjectUploader::Store::LOCAL
30
31
  end

32
  # rubocop: disable DestroyAll
33
34
35
36
37
  def self.destroy_unreferenced
    joins("LEFT JOIN lfs_objects_projects ON lfs_objects_projects.lfs_object_id = #{table_name}.id")
        .where(lfs_objects_projects: { id: nil })
        .destroy_all
  end
38
  # rubocop: enable DestroyAll
39
40
41
42

  def self.calculate_oid(path)
    Digest::SHA256.file(path).hexdigest
  end
Marin Jankovski's avatar
Marin Jankovski committed
43
end