/uploadToSiaService.ts Secret
Created
August 21, 2024 13:48
Sia VidTv uploadToSiaService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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