Clean up code using request promise
This commit is contained in:
@ -38,8 +38,10 @@
|
||||
"@types/archiver": "^1.3.4",
|
||||
"@types/express": "^4.0.35",
|
||||
"@types/request": "^2.0.3",
|
||||
"@types/request-promise": "^4.1.39",
|
||||
"archiver": "^1.3.0",
|
||||
"request": "^2.83.0",
|
||||
"request-promise": "^4.2.2",
|
||||
"xml": "^1.0.1"
|
||||
},
|
||||
"author": "Dolan Miu",
|
||||
|
@ -1,3 +1,3 @@
|
||||
// tslint:disable-next-line:no-empty-interface
|
||||
/* tslint:disable-next-line:no-empty-interface */
|
||||
export interface IPackOptions {
|
||||
}
|
||||
|
@ -1,40 +1,34 @@
|
||||
/* tslint:disable:object-literal-key-quotes */
|
||||
// This tslint disable is needed, or it simply won't work
|
||||
import * as fs from "fs";
|
||||
import * as request from "request";
|
||||
import * as request from "request-promise";
|
||||
|
||||
export interface IConvertOutput {
|
||||
data: string;
|
||||
}
|
||||
|
||||
export class PdfConvertWrapper {
|
||||
public convert(filePath: string): Promise<IConvertOutput> {
|
||||
const buffer = fs.readFileSync(filePath);
|
||||
|
||||
return new Promise<IConvertOutput>((resolve, reject) => {
|
||||
const r = request.post({
|
||||
url: "http://mirror1.convertonlinefree.com",
|
||||
encoding: null,
|
||||
headers: {
|
||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36",
|
||||
public convert(filePath: string): request.RequestPromise {
|
||||
return request.post({
|
||||
url: "http://mirror1.convertonlinefree.com",
|
||||
encoding: null,
|
||||
headers: {
|
||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36",
|
||||
},
|
||||
formData: {
|
||||
"__EVENTTARGET": "",
|
||||
"__EVENTARGUMENT": "",
|
||||
"__VIEWSTATE": "",
|
||||
"ctl00$MainContent$fu": {
|
||||
value: fs.readFileSync(filePath),
|
||||
options: {
|
||||
filename: "output.docx",
|
||||
contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
},
|
||||
},
|
||||
}, (err, response, body: IConvertOutput) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
resolve(body);
|
||||
});
|
||||
|
||||
const form = r.form();
|
||||
form.append("__EVENTTARGET", "");
|
||||
form.append("__EVENTARGUMENT", "");
|
||||
form.append("__VIEWSTATE", "");
|
||||
form.append("ctl00$MainContent$fu", buffer, {
|
||||
filename: "output.docx",
|
||||
contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
});
|
||||
|
||||
form.append("ctl00$MainContent$btnConvert", "Convert");
|
||||
form.append("ctl00$MainContent$fuZip", "");
|
||||
"ctl00$MainContent$btnConvert": "Convert",
|
||||
"ctl00$MainContent$fuZip": "",
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user