bb-plane-fork/packages/services/src/file/file-upload.service.ts
Prateek Shourya ade4d290f5
[WEB-3066] refactor: replace Space Services with Services Package (#6353)
* [WEB-3066] refactor: replace Space Services with Services Package

* chore: minor improvements

* fix: type

---------

Co-authored-by: sriram veeraghanta <veeraghanta.sriram@gmail.com>
2025-01-11 15:16:11 +05:30

52 lines
No EOL
1.2 KiB
TypeScript

import axios from "axios";
// api service
import { APIService } from "../api.service";
/**
* Service class for handling file upload operations
* Handles file uploads
* @extends {APIService}
*/
export class FileUploadService extends APIService {
private cancelSource: any;
constructor() {
super("");
}
/**
* Uploads a file to the specified signed URL
* @param {string} url - The URL to upload the file to
* @param {FormData} data - The form data to upload
* @returns {Promise<void>} Promise resolving to void
* @throws {Error} If the request fails
*/
async uploadFile(
url: string,
data: FormData,
): Promise<void> {
this.cancelSource = axios.CancelToken.source();
return this.post(url, data, {
headers: {
"Content-Type": "multipart/form-data",
},
cancelToken: this.cancelSource.token,
withCredentials: false,
})
.then((response) => response?.data)
.catch((error) => {
if (axios.isCancel(error)) {
console.log(error.message);
} else {
throw error?.response?.data;
}
});
}
/**
* Cancels the upload
*/
cancelUpload() {
this.cancelSource.cancel("Upload canceled");
}
}