forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_space.rb
40 lines (34 loc) · 922 Bytes
/
disk_space.rb
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
# frozen_string_literal: true
class DiskSpace
def self.uploads_used_bytes
if Discourse.store.external?
Upload.sum(:filesize).to_i + OptimizedImage.sum(:filesize).to_i
else
used(uploads_path)
end
end
def self.uploads_free_bytes
if Discourse.store.external?
0
else
free(uploads_path)
end
end
def self.free(path)
output = Discourse::Utils.execute_command("df", "-Pk", path)
size_line = output.split("\n")[1]
size_line.split(/\s+/)[3].to_i * 1024
end
def self.percent_free(path)
output = Discourse::Utils.execute_command("df", "-P", path)
size_line = output.split("\n")[1]
size_line.split(/\s+/)[4].to_i
end
def self.used(path)
Discourse::Utils.execute_command("du", "-s", path).to_i * 1024
end
def self.uploads_path
"#{Rails.root}/public/#{Discourse.store.upload_path}"
end
private_class_method :uploads_path
end