2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 13:17:42 +00:00
|
|
|
class LfsObject < ApplicationRecord
|
2017-12-08 09:09:06 +00:00
|
|
|
include AfterCommitQueue
|
2018-02-21 16:43:21 +00:00
|
|
|
include ObjectStorage::BackgroundMove
|
2017-12-08 09:09:06 +00:00
|
|
|
|
2017-06-08 15:16:27 +00:00
|
|
|
has_many :lfs_objects_projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
|
2019-06-06 06:51:50 +00:00
|
|
|
has_many :projects, -> { distinct }, through: :lfs_objects_projects
|
2015-10-12 14:42:14 +00:00
|
|
|
|
2018-10-05 12:20:12 +00:00
|
|
|
scope :with_files_stored_locally, -> { where(file_store: LfsObjectUploader::Store::LOCAL) }
|
2018-02-21 16:43:21 +00:00
|
|
|
|
2015-10-12 14:42:14 +00:00
|
|
|
validates :oid, presence: true, uniqueness: true
|
|
|
|
|
|
|
|
mount_uploader :file, LfsObjectUploader
|
2015-12-03 13:59:10 +00:00
|
|
|
|
2019-01-15 21:05:36 +00:00
|
|
|
after_save :update_file_store, if: :saved_change_to_file?
|
2018-03-23 10:07:22 +00:00
|
|
|
|
|
|
|
def update_file_store
|
2018-04-13 08:20:07 +00:00
|
|
|
# The file.object_store is set during `uploader.store!`
|
|
|
|
# which happens after object is inserted/updated
|
|
|
|
self.update_column(:file_store, file.object_store)
|
2018-03-23 10:07:22 +00:00
|
|
|
end
|
|
|
|
|
2015-12-07 14:03:50 +00:00
|
|
|
def project_allowed_access?(project)
|
2017-11-08 12:59:48 +00:00
|
|
|
projects.exists?(project.lfs_storage_project.id)
|
2015-12-07 14:03:50 +00:00
|
|
|
end
|
2016-10-28 17:39:20 +00:00
|
|
|
|
2018-02-21 16:43:21 +00:00
|
|
|
def local_store?
|
2018-10-05 12:20:12 +00:00
|
|
|
file_store == LfsObjectUploader::Store::LOCAL
|
2018-02-21 16:43:21 +00:00
|
|
|
end
|
|
|
|
|
2018-08-16 12:46:40 +00:00
|
|
|
# rubocop: disable DestroyAll
|
2016-10-28 17:39:20 +00:00
|
|
|
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
|
2018-08-16 12:46:40 +00:00
|
|
|
# rubocop: enable DestroyAll
|
2018-03-05 11:16:17 +00:00
|
|
|
|
|
|
|
def self.calculate_oid(path)
|
|
|
|
Digest::SHA256.file(path).hexdigest
|
|
|
|
end
|
2015-10-12 14:42:14 +00:00
|
|
|
end
|