2020-10-29 11:49:09 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Reports::TopUploads
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def report_top_uploads(report)
|
|
|
|
report.modes = [:table]
|
|
|
|
|
|
|
|
extension_filter = report.filters.dig(:file_extension)
|
2023-01-09 06:20:10 -06:00
|
|
|
report.add_filter(
|
|
|
|
"file_extension",
|
|
|
|
type: "list",
|
|
|
|
default: extension_filter || "any",
|
|
|
|
choices: (SiteSetting.authorized_extensions.split("|") + Array(extension_filter)).uniq,
|
2020-10-29 11:49:09 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
report.labels = [
|
|
|
|
{
|
|
|
|
type: :link,
|
2023-01-09 06:20:10 -06:00
|
|
|
properties: %i[file_url file_name],
|
|
|
|
title: I18n.t("reports.top_uploads.labels.filename"),
|
2020-10-29 11:49:09 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: :user,
|
|
|
|
properties: {
|
|
|
|
username: :author_username,
|
|
|
|
id: :author_id,
|
|
|
|
avatar: :author_avatar_template,
|
|
|
|
},
|
2023-01-09 06:20:10 -06:00
|
|
|
title: I18n.t("reports.top_uploads.labels.author"),
|
2020-10-29 11:49:09 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: :text,
|
|
|
|
property: :extension,
|
2023-01-09 06:20:10 -06:00
|
|
|
title: I18n.t("reports.top_uploads.labels.extension"),
|
2020-10-29 11:49:09 -05:00
|
|
|
},
|
2023-01-09 06:20:10 -06:00
|
|
|
{ type: :bytes, property: :filesize, title: I18n.t("reports.top_uploads.labels.filesize") },
|
2020-10-29 11:49:09 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
report.data = []
|
|
|
|
|
|
|
|
sql = <<~SQL
|
|
|
|
SELECT
|
|
|
|
u.id as user_id,
|
|
|
|
u.username,
|
|
|
|
u.uploaded_avatar_id,
|
|
|
|
up.filesize,
|
|
|
|
up.original_filename,
|
|
|
|
up.extension,
|
|
|
|
up.url
|
|
|
|
FROM uploads up
|
|
|
|
JOIN users u
|
|
|
|
ON u.id = up.user_id
|
|
|
|
/*where*/
|
|
|
|
ORDER BY up.filesize DESC
|
|
|
|
LIMIT #{report.limit || 250}
|
|
|
|
SQL
|
|
|
|
|
|
|
|
builder = DB.build(sql)
|
2023-01-09 06:20:10 -06:00
|
|
|
builder.where(
|
|
|
|
"up.id > :seeded_id_threshold",
|
|
|
|
seeded_id_threshold: Upload::SEEDED_ID_THRESHOLD,
|
|
|
|
)
|
2020-10-29 11:49:09 -05:00
|
|
|
builder.where("up.created_at >= :start_date", start_date: report.start_date)
|
|
|
|
builder.where("up.created_at < :end_date", end_date: report.end_date)
|
|
|
|
|
|
|
|
if extension_filter
|
2023-01-20 12:52:49 -06:00
|
|
|
builder.where("up.extension = :extension", extension: extension_filter.sub(/\A\./, ""))
|
2020-10-29 11:49:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
builder.query.each do |row|
|
|
|
|
data = {}
|
|
|
|
data[:author_id] = row.user_id
|
|
|
|
data[:author_username] = row.username
|
|
|
|
data[:author_avatar_template] = User.avatar_template(row.username, row.uploaded_avatar_id)
|
|
|
|
data[:filesize] = row.filesize
|
|
|
|
data[:extension] = row.extension
|
|
|
|
data[:file_url] = Discourse.store.cdn_url(row.url)
|
|
|
|
data[:file_name] = row.original_filename.truncate(25)
|
|
|
|
report.data << data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|