feat: Add upload under account scope (#7914)

This commit is contained in:
Shivam Mishra
2023-09-19 09:51:54 +05:30
committed by GitHub
parent 2429daa45c
commit 53d530b815
8 changed files with 28 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
class Api::V1::UploadController < Api::BaseController
class Api::V1::Accounts::UploadController < Api::V1::Accounts::BaseController
def create
file_blob = ActiveStorage::Blob.create_and_upload!(
key: nil,

View File

@@ -25,10 +25,10 @@ describe('#Upload Helpers', () => {
axios.post.mockResolvedValueOnce(mockResponse);
const result = await uploadFile(mockFile);
const result = await uploadFile(mockFile, '1602');
expect(axios.post).toHaveBeenCalledWith(
'/api/v1/upload',
'/api/v1/accounts/1602/upload',
expect.any(FormData),
{ headers: { 'Content-Type': 'multipart/form-data' } }
);

View File

@@ -21,17 +21,25 @@ const HEADERS = {
* @param {File} file - The file to be uploaded. It should be a File object (typically coming from a file input element).
* @returns {Promise} A promise that resolves with the server's response when the upload is successful, or rejects if there's an error.
*/
export async function uploadFile(file) {
export async function uploadFile(file, accountId) {
// Create a new FormData instance.
let formData = new FormData();
if (!accountId) {
accountId = window.location.pathname.split('/')[3];
}
// Append the file to the FormData instance under the key 'attachment'.
formData.append('attachment', file);
// Use axios to send a POST request to the upload endpoint.
const { data } = await axios.post(`/api/${API_VERSION}/upload`, formData, {
headers: HEADERS,
});
const { data } = await axios.post(
`/api/${API_VERSION}/accounts/${accountId}/upload`,
formData,
{
headers: HEADERS,
}
);
return {
fileUrl: data.file_url,

View File

@@ -138,8 +138,9 @@ class Rack::Attack
end
## Prevent Abuse of attachment upload APIs ##
throttle('/api/v1/upload', limit: 60, period: 1.hour) do |req|
req.ip if req.path_without_extentions == '/api/v1/upload' && req.post?
throttle('/api/v1/accounts/:account_id/upload', limit: 60, period: 1.hour) do |req|
match_data = %r{/api/v1/accounts/(?<account_id>\d+)/upload}.match(req.path)
match_data[:account_id] if match_data.present?
end
## ----------------------------------------------- ##

View File

@@ -217,13 +217,13 @@ Rails.application.routes.draw do
post :reorder, on: :collection
end
end
resources :upload, only: [:create]
end
end
# end of account scoped api routes
# ----------------------------------
resources :upload, only: [:create]
namespace :integrations do
resources :webhooks, only: [:create]
end

View File

@@ -128,7 +128,7 @@ RSpec.describe 'Api::V1::Accounts::AutomationRulesController', type: :request do
expect(account.automation_rules.count).to eq(0)
post '/api/v1/upload',
post "/api/v1/accounts/#{account.id}/upload/",
headers: administrator.create_new_auth_token,
params: { attachment: file }
@@ -163,13 +163,13 @@ RSpec.describe 'Api::V1::Accounts::AutomationRulesController', type: :request do
file_1 = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
file_2 = fixture_file_upload(Rails.root.join('spec/assets/sample.png'), 'image/png')
post '/api/v1/upload',
post "/api/v1/accounts/#{account.id}/upload/",
headers: administrator.create_new_auth_token,
params: { attachment: file_1 }
blob_1 = response.parsed_body
post '/api/v1/upload',
post "/api/v1/accounts/#{account.id}/upload/",
headers: administrator.create_new_auth_token,
params: { attachment: file_2 }

View File

@@ -129,7 +129,7 @@ RSpec.describe 'Api::V1::Accounts::MacrosController', type: :request do
it 'Saves file in the macros actions to send an attachments' do
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
post '/api/v1/upload',
post "/api/v1/accounts/#{account.id}/upload/",
headers: administrator.create_new_auth_token,
params: { attachment: file }

View File

@@ -1,14 +1,14 @@
require 'rails_helper'
RSpec.describe 'Api::V1::UploadController', type: :request do
describe 'POST /api/v1/upload/' do
RSpec.describe 'Api::V1::Accounts::UploadController', type: :request do
describe 'POST /api/v1/account/1/upload/' do
let(:account) { create(:account) }
let(:user) { create(:user, account: account) }
it 'uploads the image when authorized' do
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
post '/api/v1/upload/',
post "/api/v1/accounts/#{account.id}/upload/",
headers: user.create_new_auth_token,
params: { attachment: file }
@@ -25,7 +25,7 @@ RSpec.describe 'Api::V1::UploadController', type: :request do
it 'does not upload when un-authorized' do
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
post '/api/v1/upload/',
post "/api/v1/accounts/#{account.id}/upload/",
headers: {},
params: { attachment: file }