2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class LfsObject < ApplicationRecord
|
2017-12-08 04:09:06 -05:00
|
|
|
include AfterCommitQueue
|
2019-10-08 20:06:06 -04:00
|
|
|
include Checksummable
|
2019-08-27 09:07:38 -04:00
|
|
|
include EachBatch
|
2018-02-21 11:43:21 -05:00
|
|
|
include ObjectStorage::BackgroundMove
|
2020-08-13 14:10:36 -04:00
|
|
|
include FileStoreMounter
|
2017-12-08 04:09:06 -05:00
|
|
|
|
2021-05-26 14:10:52 -04:00
|
|
|
has_many :lfs_objects_projects
|
2019-06-06 02:51:50 -04:00
|
|
|
has_many :projects, -> { distinct }, through: :lfs_objects_projects
|
2015-10-12 10:42:14 -04:00
|
|
|
|
2018-10-05 08:20:12 -04:00
|
|
|
scope :with_files_stored_locally, -> { where(file_store: LfsObjectUploader::Store::LOCAL) }
|
2019-09-20 08:05:52 -04:00
|
|
|
scope :with_files_stored_remotely, -> { where(file_store: LfsObjectUploader::Store::REMOTE) }
|
2020-02-21 16:08:57 -05:00
|
|
|
scope :for_oids, -> (oids) { where(oid: oids) }
|
2021-12-09 19:13:05 -05:00
|
|
|
scope :for_oid_and_size, -> (oid, size) { find_by(oid: oid, size: size) }
|
2018-02-21 11:43:21 -05:00
|
|
|
|
2015-10-12 10:42:14 -04:00
|
|
|
validates :oid, presence: true, uniqueness: true
|
|
|
|
|
2020-08-13 14:10:36 -04:00
|
|
|
mount_file_store_uploader LfsObjectUploader
|
2018-03-23 06:07:22 -04:00
|
|
|
|
2021-05-27 11:10:39 -04:00
|
|
|
BATCH_SIZE = 3000
|
|
|
|
|
2019-11-08 10:06:21 -05: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 09:03:50 -05:00
|
|
|
def project_allowed_access?(project)
|
2019-09-30 23:05:57 -04: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 09:03:50 -05:00
|
|
|
end
|
2016-10-28 13:39:20 -04:00
|
|
|
|
2018-02-21 11:43:21 -05:00
|
|
|
def local_store?
|
2018-10-05 08:20:12 -04:00
|
|
|
file_store == LfsObjectUploader::Store::LOCAL
|
2018-02-21 11:43:21 -05:00
|
|
|
end
|
|
|
|
|
2021-05-27 11:10:39 -04: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 13:39:20 -04:00
|
|
|
end
|
2018-03-05 06:16:17 -05:00
|
|
|
|
|
|
|
def self.calculate_oid(path)
|
2021-09-27 11:10:13 -04:00
|
|
|
self.sha256_hexdigest(path)
|
2018-03-05 06:16:17 -05:00
|
|
|
end
|
2015-10-12 10:42:14 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
LfsObject.prepend_mod_with('LfsObject')
|