Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing request timeout to infinite for file upload #58

Merged
merged 5 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion frontend/src/components/object/ObjectUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ const onUpload = async (event: any) => {
await Promise.allSettled(
event.files.map(async (file: File) => {
try {
await objectStore.createObject(file, bucketId);
// Infinite timeout for big files upload to avoid timeout error
await objectStore.createObject(file, bucketId, { timeout: 0 });
successfulFiles.value.push(file);
} catch (error) {
toast.add({ severity: 'error', summary: 'Error', detail: `Failed to upload file ${file.name}`, life: 3000 });
Expand Down
18 changes: 8 additions & 10 deletions frontend/src/services/interceptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ import axios from 'axios';

import { AuthService, ConfigService } from './index';

import type { AxiosInstance, InternalAxiosRequestConfig } from 'axios';
import type { AxiosInstance, AxiosRequestConfig, InternalAxiosRequestConfig } from 'axios';

/**
* @function comsAxios
* Returns an Axios instance for the COMS API
* @param {number} [timeout=10000] Number of milliseconds before timing out the request
* @param {AxiosRequestConfig} options Axios request config options
* @returns {AxiosInstance} An axios instance
*/
export function comsAxios(timeout: number = 10000): AxiosInstance {
const configService = new ConfigService();
const axiosOptions = {
timeout: timeout,
baseURL: configService.getConfig().coms.apiPath,
};

const instance = axios.create(axiosOptions);
export function comsAxios(options: AxiosRequestConfig = {}): AxiosInstance {
const instance = axios.create({
baseURL: new ConfigService().getConfig().coms.apiPath,
timeout: 10000,
...options
});

instance.interceptors.request.use(
async (cfg: InternalAxiosRequestConfig) => {
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/services/objectService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { comsAxios } from './interceptors';

import type { AxiosRequestConfig } from 'axios';
import type { GetMetadataOptions, GetObjectTaggingOptions, SearchObjectsOptions } from '@/types';

const PATH = '/object';
Expand All @@ -8,16 +9,19 @@ export default {
/**
* @function createObject
* Post an object
* @param {Any} object Object to be created
* @param {String} bucketId Bucket id containing the object
* @param {AxiosRequestConfig} axiosOptions Axios request config options
* @returns {Promise} An axios response
*/
createObject(object: any, bucketId?: string) {
createObject(object: any, bucketId?: string, axiosOptions?: AxiosRequestConfig) {
const config = {
headers: { 'Content-Type': 'multipart/form-data' },
params: { bucketId: bucketId },
};
const fd = new FormData();
fd.append('file', object);
return comsAxios().post(PATH, fd, config);
return comsAxios(axiosOptions).post(PATH, fd, config);
},

/**
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/store/objectStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { objectService } from '@/services';
import { useAppStore, usePermissionStore } from '@/store';
import { partition } from '@/utils/utils';

import type { AxiosRequestConfig } from 'axios';
import type { Ref } from 'vue';
import type { COMSObject, ObjectSearchPermissionsOptions } from '@/types';

Expand Down Expand Up @@ -34,10 +35,10 @@ export const useObjectStore = defineStore('object', () => {
};

// Actions
async function createObject(object: any, bucketId?: string) {
async function createObject(object: any, bucketId?: string, axiosOptions?: AxiosRequestConfig) {
try {
appStore.beginIndeterminateLoading();
await objectService.createObject(object, bucketId);
await objectService.createObject(object, bucketId, axiosOptions);
}
catch (error) {
toast.add({ severity: 'error', summary: 'Error creating object', detail: error, life: 3000 });
Expand Down