Welcome to mirror list, hosted at ThFree Co, Russian Federation.

metadata_cache.rb « npm « packages « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b6ab2a88a9811e190321a39cf8731f334b3ab654 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

module Packages
  module Npm
    class MetadataCache < ApplicationRecord
      include FileStoreMounter
      include Packages::Downloadable
      include Packages::Destructible

      enum status: { default: 0, processing: 1, error: 3 }

      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

      scope :stale, -> { where(project_id: nil) }
      scope :pending_destruction, -> { stale.default }

      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