2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
class UploadsController < ApplicationController
|
2019-10-22 02:05:40 -04:00
|
|
|
before_action :member_only
|
|
|
|
before_action :janitor_only, only: [:index, :show]
|
2024-12-08 22:28:31 -05:00
|
|
|
before_action :ensure_uploads_enabled, only: %i[new create]
|
2020-02-06 18:14:59 -05:00
|
|
|
respond_to :html, :json
|
2021-03-27 23:11:25 -04:00
|
|
|
content_security_policy only: [:new] do |p|
|
2022-04-20 15:21:11 -04:00
|
|
|
p.img_src :self, :data, :blob, "*"
|
|
|
|
p.media_src :self, :data, :blob, "*"
|
2020-02-06 18:14:59 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
def new
|
2019-08-07 23:26:28 -04:00
|
|
|
if CurrentUser.can_upload_with_reason == :REJ_UPLOAD_NEWBIE
|
|
|
|
return access_denied("You can not upload during your first week.")
|
|
|
|
end
|
2019-04-26 10:27:06 -04:00
|
|
|
@upload = Upload.new
|
2011-01-12 18:21:39 -05:00
|
|
|
respond_with(@upload)
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-17 19:20:44 -04:00
|
|
|
def index
|
2018-04-20 16:04:35 -04:00
|
|
|
@uploads = Upload.search(search_params).includes(:post, :uploader).paginate(params[:page], :limit => params[:limit])
|
2019-11-26 17:16:06 -05:00
|
|
|
respond_with(@uploads)
|
2010-03-17 19:20:44 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
def show
|
2010-03-12 19:27:54 -05:00
|
|
|
@upload = Upload.find(params[:id])
|
2013-02-21 14:03:22 -05:00
|
|
|
respond_with(@upload) do |format|
|
|
|
|
format.html do
|
|
|
|
if @upload.is_completed? && @upload.post_id
|
|
|
|
redirect_to(post_path(@upload.post_id))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2020-10-23 02:27:32 -04:00
|
|
|
Post.transaction do
|
|
|
|
@service = UploadService.new(upload_params)
|
|
|
|
@upload = @service.start!
|
|
|
|
end
|
2017-11-25 13:42:09 -05:00
|
|
|
|
2019-02-07 13:05:50 -05:00
|
|
|
if @upload.invalid?
|
|
|
|
flash[:notice] = @upload.errors.full_messages.join("; ")
|
2019-09-16 21:10:55 -04:00
|
|
|
return render json: {success: false, reason: 'invalid', message: @upload.errors.full_messages.join("; ")}, status: 412
|
2019-02-07 13:05:50 -05:00
|
|
|
end
|
2019-10-25 09:33:50 -04:00
|
|
|
if @service.warnings.any? && !@upload.is_errored? && !@upload.is_duplicate?
|
|
|
|
warnings = @service.warnings.join(".\n \n")
|
|
|
|
if warnings.length > 1500
|
|
|
|
Dmail.create_automated({
|
|
|
|
to_id: CurrentUser.id,
|
|
|
|
title: "Upload notices for post ##{@service.post.id}",
|
|
|
|
body: "While uploading post ##{@service.post.id} some notices were generated. Please review them below:\n\n#{warnings}"
|
|
|
|
})
|
2023-04-10 08:31:44 -04:00
|
|
|
flash[:notice] = "This upload created a LOT of notices. They have been dmailed to you. Please review them"
|
2019-10-25 09:33:50 -04:00
|
|
|
else
|
|
|
|
flash[:notice] = warnings
|
|
|
|
end
|
2017-11-25 13:42:09 -05:00
|
|
|
end
|
|
|
|
|
2019-07-12 22:31:11 -04:00
|
|
|
respond_to do |format|
|
2019-07-11 23:19:56 -04:00
|
|
|
format.json do
|
|
|
|
return render json: {success: false, reason: 'duplicate', location: post_path(@upload.duplicate_post_id), post_id: @upload.duplicate_post_id}, status: 412 if @upload.is_duplicate?
|
|
|
|
return render json: {success: false, reason: 'invalid', message: @upload.sanitized_status}, status: 412 if @upload.is_errored?
|
2019-07-12 22:31:11 -04:00
|
|
|
|
|
|
|
render json: {success: true, location: post_path(@upload.post_id), post_id: @upload.post_id}
|
2019-07-11 23:19:56 -04:00
|
|
|
end
|
|
|
|
end
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2018-04-02 13:51:26 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def upload_params
|
|
|
|
permitted_params = %i[
|
2024-11-10 14:58:15 -05:00
|
|
|
file direct_url source tag_string rating parent_id title description description transcript as_pending
|
2018-04-02 13:51:26 -04:00
|
|
|
]
|
|
|
|
|
2019-08-08 02:10:47 -04:00
|
|
|
permitted_params << :locked_tags if CurrentUser.is_admin?
|
|
|
|
permitted_params << :locked_rating if CurrentUser.is_privileged?
|
|
|
|
|
2020-06-11 20:55:37 -04:00
|
|
|
params.require(:upload).permit(permitted_params).merge(uploader_id: CurrentUser.id, uploader_ip_addr: CurrentUser.ip_addr)
|
2018-04-02 13:51:26 -04:00
|
|
|
end
|
2022-05-22 15:17:12 -04:00
|
|
|
|
|
|
|
def ensure_uploads_enabled
|
2024-12-08 22:28:31 -05:00
|
|
|
access_denied if Security::Lockdown.uploads_disabled? || CurrentUser.user.level < Security::Lockdown.uploads_min_level
|
2022-05-22 15:17:12 -04:00
|
|
|
end
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|