2021-01-26 11:44:34 +00:00
|
|
|
const fs = require("fs");
|
2020-09-09 12:25:00 +00:00
|
|
|
const superagent = require("superagent");
|
2021-01-26 11:44:34 +00:00
|
|
|
const tmp = require('tmp');
|
2020-09-09 12:25:00 +00:00
|
|
|
const { StatusCodes } = require("http-status-codes");
|
2020-09-30 14:20:55 +00:00
|
|
|
const { calculateElapsedTime, getResponseContent } = require("../utils");
|
2020-09-09 12:25:00 +00:00
|
|
|
|
|
|
|
// uploadCheck returns the result of uploading a sample file
|
|
|
|
async function uploadCheck(done) {
|
|
|
|
const time = process.hrtime();
|
2021-01-26 11:44:34 +00:00
|
|
|
const file = tmp.fileSync();
|
|
|
|
|
|
|
|
fs.writeSync(file.fd, Buffer.from(new Date())); // write current date to temp file
|
2020-09-09 12:25:00 +00:00
|
|
|
|
|
|
|
superagent
|
2021-01-26 11:18:40 +00:00
|
|
|
.post(`${process.env.PORTAL_URL}/skynet/skyfile`)
|
2021-01-26 11:44:34 +00:00
|
|
|
.attach("file", file.name, file.name)
|
2020-09-09 12:25:00 +00:00
|
|
|
.end((error, response) => {
|
2021-01-26 11:44:34 +00:00
|
|
|
file.removeCallback();
|
|
|
|
|
2020-09-09 12:25:00 +00:00
|
|
|
const statusCode = (response && response.statusCode) || (error && error.statusCode) || null;
|
|
|
|
|
|
|
|
done({
|
|
|
|
name: "upload_file",
|
|
|
|
up: statusCode === StatusCodes.OK,
|
|
|
|
statusCode,
|
2020-09-30 14:20:55 +00:00
|
|
|
errorResponseContent: getResponseContent(error?.response),
|
2020-09-09 12:53:05 +00:00
|
|
|
time: calculateElapsedTime(time),
|
2020-09-09 12:25:00 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// downloadCheck returns the result of downloading the hard coded link
|
|
|
|
async function downloadCheck(done) {
|
|
|
|
const time = process.hrtime();
|
|
|
|
const skylink = "AACogzrAimYPG42tDOKhS3lXZD8YvlF8Q8R17afe95iV2Q";
|
2020-09-30 14:20:55 +00:00
|
|
|
let statusCode, errorResponseContent;
|
2020-09-09 12:25:00 +00:00
|
|
|
|
|
|
|
try {
|
2021-01-26 11:18:40 +00:00
|
|
|
const response = await superagent.get(`${process.env.PORTAL_URL}/${skylink}?nocache=true`);
|
2020-09-09 12:25:00 +00:00
|
|
|
|
|
|
|
statusCode = response.statusCode;
|
|
|
|
} catch (error) {
|
|
|
|
statusCode = error.statusCode || error.status;
|
2020-09-30 14:20:55 +00:00
|
|
|
errorResponseContent = getResponseContent(error.response);
|
2020-09-09 12:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done({
|
|
|
|
name: "download_file",
|
|
|
|
up: statusCode === StatusCodes.OK,
|
|
|
|
statusCode,
|
2020-09-30 14:20:55 +00:00
|
|
|
errorResponseContent,
|
2020-09-09 12:53:05 +00:00
|
|
|
time: calculateElapsedTime(time),
|
2020-09-09 12:25:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:18:40 +00:00
|
|
|
module.exports = [uploadCheck, downloadCheck];
|