mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-04-24 16:00:53 +08:00
fix(blocks-antd): Fix S3UploadButton not uploading files.
This commit is contained in:
parent
45aaa18545
commit
7005a8f547
@ -33,48 +33,51 @@ const getCustomRequest = ({ methods, setS3Parameters }) => async ({
|
||||
onProgress,
|
||||
onSuccess,
|
||||
}) => {
|
||||
const { name, size, type, uid } = file;
|
||||
try {
|
||||
const { name, size, type, uid } = file;
|
||||
|
||||
const s3PostPolicyResponse = await methods.triggerEvent({
|
||||
name: '__getS3PostPolicy',
|
||||
event: { filename: name, size, type, uid },
|
||||
});
|
||||
const s3PostPolicyResponse = await methods.triggerEvent({
|
||||
name: '__getS3PostPolicy',
|
||||
event: { filename: name, size, type, uid },
|
||||
});
|
||||
|
||||
if (s3PostPolicyResponse[0].error) {
|
||||
onError(s3PostPolicyResponse[0].error);
|
||||
return;
|
||||
}
|
||||
|
||||
const { url, fields } = s3PostPolicyResponse[0].response;
|
||||
const { bucket, key } = fields;
|
||||
|
||||
setS3Parameters((prevState) => {
|
||||
const ret = { ...prevState };
|
||||
ret[uid] = { bucket, key };
|
||||
return ret;
|
||||
});
|
||||
|
||||
// Set 20 % progress on policy is acquired else user waits to long before progress is reported
|
||||
onProgress({ percent: 20 });
|
||||
|
||||
// Create FormData with all required fields in S3 policy
|
||||
const formData = new FormData();
|
||||
Object.keys(fields).forEach((field) => {
|
||||
formData.append(field, fields[field]);
|
||||
});
|
||||
// file needs to be the last field in the form
|
||||
formData.append('file', file);
|
||||
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.upload.onprogress = (event) => {
|
||||
if (event.lengthComputable) {
|
||||
onProgress({ percent: (event.loaded / event.total) * 80 + 20 });
|
||||
if (s3PostPolicyResponse.success !== true) {
|
||||
throw new Error('S3 post policy request error.');
|
||||
}
|
||||
};
|
||||
xhr.addEventListener('error', onError);
|
||||
xhr.addEventListener('load', onSuccess);
|
||||
xhr.open('post', url);
|
||||
xhr.send(formData);
|
||||
|
||||
const { url, fields } = s3PostPolicyResponse.responses[0].response[0];
|
||||
const { bucket, key } = fields;
|
||||
|
||||
setS3Parameters((prevState) => {
|
||||
const ret = { ...prevState };
|
||||
ret[uid] = { bucket, key };
|
||||
return ret;
|
||||
});
|
||||
|
||||
// Set 20 % progress on policy is acquired else user waits to long before progress is reported
|
||||
onProgress({ percent: 20 });
|
||||
|
||||
// Create FormData with all required fields in S3 policy
|
||||
const formData = new FormData();
|
||||
Object.keys(fields).forEach((field) => {
|
||||
formData.append(field, fields[field]);
|
||||
});
|
||||
// file needs to be the last field in the form
|
||||
formData.append('file', file);
|
||||
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.upload.onprogress = (event) => {
|
||||
if (event.lengthComputable) {
|
||||
onProgress({ percent: (event.loaded / event.total) * 80 + 20 });
|
||||
}
|
||||
};
|
||||
xhr.addEventListener('error', onError);
|
||||
xhr.addEventListener('load', onSuccess);
|
||||
xhr.open('post', url);
|
||||
xhr.send(formData);
|
||||
} catch (error) {
|
||||
onError(error);
|
||||
}
|
||||
};
|
||||
|
||||
const S3UploadButtonBlock = ({ blockId, events, methods, properties, value }) => {
|
||||
@ -83,7 +86,7 @@ const S3UploadButtonBlock = ({ blockId, events, methods, properties, value }) =>
|
||||
// so it cannot set the value directly. customRequest sets the parameters to s3Parameters state,
|
||||
// and then onChange updates the block value.
|
||||
const [s3Parameters, setS3Parameters] = useState(value);
|
||||
let customRequest;
|
||||
const customRequest = getCustomRequest({ methods, setS3Parameters });
|
||||
useEffect(() => {
|
||||
methods.setValue({ file: null, fileList: [] });
|
||||
methods.registerEvent({
|
||||
@ -92,11 +95,10 @@ const S3UploadButtonBlock = ({ blockId, events, methods, properties, value }) =>
|
||||
{
|
||||
id: `${blockId}__getS3PostPolicy`,
|
||||
type: 'Request',
|
||||
params: properties.s3PostPolicyRequestId,
|
||||
params: [properties.s3PostPolicyRequestId],
|
||||
},
|
||||
],
|
||||
});
|
||||
customRequest = getCustomRequest({ methods, setS3Parameters });
|
||||
}, []);
|
||||
|
||||
const disabled = getDisabled({ properties, value });
|
||||
|
Loading…
x
Reference in New Issue
Block a user