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
|
2019-10-09 00:06:06 +00:00
|
|
|
include Checksummable
|
2019-08-27 13:07:38 +00:00
|
|
|
include EachBatch
|
2018-02-21 16:43:21 +00:00
|
|
|
include ObjectStorage::BackgroundMove
|
2020-08-13 18:10:36 +00:00
|
|
|
include FileStoreMounter
|
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) }
|
2019-09-20 12:05:52 +00:00
|
|
|
scope :with_files_stored_remotely, -> { where(file_store: LfsObjectUploader::Store::REMOTE) }
|
2020-02-21 21:08:57 +00:00
|
|
|
scope :for_oids, -> (oids) { where(oid: oids) }
|
2018-02-21 16:43:21 +00:00
|
|
|
|
2015-10-12 14:42:14 +00:00
|
|
|
validates :oid, presence: true, uniqueness: true
|
|
|
|
|
2020-08-13 18:10:36 +00:00
|
|
|
mount_file_store_uploader LfsObjectUploader
|
2018-03-23 10:07:22 +00:00
|
|
|
|
2019-11-08 15:06:21 +00:00
|
|
|
def self.not_linked_to_project(project)
|
|
|
|
where('NOT EXISTS (?)',
|
|
|
|
project.lfs_objects_projects.select(1).where('lfs_objects_projects.lfs_object_id = lfs_objects.id'))
|
|
|
|
end
|
|
|
|
|
2015-12-07 14:03:50 +00:00
|
|
|
def project_allowed_access?(project)
|
2019-10-01 03:05:57 +00:00
|
|
|
if project.fork_network_member
|
|
|
|
lfs_objects_projects
|
|
|
|
.where("EXISTS(?)", project.fork_network.fork_network_members.select(1).where("fork_network_members.project_id = lfs_objects_projects.project_id"))
|
|
|
|
.exists?
|
|
|
|
else
|
|
|
|
lfs_objects_projects.where(project_id: project.id).exists?
|
|
|
|
end
|
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
|
|
|
|
|
2020-05-28 21:08:22 +00:00
|
|
|
# rubocop: disable Cop/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
|
2020-05-28 21:08:22 +00:00
|
|
|
# rubocop: enable Cop/DestroyAll
|
2018-03-05 11:16:17 +00:00
|
|
|
|
|
|
|
def self.calculate_oid(path)
|
2019-10-09 00:06:06 +00:00
|
|
|
self.hexdigest(path)
|
2018-03-05 11:16:17 +00:00
|
|
|
end
|
2015-10-12 14:42:14 +00:00
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
LfsObject.prepend_if_ee('EE::LfsObject')
|