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
|
2017-12-08 04:09:06 -05:00
|
|
|
|
2017-06-08 11:16:27 -04:00
|
|
|
has_many :lfs_objects_projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
|
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) }
|
2018-02-21 11:43:21 -05:00
|
|
|
|
2015-10-12 10:42:14 -04:00
|
|
|
validates :oid, presence: true, uniqueness: true
|
|
|
|
|
|
|
|
mount_uploader :file, LfsObjectUploader
|
2015-12-03 08:59:10 -05:00
|
|
|
|
2019-01-15 16:05:36 -05:00
|
|
|
after_save :update_file_store, if: :saved_change_to_file?
|
2018-03-23 06:07:22 -04:00
|
|
|
|
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
|
|
|
|
|
2018-03-23 06:07:22 -04:00
|
|
|
def update_file_store
|
2018-04-13 04:20:07 -04: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 06:07:22 -04:00
|
|
|
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
|
|
|
|
|
2018-08-16 08:46:40 -04:00
|
|
|
# rubocop: disable DestroyAll
|
2016-10-28 13:39:20 -04: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 08:46:40 -04:00
|
|
|
# rubocop: enable DestroyAll
|
2018-03-05 06:16:17 -05:00
|
|
|
|
|
|
|
def self.calculate_oid(path)
|
2019-10-08 20:06:06 -04:00
|
|
|
self.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
|
|
|
|
|
|
|
LfsObject.prepend_if_ee('EE::LfsObject')
|