Skip to content

Instantly share code, notes, and snippets.

@Daltonic
Created August 21, 2024 13:48
Sia VidTv uploadToSiaService
private async uploadToSiaService(
file: FileUpload,
folder: string,
fileId: string
): Promise<AxiosResponse> {
// Step 1: Construct the upload URL
const url: string = `${this.siaUrl}/api/worker/objects/${folder}/${fileId}?bucket=${this.siaBucket}`
// Step 2: Set up the request configuration
let config = {
method: 'PUT',
maxBodyLength: Infinity,
url,
headers: {
Authorization: `Basic ${Buffer.from(`:${this.siaPassword}`).toString(
'base64'
)}`,
'Content-Type': file.mimetype,
},
data: file.data,
onUploadProgress: (progressEvent: AxiosProgressEvent) => {
// Step 3: Track upload progress
const { loaded, total } = progressEvent
const percentCompleted = Math.round((loaded / Number(total)) * 100)
console.log(`Upload progress: ${percentCompleted}%`)
},
}
try {
// Step 4: Send the upload request
return await axios.request(config)
} catch (e: any) {
// Step 5: Return the response
console.error(e.message)
throw new Error(e.message || 'Error uploading file')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment