diff options
Diffstat (limited to 'app/models/packages/npm/metadata_cache.rb')
-rw-r--r-- | app/models/packages/npm/metadata_cache.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/app/models/packages/npm/metadata_cache.rb b/app/models/packages/npm/metadata_cache.rb new file mode 100644 index 00000000000..7a7c66d7a45 --- /dev/null +++ b/app/models/packages/npm/metadata_cache.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module Packages + module Npm + class MetadataCache < ApplicationRecord + include FileStoreMounter + + belongs_to :project, inverse_of: :npm_metadata_caches + + validates :file, :object_storage_key, :package_name, :project, :size, presence: true + validates :package_name, uniqueness: { scope: :project_id } + validates :package_name, format: { with: Gitlab::Regex.package_name_regex } + validates :package_name, format: { with: Gitlab::Regex.npm_package_name_regex } + + mount_file_store_uploader MetadataCacheUploader + + before_validation :set_object_storage_key + attr_readonly :object_storage_key + + def self.find_or_build(package_name:, project_id:) + find_or_initialize_by( + package_name: package_name, + project_id: project_id + ) + end + + private + + def set_object_storage_key + return unless package_name && project_id + + self.object_storage_key = Gitlab::HashedPath.new( + 'packages', 'metadata_caches', 'npm', OpenSSL::Digest::SHA256.hexdigest(package_name), + root_hash: project_id + ).to_s + end + end + end +end |