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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# frozen_string_literal: true
module Packages
module Debian
module ComponentFile
extend ActiveSupport::Concern
included do
include Sortable
include FileStoreMounter
def self.container_foreign_key
"#{container_type}_id".to_sym
end
def self.distribution_class
"::Packages::Debian::#{container_type.capitalize}Distribution".constantize
end
belongs_to :component, class_name: "Packages::Debian::#{container_type.capitalize}Component", inverse_of: :files
belongs_to :architecture, class_name: "Packages::Debian::#{container_type.capitalize}Architecture", inverse_of: :files, optional: true
enum file_type: { packages: 1, source: 2, di_packages: 3 }
enum compression_type: { gz: 1, bz2: 2, xz: 3 }
validates :component, presence: true
validates :file_type, presence: true
validates :architecture, presence: true, unless: :source?
validates :architecture, absence: true, if: :source?
validates :file, length: { minimum: 0, allow_nil: false }
validates :size, presence: true
validates :file_store, presence: true
validates :file_md5, presence: true
validates :file_sha256, presence: true
scope :with_container, ->(container) do
joins(component: :distribution)
.where("packages_debian_#{container_type}_distributions" => { container_foreign_key => container.id })
end
scope :with_codename_or_suite, ->(codename_or_suite) do
joins(component: :distribution)
.merge(distribution_class.with_codename_or_suite(codename_or_suite))
end
scope :with_component_name, ->(component_name) do
joins(:component)
.where("packages_debian_#{container_type}_components" => { name: component_name })
end
scope :with_file_type, ->(file_type) { where(file_type: file_type) }
scope :with_architecture_name, ->(architecture_name) do
left_outer_joins(:architecture)
.where("packages_debian_#{container_type}_architectures" => { name: architecture_name })
end
scope :with_compression_type, ->(compression_type) { where(compression_type: compression_type) }
scope :with_file_sha256, ->(file_sha256) { where(file_sha256: file_sha256) }
scope :preload_distribution, -> { includes(component: :distribution) }
mount_file_store_uploader Packages::Debian::ComponentFileUploader
before_validation :update_size_from_file
def file_name
case file_type
when 'di_packages'
'Packages'
else
file_type.capitalize
end
end
def relative_path
case file_type
when 'packages'
"#{component.name}/binary-#{architecture.name}/#{file_name}#{extension}"
when 'source'
"#{component.name}/source/#{file_name}#{extension}"
when 'di_packages'
"#{component.name}/debian-installer/binary-#{architecture.name}/#{file_name}#{extension}"
end
end
private
def extension
return '' unless compression_type
".#{compression_type}"
end
def update_size_from_file
self.size ||= file.size
end
end
end
end
end
|