Focusing on managing products, variants, and collections through the API.
Hi all, I am trying to upload the 3d models using the stagedUploadscreate and fileCreate GraphQL mutaitons
but after downloading the file on the local server it shows that the usdz file has incorrect format. Any leads regarding this would be very helpful !
const filePath = "path/to/model/centellaBottle.usdz";
const fileData = fs.readFileSync(filePath, "base64");
const filename = filePath.split("/").pop();
const fileSize = fs.statSync(filePath).size.toString();
const input = [];
input.push({
filename,
mimeType: "model/vnd.usdz+zip" || "model/gltf-binary",
resource: "FILE",
fileSize,
httpMethod: "POST",
});
const fileInput = {
alt: "3d models",
contentType: "FILE",
originalSource: "",
};
const stagedUploadsResponse = await admin!.graphql(
`#graphql
mutation StagedUploadsCreate($input: [StagedUploadInput!]!) {
stagedUploadsCreate(input: $input) {
stagedTargets {
url
resourceUrl
parameters {
name
value
}
}
}
}`,
{
variables: {
input: input,
},
},
);
const stagedUploadsData = await stagedUploadsResponse.json();
console.log(stagedUploadsData);
const target = stagedUploadsData.data.stagedUploadsCreate.stagedTargets[0];
const { resourceUrl, parameters, url } = target;
fileInput.originalSource = target.resourceUrl;
const formData = new FormData();
for (var param of target.parameters) {
formData.append(param.name, param.value);
}
formData.append("file", fileData);
await axios.post(target.url, formData, {
headers: {
"Content-Length": fileSize + 5000,
"Content-Type": "multipart/form-data",
},
});
console.log(resourceUrl, "resourceUrl", parameters, "parameters");
const fileCreateResponse = await admin!.graphql(
`#graphql
mutation fileCreate($files: [FileCreateInput!]!) {
fileCreate(files: $files) {
files {
alt
createdAt
}
}
}`,
{
variables: {
files: fileInput,
},
},
);
console.log(fileCreateResponse, "fileCreateResponse");
const productData = await fileCreateResponse.json();
return json({
data: productData.data,
});
}