Hello,
Ultimately I am trying to fetch a pdf, clear the metadata, then save the pdf to a wix database; however I get an error when attempting to use wix’s Media Manager upload function.
In order to narrow down the issue, I have eliminated all of the pdf manipulation. Here is the code where I fetch a pdf, get the text, then try to upload the pdf. Any idea what I’m doing wrong? Thank you.
import {fetch} from 'wix-fetch';
import { mediaManager } from 'wix-media-backend';
var Buffer = require('buffer/').Buffer;
export async function uploadPDF(url, title, pathToSaveTo, fileName) {
let textPdf;
try {
let response = await fetch(url, {"method": "get"});
textPdf = await response.text();
}
catch (err) {
console.log(err.message);
throw err;
}
// try to save it to wix database
let newFileInfo;
try {
newFileInfo = await mediaManager.upload(
pathToSaveTo,
Buffer.from(textPdf),
fileName,
{
"mediaOptions": {
"mediaType": "document"
},
"metadataOptions": {
"isPrivate": false,
"isVisitorUpload": false
}
});
}
catch (err) {
throw err;
}
return newFileInfo;
}
“source.on is not a function” is the error I get on mediamanager.upload
I have tried it with or without the mimetype set to application/pdf. I should probably mention that I haven’t tried this on the live site. All of this is in “Preview” mode.
Sorry in advance if this is something silly I missed. Cheers.
-Max
I don’t know about that error message that you’re getting, but you are incorrectly calling fetch() with both an await and a .then() function. You should use one or the other - just take your pick. This will certainly cause issues.
Yup fair enough. This was a rush job to try to get this error figured out, but looking back at my code I do that a lot :s haha so thanks for bringing that to my attention
I should probably mention that I haven’t tried this on the live site. All of this is in “Preview” mode.
I am getting same error. Have you came to fix the issue?
Yes. It’s been more than a year since I’ve looked at the code, but here is the solution:
async function download ( url , filePath ) {
const proto = hh ;
return new Promise (( resolve , reject ) => {
const file = fs . createWriteStream ( filePath );
let fileInfo = null ;
**const** request = proto . **get** ( url , response => {
**if** ( response . statusCode !== 200 ) {
reject ( **new** Error ( `Failed to get ' ${ url } ' ( ${ response . statusCode } )` ));
**return** ;
}
fileInfo = {
mime : response . headers [ 'content-type' ],
size : parseInt ( response . headers [ 'content-length' ], 10 ),
};
response . pipe ( file );
});
// The destination stream is ended by the time it's called
file . on ( 'finish' , () => resolve ( fileInfo ));
request . on ( 'error' , err => {
fs . unlink ( filePath , () => reject ( err ));
});
file . on ( 'error' , err => {
fs . unlink ( filePath , () => reject ( err ));
});
request . end ();
});
}
// returns a file containing:
// unique file name —> newPdfInfo.fileName, // used to get url
// pdf —> newPdfInfo.fileUrl
export async function copyPdfFromOneLocationToAnother ( url , title , pathToSaveTo ) {
const tempPath = __dirname + “/” + title + “.pdf”
await download ( url , tempPath );
var buffer = fs . readFileSync ( tempPath );
const newFileInfo = await mediaManager . upload (
pathToSaveTo ,
buffer ,
title + “.pdf” ,
{
“mediaOptions” : {
“mimeType” : “application/pdf” ,
“mediaType” : “document”
},
“metadataOptions” : {
“isPrivate” : false ,
“isVisitorUpload” : false
}
});
**if** ( newFileInfo ) {
**return** newFileInfo ;
}
**else** {
**throw** { name : "SBError" , message : "Error trying to copy pdf to different location." };
}
}
@maxbjork
I figured it out with importFile API as I had only images to work with. Many thanks for sharing the solution. I really appreciate it.
I believe it will be very useful to the community.
Hey Max,
Thanks for your sample code. What exactly is proto in your code? Wix-fetch doesn’t have a pipe or blob method