Module: PresignedUploadActions

Extended by:
ActiveSupport::Concern
Included in:
UploadsController, Www::UploadsController
Defined in:
app/concerns/presigned_upload_actions.rb

Overview

Concern for handling presigned URL uploads
Provides presigned_url and upload_complete actions for direct S3 uploads

Instance Method Summary collapse

Instance Method Details

#presigned_urlObject

Generate presigned URL for direct S3 upload



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
# File 'app/concerns/presigned_upload_actions.rb', line 9

def presigned_url
  # Only authorize if this is a CRM request (which has authorization)
  # WWW requests are public-facing and don't require authorization
  authorize!(:create, Upload) if respond_to?(:authorize!) && is_crm_request?

  file_name = params[:file_name]
  content_type = params[:content_type]
  file_size = params[:file_size]&.to_i
  category = params[:category] || 'photo'
  resource_type = params[:resource_type]
  resource_id = params[:resource_id]

  # Validate required parameters
  unless file_name.present? && content_type.present? && file_size.present?
    render json: { error: 'Missing required parameters: file_name, content_type, file_size' }, status: :bad_request
    return
  end

  # Validate file size (match Upload model validation)
  if file_size > 500.megabytes
    render json: { error: 'File size exceeds maximum allowed size' }, status: :unprocessable_entity
    return
  end

  begin
    presigned_data = PresignedUploadService.instance.generate_presigned_url(
      file_name: file_name,
      content_type: content_type,
      file_size: file_size,
      category: category,
      resource_type: resource_type,
      resource_id: resource_id
    )

    render json: {
      presigned_url: presigned_data[:presigned_url],
      key: presigned_data[:key],
      bucket: presigned_data[:bucket],
      region: presigned_data[:region]
    }
  rescue StandardError => e
    Rails.logger.error "Failed to generate presigned URL: #{e.message}"
    render json: { error: 'Failed to generate presigned URL' }, status: :internal_server_error
  end
end

#upload_completeObject

Handle upload completion after direct S3 upload



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
# File 'app/concerns/presigned_upload_actions.rb', line 56

def upload_complete
  # Only authorize if this is a CRM request (which has authorization)
  # WWW requests are public-facing and don't require authorization
  authorize!(:create, Upload) if respond_to?(:authorize!) && is_crm_request?

  s3_key = params[:key]
  file_name = params[:file_name]
  content_type = params[:content_type]
  file_size = params[:file_size]&.to_i
  category = params[:category] || 'photo'
  resource_type = params[:resource_type]
  resource_id = params[:resource_id]

  unless s3_key.present? && file_name.present? && content_type.present? && file_size.present?
    render json: { error: 'Missing required parameters' }, status: :bad_request
    return
  end

  begin
    upload = PresignedUploadService.instance.create_upload_from_s3_key(
      s3_key: s3_key,
      file_name: file_name,
      content_type: content_type,
      file_size: file_size,
      category: category,
      resource_type: resource_type,
      resource_id: resource_id
    )

    if upload
      render json: {
        message: 'Upload completed successfully.',
        files_list: [upload.id].to_json,
        upload_ids: [upload.id]
      }
    else
      render json: { error: 'Failed to create upload record' }, status: :unprocessable_entity
    end
  rescue StandardError => e
    Rails.logger.error "Failed to complete upload: #{e.message}"
    render json: { error: 'Failed to complete upload' }, status: :internal_server_error
  end
end