Files
P42_UI/app/thirdparty/AwsGoodies/AwsFileUpload.js
T
2025-08-27 07:03:09 +00:00

56 lines
1.8 KiB
JavaScript

/**
* AWS File upload
* @extends FileUpload
* @author Nike
* @version 1.0
* @category MyEic
* @subcategory Libraries
*/
class AwsFileUpload extends FileUpload {
constructor(el, options) {
super(el, options);
this.filename = options.filename
this.pic = options.pic
this.pid = options.pid
}
mergeOptionsNode(target, source) {
for (let option in source) {
if(typeof source[option] === 'object' && !Array.isArray(source[option]) && (option in target) && !(target[option] instanceof(Element))) {
this.mergeOptionsNode(target[option], source[option]);
} else {
target[option] = source[option];
}
}
}
async onBeforeUploadOneStart(file) {
let response
let ext = file.name.substring(file.name.lastIndexOf('.')+1) //Get the extension from the user file name
if((!this.filename) || (this.filename != this.createFileName(this.pic, this.pid)+'.'+ext)) { // Carefull: extension could change !
this.filename = this.createFileName(this.pic, this.pid)+'.'+ext
}
let uri = this.options.getSignedUrl+this.filename
try {
response = await fetch(uri, {
method: this.options.getSignedMethod,
headers: { 'Accept': 'application/json',
'Content-Type': file.type,
},
credentials: 'include',
}).then( response => response.json())
let payload = response.payload
this.options.uploadUrl = payload.url // Signed url is here
this.options.uploadMethod = 'PUT'
} catch(debugMessage) {
console.warn('Error getting upload signed URL:',debugMessage)
return(false)
}
return(true)
}
createFileName(pic, pid) { return(pic+'_'+pid+'_'+crypto.randomUUID()) }
}