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
|
# 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
|