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
|
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) }
|
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
|
|
|
|
|
|
|
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)
|
2017-11-08 07:59:48 -05:00
|
|
|
projects.exists?(project.lfs_storage_project.id)
|
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)
|
|
|
|
Digest::SHA256.file(path).hexdigest
|
|
|
|
end
|
2015-10-12 10:42:14 -04:00
|
|
|
end
|