Hi everyone,
having some technical issue plugin meteor/react and aws-sdk for uploading videos. It seems my file is getting uploaded but somehow is corrupt or can not be read. I have the following code on client:
mport React from "react";
import { Meteor } from "meteor/meteor";
import PropTypes from "prop-types";
import { types } from "../../../utils/constants/types";
const FileUpload = ({ fileType, type, typeId, subtype, setFileName }) => {
const handleUpload = event => {
event.preventDefault();
const file = event.target.files[0];
const fileExtension = file.type;
var reader = new FileReader();
reader.onload = function () {
Meteor.call(
"uploadFile",
fileExtension,
reader.result,
type,
typeId,
(err, result) => {
if (err) {
console.log(err);
} else {
setFileName(result);
}
}
);
};
reader.readAsDataURL(file);
};
return (
<div>
<input name="Uploader" onChange={handleUpload} type="file" />
</div>
);
};
and this method on server side:
Meteor.methods({
uploadFile: async function (fileType, data, type, typeId) {
let extension;
let contentType = fileType;
if (fileType.includes("jpeg") || fileType.includes("jpg")) {
extension = "jpg";
} else if (fileType.includes("png")) {
extension = "png";
} else if (fileType.includes("mp4")) {
extension = "mp4";
} else if (fileType.includes("audio/mpeg")) {
contentType = "video/mp4";
extension = "mp3";
} else if (fileType.includes("pdf")) {
extension = "pdf";
} else {
throw new Meteor.Error("format-error", "Only authorized format");
}
const random = Random.id();
const key = `random.${extension}`;
const buf =
extension !== "mp4"
? Buffer.from(data.replace(/^data:image\/\w+;base64,/, ""), "base64")
: data;
const config = {
Bucket: bucketName,
Key: key,
Body: buf,
ContentType: contentType,
ACL: "public-read",
};
if (extension !== "mp4") {
config.ContentEncoding = "base64";
}
const uploadResult = await s3.upload(config).promise();
return uploadResult.Location;
},
});
The code works just fine for mp3 and images but somehow I can not read any mp4 I am uploading with this method. First I treated the mp4 same way as other file but it didn’t work either. Not sure if it’s a contentType issue. I’d be open to alternative solution in react/meteor to upload to s3 if they are up to date and not too hard to implement.
Thanks in advance.