FixUploading Issues on newpost

This commit is contained in:
Adolfo Reyna
2023-08-24 22:29:59 -04:00
parent e539e05c6e
commit bdbf2f63c1
5 changed files with 56 additions and 217 deletions

View File

@@ -21,53 +21,58 @@ let NewPost = ({ profileid, newPostCB }) => {
mediaTypes: ImagePicker.MediaTypeOptions.Images,
//allowsEditing: true,
//aspect: [4, 3],
//quality: 1,
quality: 0.2,
allowsMultipleSelection: true,
});
if (!result.cancelled) {
if (!result.canceled) {
setPhoto(result);
let newPhotoURL = await handleUploadPhoto(result);
let newExtraContent = ["@image:" + newPhotoURL].concat(extraContent);
let newPhotoURLs = await handleUploadPhoto(result);
let newExtraContent = [extraContent]
newPhotoURLs.forEach((newPhotoURL) => {
newExtraContent = ["@image:" + newPhotoURL].concat(newExtraContent);
});
setExtraContent(newExtraContent);
console.log(newExtraContent.join(" "));
setPhoto(null);
}
};
const handleUploadPhoto = async (photo) => {
if (!photo) return;
const uri =
Platform.OS === "android"
? photo.uri
: photo.uri.replace("file://", "");
const filename = photo.uri.split("/").pop();
const match = /\.(\w+)$/.exec(filename);
const ext = match?.[1];
const type = match ? `image/${match[1]}` : `image`;
const formData = new FormData();
formData.append("banner", {
uri,
name: `image.${ext}`,
type,
});
try {
let uploadedFile = await fetch("https://social.emmint.com/upload.php", {
method: "POST",
body: formData,
headers: { "Content-Type": "multipart/form-data" }
})
.then((res) => res.json())
.then((data) => {
console.log(data);
return data.fileName;
const handleUploadPhoto = async (results) => {
if (!results) return;
let allUploads = [];
results.assets.forEach(photo => {
const uri =
Platform.OS === "android"
? photo.uri
: photo.uri.replace("file://", "");
const filename = photo.uri.split("/").pop();
const match = /\.(\w+)$/.exec(filename);
const ext = match?.[1];
const type = match ? `image/${match[1]}` : `image`;
const formData = new FormData();
formData.append("banner", {
uri,
name: `image.${ext}`,
type,
});
try {
allUploads.push(fetch("https://social.emmint.com/upload.php", {
method: "POST",
body: formData,
headers: { "Content-Type": "multipart/form-data" }
})
.catch((err) => console.error(err));
return uploadedFile;
} catch (err) {
console.log(err);
alert("Something went wrong");
}
.then((res) => res.json())
.then((data) => {
return data.fileName;
})
.catch((err) => console.error(err)));
} catch (err) {
console.log(err);
alert("Something went wrong uploading the photo.");
}
});
uploadedFiles = Promise.all(allUploads);
return uploadedFiles;
};
const handleNewPostButton = async () => {