Commit 9e24ea98 authored by Maxime Orefice's avatar Maxime Orefice Committed by Robert Speicher

Introduce HashedPath class

This class centralizes the business logic of
disk path for a given uploader.
parent 0949c003
......@@ -36,15 +36,10 @@ class JobArtifactUploader < GitlabUploader
end
def hashed_path
File.join(disk_hash[0..1], disk_hash[2..3], disk_hash,
model.created_at.utc.strftime('%Y_%m_%d'), model.job_id.to_s, model.id.to_s)
Gitlab::HashedPath.new(model.created_at.utc.strftime('%Y_%m_%d'), model.job_id, model.id, root_hash: model.project_id)
end
def legacy_path
File.join(model.created_at.utc.strftime('%Y_%m'), model.project_id.to_s, model.job_id.to_s)
end
def disk_hash
@disk_hash ||= Digest::SHA2.hexdigest(model.project_id.to_s)
end
end
......@@ -20,11 +20,6 @@ class Packages::PackageFileUploader < GitlabUploader
private
def dynamic_segment
File.join(disk_hash[0..1], disk_hash[2..3], disk_hash,
'packages', model.package.id.to_s, 'files', model.id.to_s)
end
def disk_hash
@disk_hash ||= Digest::SHA2.hexdigest(model.package.project_id.to_s)
Gitlab::HashedPath.new('packages', model.package.id, 'files', model.id, root_hash: model.package.project_id)
end
end
......@@ -18,11 +18,6 @@ class DependencyProxy::FileUploader < GitlabUploader
private
def dynamic_segment
File.join(disk_hash[0..1], disk_hash[2..3], disk_hash,
'dependency_proxy', model.group_id.to_s, 'files', model.id.to_s)
end
def disk_hash
@disk_hash ||= Digest::SHA2.hexdigest(model.group_id.to_s)
Gitlab::HashedPath.new('dependency_proxy', model.group_id, 'files', model.id, root_hash: model.group_id)
end
end
# frozen_string_literal: true
# Class that returns the disk path for a model using hashed storage
module Gitlab
class HashedPath
def initialize(*paths, root_hash:)
@paths = paths
@root_hash = root_hash
end
def to_s
File.join(disk_hash[0..1], disk_hash[2..3], disk_hash, @paths.map(&:to_s))
end
alias_method :to_str, :to_s
private
def disk_hash
@disk_hash ||= Digest::SHA2.hexdigest(@root_hash.to_s)
end
end
end
# frozen_string_literal: true
require 'fast_spec_helper'
RSpec.describe Gitlab::HashedPath do
let(:root_hash) { 1 }
let(:hashed_path) { described_class.new(*path, root_hash: root_hash) }
describe '#to_s' do
subject { hashed_path }
context 'when path contains a single value' do
let(:path) { 'path' }
it 'returns the disk path' do
expect(subject).to match(%r[\h{2}/\h{2}/\h{64}/path])
end
end
context 'when path contains multiple values' do
let(:path) { %w(path1 path2) }
it 'returns the disk path' do
expect(subject).to match(%r[\h{2}/\h{2}/\h{64}/path1/path2])
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment