Improve cloudinary media upload experience.
This commit is contained in:
@@ -13,13 +13,13 @@ var cleanAxios = axios.create();
|
||||
cleanAxios.interceptors.request.eject(axiosAuthInterceptorId);
|
||||
|
||||
export const handleUpload = async (ev, context) => {
|
||||
const { filename, mediaId, onError, onSuccess, onProgress } = ev;
|
||||
const { mediaId, onError, onSuccess, onProgress } = ev;
|
||||
const { bodyshop, jobId } = context;
|
||||
|
||||
const imageData = await MediaLibrary.getAssetInfoAsync(mediaId);
|
||||
const newFile = await (await fetch(imageData.localUri)).blob();
|
||||
const newFile = await (await fetch(imageData.uri)).blob();
|
||||
let extension = imageData.localUri.split(".").pop();
|
||||
let key = `${bodyshop.id}/${jobId}/${(filename || newFile.data.name).replace(
|
||||
let key = `${bodyshop.id}/${jobId}/${newFile.data.name.replace(
|
||||
/\.[^/.]+$/,
|
||||
""
|
||||
)}-${new Date().getTime()}`;
|
||||
@@ -29,8 +29,8 @@ export const handleUpload = async (ev, context) => {
|
||||
mediaId,
|
||||
imageData,
|
||||
extension,
|
||||
newFile.type,
|
||||
newFile,
|
||||
newFile.type, //Filetype
|
||||
newFile, //File
|
||||
onError,
|
||||
onSuccess,
|
||||
onProgress,
|
||||
@@ -51,30 +51,21 @@ export const uploadToCloudinary = async (
|
||||
onProgress,
|
||||
context
|
||||
) => {
|
||||
const { bodyshop, jobId, billId, uploaded_by, callback, tagsArray, photo } =
|
||||
context;
|
||||
const { bodyshop, jobId, uploaded_by } = context;
|
||||
|
||||
//Set variables for getting the signed URL.
|
||||
let timestamp = Math.floor(Date.now() / 1000);
|
||||
let public_id = key;
|
||||
let tags = `${bodyshop.textid},${
|
||||
tagsArray ? tagsArray.map((tag) => `${tag},`) : ""
|
||||
}`;
|
||||
// let eager = process.env.REACT_APP_CLOUDINARY_THUMB_TRANSFORMATIONS;
|
||||
|
||||
const upload_preset = fileType.startsWith("video")
|
||||
? "incoming_upload_video"
|
||||
: "incoming_upload";
|
||||
|
||||
//Get the signed url.
|
||||
|
||||
let signedURLResponse;
|
||||
try {
|
||||
signedURLResponse = await axios.post(`${env.API_URL}/media/sign`, {
|
||||
public_id: public_id,
|
||||
tags: tags,
|
||||
timestamp: timestamp,
|
||||
|
||||
upload_preset: upload_preset,
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -85,7 +76,6 @@ export const uploadToCloudinary = async (
|
||||
if (signedURLResponse.status !== 200) {
|
||||
console.log("Error Getting Signed URL", signedURLResponse.statusText);
|
||||
if (onError) onError(signedURLResponse.statusText);
|
||||
|
||||
return { success: false, error: signedURLResponse.statusText };
|
||||
}
|
||||
|
||||
@@ -112,7 +102,6 @@ export const uploadToCloudinary = async (
|
||||
formData.append("upload_preset", upload_preset);
|
||||
formData.append("api_key", env.REACT_APP_CLOUDINARY_API_KEY);
|
||||
formData.append("public_id", public_id);
|
||||
formData.append("tags", tags);
|
||||
formData.append("timestamp", timestamp);
|
||||
formData.append("signature", signature);
|
||||
|
||||
@@ -124,13 +113,11 @@ export const uploadToCloudinary = async (
|
||||
fileType
|
||||
)}/upload`,
|
||||
formData,
|
||||
{
|
||||
...options,
|
||||
}
|
||||
options
|
||||
);
|
||||
// console.log("Cloudinary Upload Response", cloudinaryUploadResponse.data);
|
||||
} catch (error) {
|
||||
console.log("CLOUDINARY error", error.response, cloudinaryUploadResponse);
|
||||
if (onError) onError(error.message);
|
||||
return { success: false, error: error };
|
||||
}
|
||||
|
||||
@@ -147,35 +134,10 @@ export const uploadToCloudinary = async (
|
||||
//Insert the document with the matching key.
|
||||
const documentInsert = await client.mutate({
|
||||
mutation: INSERT_NEW_DOCUMENT,
|
||||
|
||||
update: (cache, { data }) => {
|
||||
cache.modify({
|
||||
fields: {
|
||||
documents: (existingDocs = []) => {
|
||||
const newDocRef = cache.writeFragment({
|
||||
data: data.insert_documents.returning[0],
|
||||
fragment: gql`
|
||||
fragment newDoc on documents {
|
||||
id
|
||||
name
|
||||
key
|
||||
type
|
||||
takenat
|
||||
extension
|
||||
jobid
|
||||
}
|
||||
`,
|
||||
});
|
||||
return [...existingDocs, newDocRef];
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
variables: {
|
||||
docInput: [
|
||||
{
|
||||
...(jobId ? { jobid: jobId } : {}),
|
||||
...(billId ? { billid: billId } : {}),
|
||||
uploaded_by: uploaded_by,
|
||||
key: key,
|
||||
type: fileType,
|
||||
@@ -197,19 +159,8 @@ export const uploadToCloudinary = async (
|
||||
status: "done",
|
||||
key: documentInsert.data.insert_documents.returning[0].key,
|
||||
});
|
||||
// notification["success"]({
|
||||
// message: i18n.t("documents.successes.insert"),
|
||||
// });
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
} else {
|
||||
if (onError) onError(JSON.stringify(documentInsert.errors));
|
||||
// notification["error"]({
|
||||
// message: i18n.t("documents.errors.insert", {
|
||||
// message: JSON.stringify(JSON.stringify(documentInsert.errors)),
|
||||
// }),
|
||||
// });
|
||||
return {
|
||||
success: false,
|
||||
error: JSON.stringify(documentInsert.errors),
|
||||
@@ -234,6 +185,7 @@ export function formatBytes(a, b = 2) {
|
||||
if (0 === a || !a) return "0 Bytes";
|
||||
const c = 0 > b ? 0 : b,
|
||||
d = Math.floor(Math.log(a) / Math.log(1024));
|
||||
|
||||
return (
|
||||
parseFloat((a / Math.pow(1024, d)).toFixed(c)) +
|
||||
" " +
|
||||
|
||||
@@ -52,40 +52,6 @@ export const handleLocalUpload = async ({
|
||||
const formData = new FormData();
|
||||
|
||||
formData.append("jobid", jobid);
|
||||
// const imageData = await MediaLibrary.getAssetInfoAsync(mediaId);
|
||||
// const mimeType = mime.getType(imageData.uri);
|
||||
|
||||
// //let thumb;
|
||||
// let fileData = {
|
||||
// uri: null,
|
||||
// type: null,
|
||||
// name: null,
|
||||
// };
|
||||
// if (mimeType === "image/heic") {
|
||||
// try {
|
||||
// thumb = await ImageManipulator.manipulateAsync(imageData.uri, [], {
|
||||
// format: "jpeg",
|
||||
// base64: true,
|
||||
// compress: 0.75,
|
||||
// });
|
||||
// const name = imageData.filename.split(".");
|
||||
// name.pop();
|
||||
// fileData = {
|
||||
// uri: thumb.uri,
|
||||
// type: "image/jpeg",
|
||||
// name: name.join("") + ".jpeg",
|
||||
// };
|
||||
// } catch (error) {
|
||||
// console.log(error);
|
||||
// onError && onError(error.message);
|
||||
// }
|
||||
// } else {
|
||||
// fileData = {
|
||||
// uri: imageData.localUri || imageData.uri,
|
||||
// type: mimeType,
|
||||
// name: filename,
|
||||
// };
|
||||
//}
|
||||
|
||||
const filesList = [];
|
||||
for (const file of files) {
|
||||
|
||||
Reference in New Issue
Block a user