Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added post requests #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ app.get('/pdf', (req, res) => {
})

app.post('/pdf', (req, res) => {
let { url, dpi } = req.body;
let { url, dpi, method, body } = req.body;

if (isValidURL(url) && dpi) {
let id = Date.now().toString(36);
controller.addDownload({ url, dpi }, id)
controller.addDownload({ url, dpi, method, body }, id)
.then((job) => {
let { id, data, status } = job;
res.json({ id, data, status });
Expand Down
8 changes: 4 additions & 4 deletions sample.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GSX_OPTIMIZE_COMMAND=ghostscript
DB_HOST='your redis hostname'
DB_PORT='port'
DB_PASS='password'
GSX_OPTIMIZE_COMMAND=./ghostscript
DB_HOST='redis-3.0'
DB_PORT='6379'
DB_PASS='sup3r_s3cr3t_p4ssw0rd_aa3c2cde'
WS_HOST='ws://localhost:5000'
HOSTNAME='http://localhost:5000'
ADOBE_ID='adobe view sdk client id'
65 changes: 45 additions & 20 deletions utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,52 @@ const FileType = require('file-type');
const download = (job) => {
let url = job.data.url;
let dest = path.join(__dirname, `public/${job.id}.pdf`);
return new Promise((resolve, reject) => {
fetch(url).then(async (response) => {
if (response.ok) {
let file = fs.createWriteStream(dest);
let totalSize = response.headers.get('content-length');
let timer = setInterval(() => {
job.reportProgress({ done: parseInt(file.bytesWritten), total: parseInt(totalSize) });
}, 700);
console.log('\x1b[46m\x1b[30m%s\x1b[0m', `Download started: ${file.path}`);
const fileTypeStream = await FileType.stream(response.body);
console.log(fileTypeStream.fileType);
await streamPipeline(fileTypeStream, file);
clearInterval(timer);
resolve(job.data);
} else {
reject(response.statusText);
}
}).catch((err) => {
reject(err.message);
if(job.data.method === "GET"){
return new Promise((resolve, reject) => {
fetch(url, { method: "GET" }).then(async (response) => {
if (response.ok) {
let file = fs.createWriteStream(dest);
let totalSize = response.headers.get('content-length');
let timer = setInterval(() => {
job.reportProgress({ done: parseInt(file.bytesWritten), total: parseInt(totalSize) });
}, 700);
console.log('\x1b[46m\x1b[30m%s\x1b[0m', `Download started: ${file.path}`);
const fileTypeStream = await FileType.stream(response.body);
console.log(fileTypeStream.fileType);
await streamPipeline(fileTypeStream, file);
clearInterval(timer);
resolve(job.data);
} else {
reject(response.statusText);
}
}).catch((err) => {
reject(err.message);
})
})
})
}
else if(job.data.method === "POST"){
return new Promise((resolve, reject) => {
fetch(url,{ method: "POST", body: job.data.body }).then(async (response) => {
if (response.ok) {
let file = fs.createWriteStream(dest);
let totalSize = response.headers.get('content-length');
let timer = setInterval(() => {
job.reportProgress({ done: parseInt(file.bytesWritten), total: parseInt(totalSize) });
}, 700);
console.log('\x1b[46m\x1b[30m%s\x1b[0m', `Download started: ${file.path}`);
const fileTypeStream = await FileType.stream(response.body);
console.log(fileTypeStream.fileType);
await streamPipeline(fileTypeStream, file);
clearInterval(timer);
resolve(job.data);
} else {
reject(response.statusText);
}
}).catch((err) => {
reject(err.message);
})
})
}
};

const compress = async (job) => {
Expand Down