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
|
|
|
|
2021-05-26 18:10:52 +00:00
|
|
|
has_many :lfs_objects_projects
|
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) }
|
2021-12-10 00:13:05 +00:00
|
|
|
scope :for_oid_and_size, -> (oid, size) { find_by(oid: oid, size: size) }
|
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
|
|
|
|
2021-05-27 15:10:39 +00:00
|
|
|
BATCH_SIZE = 3000
|
|
|
|
|
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
|
|
|
|
|
2021-05-27 15:10:39 +00:00
|
|
|
def self.unreferenced_in_batches
|
|
|
|
each_batch(of: BATCH_SIZE, order: :desc) do |lfs_objects|
|
|
|
|
relation = lfs_objects.where('NOT EXISTS (?)',
|
|
|
|
LfsObjectsProject.select(1).where('lfs_objects_projects.lfs_object_id = lfs_objects.id'))
|
|
|
|
|
|
|
|
yield relation if relation.any?
|
|
|
|
end
|
2016-10-28 17:39:20 +00:00
|
|
|
end
|
2018-03-05 11:16:17 +00:00
|
|
|
|
|
|
|
def self.calculate_oid(path)
|
2021-09-27 15:10:13 +00:00
|
|
|
self.sha256_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
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
LfsObject.prepend_mod_with('LfsObject')
|