Class: CommentsFilesController

Inherits:
ApplicationController show all
Includes:
AttachmentsHelper
Defined in:
app/controllers/comments_files_controller.rb

Instance Method Summary collapse

Methods included from AttachmentsHelper

#base64_attachments_to_files, #base64_with_filename_to_files

Methods included from AuthentificationHelper

#accessible_company_ids_from_tenant_and_user, #application_mode_from_hostname, #current_company_id_from_accessible_company_ids, #current_tenant_from_user, #current_user_and_locale_from_token

Instance Method Details

#check_scan_resultObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/comments_files_controller.rb', line 113

def check_scan_result
  analysis_id = params[:analysis_id]
  unless analysis_id.present?
    return render json: { error: 'No analysis ID provided' }, status: :bad_request
  end

  begin
    virustotal_service = VirustotalApiService.new
    result = virustotal_service.check_scan_result(analysis_id)

    render json: result, status: :ok
  rescue StandardError => e
    render json: {
      error: {
        message: e.message,
        backtrace: Rails.env.development? ? e.backtrace : nil
      }
    }, status: :internal_server_error
  end
end

#create_or_updateObject



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
# File 'app/controllers/comments_files_controller.rb', line 39

def create_or_update
  comment_file_id = params[:comment_file_id] || 0
  parent_comment_file_id = params[:parent_comment_file_id].to_i
  type = params[:type]
  company_id = current_company_id
  reference = params[:reference]
  reference_id = get_reference_id(reference: reference, reference_id: params[:reference_id])
  title = params[:title]
  description = params[:description]
  active = params[:active] || 1
  erp_user_id = params[:erp_user_id]
  created_by = params[:created_by] || 'Admin'
  redirect_to = params[:redirect_to]

  result = CommentsFiles.create_or_update_procedure(
    comment_file_id: comment_file_id,
    parent_comment_file_id: parent_comment_file_id,
    type: type,
    company_id: company_id,
    reference: reference,
    reference_id: reference_id,
    title: title,
    description: description,
    active: active,
    erp_user_id: erp_user_id,
    created_by: created_by
  )

  render json: {
    message: 'Comment file inserted/updated successfully',
    redirect_to: redirect_to
  }, status: :ok
end

#get_fileObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'app/controllers/comments_files_controller.rb', line 221

def get_file
  path = params[:path].to_s
  path_parts = path.gsub(%r{^/|/$}, '').split('/')

  path_parts.each do |part|
    if part.include?('..') || part.include?('~')
      return render json: { error: 'Invalid path' }, status: :bad_request
    end
  end

  filename = path_parts.last
  relative_path = File.join('storage', *path_parts)
  full_path = Rails.root.join(relative_path)

  unless full_path.to_s.start_with?(Rails.root.join('storage').to_s)
    return render json: { error: 'Invalid path' }, status: :bad_request
  end

  unless File.exist?(full_path)
    return render json: { error: 'File not found' }, status: :not_found
  end

  file_content = File.binread(full_path)
  extension = File.extname(filename).downcase.delete('.')

  mime_type = case extension
              when 'pdf'  then 'application/pdf'
              when 'png'  then 'image/png'
              when 'jpg', 'jpeg' then 'image/jpeg'
              when 'doc'  then 'application/msword'
              when 'docx' then 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
              when 'xls'  then 'application/vnd.ms-excel'
              when 'xlsx' then 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
              when 'ppt'  then 'application/vnd.ms-powerpoint'
              when 'pptx' then 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
              when 'txt'  then 'text/plain'
              when 'csv'  then 'text/csv'
              else 'application/octet-stream'
              end

  base64_encoded = Base64.strict_encode64(file_content)

  render json: {
    filename: filename,
    base64_file: "data:#{mime_type};base64,#{base64_encoded}"
  }, status: :ok
end

#indexObject



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
# File 'app/controllers/comments_files_controller.rb', line 12

def index
  company_id = current_company_id
  reference = params[:reference]
  reference_id = get_reference_id(reference: reference, reference_id: params[:reference_id])
  display_length = params[:limit] || 10
  search_value = params[:query] || ''
  display_start_from_page_no = params[:display_start_from_page_no] || 1
  order_by = params[:order_by] || 'comment_file_id'
  order_direction = params[:order_direction] || 'DESC'
  grouping = "#{order_by} #{order_direction}"

  result = CommentsFiles.find_by_page(
    company_id: company_id.to_i,
    reference: reference,
    reference_id: reference_id,
    display_length: display_length,
    search_value: search_value,
    display_start_from_page_no: display_start_from_page_no,
    grouping: grouping
  )

  render json: {
    data: result[:data],
    metadata: result[:metadata]
  }
end

#save_fileObject

this method handles saving files into storage and creating the file record in the database



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'app/controllers/comments_files_controller.rb', line 135

def save_file
  base64_files = params[:base64_files]
  comment_file_id = params[:comment_file_id] || 0
  parent_comment_file_id = params[:parent_comment_file_id] || 0
  company_id = current_company_id
  reference = params[:reference]
  reference_id = get_reference_id(reference: reference, reference_id: params[:reference_id])
  title = params[:title]
  description = params[:description] || ''
  active = params[:active] || 1
  erp_user_id = params[:erp_user_id]
  created_by = params[:created_by] || 'Admin'
  redirect_to = params[:redirect_to]

  # Create a parent comment file if it doesn't exist
  if comment_file_id == 0 && parent_comment_file_id == 0
    parent_comment_file_id = CommentsFiles.create_or_update_procedure(
      comment_file_id: 0,
      parent_comment_file_id: 0,
      type: '01',
      company_id: company_id,
      reference: reference,
      reference_id: reference_id,
      title: title,
      description: description,
      active: active,
      erp_user_id: erp_user_id,
      created_by: created_by
    )[:comment_file_id]
  end

  if base64_files.blank? || reference.blank? || reference_id.blank?
    return render json: { error: 'Missing parameters' }, status: :bad_request
  end

  if company_id.blank? && params[:reference].downcase != 'tenant'
    return render json: { error: 'Company ID is required' }, status: :bad_request
  end

  # Convert base64 + file_name objects into uploaded files
  files = base64_with_filename_to_files(base64_files)

  if files.blank?
    return render json: { error: 'No valid base64 file(s) provided' }, status: :bad_request
  end

  storage_path = build_storage_path(reference, reference_id, company_id)
  FileUtils.mkdir_p(storage_path) unless Dir.exist?(storage_path)

  saved_files = files.map do |file|
    filename = "#{SecureRandom.uuid}_#{file.original_filename}"
    file_path = File.join(storage_path, filename)

    File.binwrite(file_path, file.read)
    url = file_path.split('storage').last.to_s

    CommentsFiles.create_or_update_procedure( # for each file, create a record in the database
      comment_file_id: 0,
      parent_comment_file_id: parent_comment_file_id,
      type: '02',
      company_id: company_id,
      reference: reference,
      reference_id: reference_id,
      title: title,
      description: url,
      active: active,
      erp_user_id: erp_user_id,
      created_by: created_by
    )

    {
      message: 'File saved successfully',
      url: url,
      filename: file.original_filename
    }
  rescue StandardError => e
    { error: "Failed to save file: #{e.message}", filename: file.original_filename }
  end.compact

  render json: {
    results: saved_files,
    redirect_to: redirect_to,
    message: 'Files saved successfully'
  }, status: :ok
end

#scan_fileObject



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
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/comments_files_controller.rb', line 73

def scan_file
  base64_files = params[:base64_files]

  if base64_files.blank?
    return render json: { error: 'No file(s) provided' }, status: :bad_request
  end

  files = base64_attachments_to_files(base64_files)
  virustotal_service = VirustotalApiService.new

  results = files.map do |uploaded_file|
    sleep(1) # Rate limiting if needed
    if !allowed_file_format?(uploaded_file)
      { error: 'File format is not allowed', filename: uploaded_file.original_filename }
    elsif file_too_large?(uploaded_file)
      { error: 'File size exceeds the maximum limit of 10 MB', filename: uploaded_file.original_filename }
    else
      file_path = Rails.root.join('tmp', uploaded_file.original_filename)
      File.binwrite(file_path, uploaded_file.read)
      begin
        response = virustotal_service.scan_file(file_path)
        { result: response, filename: uploaded_file.original_filename }
      rescue StandardError => e
        { error: e.message, filename: uploaded_file.original_filename }
      ensure
        File.delete(file_path) if File.exist?(file_path)
      end
    end
  end

  render json: { results: results }
rescue StandardError => e
  render json: {
    error: {
      message: e.message,
      backtrace: Rails.env.development? ? e.backtrace : nil
    }
  }, status: :internal_server_error
end