2022-06-19 00:25:21 +00:00
|
|
|
/* eslint-disable @typescript-eslint/restrict-plus-operands */
|
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
|
|
|
import crypto from "crypto";
|
|
|
|
import {expect} from "chai";
|
|
|
|
import util from "../util";
|
|
|
|
import Config from "../../server/config";
|
|
|
|
import storage from "../../server/plugins/storage";
|
|
|
|
import link from "../../server/plugins/irc-events/link";
|
|
|
|
import {Request, Response} from "express";
|
2017-07-06 15:33:09 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
describe("Image storage", function () {
|
2019-11-27 18:25:29 +00:00
|
|
|
// Increase timeout due to unpredictable I/O on CI services
|
2020-02-09 12:21:45 +00:00
|
|
|
this.timeout(util.isRunningOnCI() ? 25000 : 5000);
|
|
|
|
this.slow(300);
|
2017-11-27 23:47:19 +00:00
|
|
|
|
2018-01-28 07:20:24 +00:00
|
|
|
const testImagePath = path.resolve(__dirname, "../../client/img/logo-grey-bg-120x120px.png");
|
2019-07-17 09:33:59 +00:00
|
|
|
const correctImageHash = crypto
|
|
|
|
.createHash("sha256")
|
|
|
|
.update(fs.readFileSync(testImagePath))
|
|
|
|
.digest("hex");
|
|
|
|
const correctImageURL = `storage/${correctImageHash.substring(
|
|
|
|
0,
|
|
|
|
2
|
|
|
|
)}/${correctImageHash.substring(2, 4)}/${correctImageHash.substring(4)}.png`;
|
2017-07-06 15:33:09 +00:00
|
|
|
|
2018-01-28 07:20:24 +00:00
|
|
|
const testSvgPath = path.resolve(__dirname, "../../client/img/logo-grey-bg.svg");
|
2019-07-17 09:33:59 +00:00
|
|
|
const correctSvgHash = crypto
|
|
|
|
.createHash("sha256")
|
|
|
|
.update(fs.readFileSync(testSvgPath))
|
|
|
|
.digest("hex");
|
|
|
|
const correctSvgURL = `storage/${correctSvgHash.substring(0, 2)}/${correctSvgHash.substring(
|
|
|
|
2,
|
|
|
|
4
|
|
|
|
)}/${correctSvgHash.substring(4)}.svg`;
|
2017-12-30 10:46:51 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
before(function (done) {
|
2017-07-06 15:33:09 +00:00
|
|
|
this.app = util.createWebserver();
|
2020-03-21 20:55:36 +00:00
|
|
|
this.app.get("/real-test-image.png", function (req, res) {
|
2017-07-06 15:33:09 +00:00
|
|
|
res.sendFile(testImagePath);
|
|
|
|
});
|
2020-03-21 20:55:36 +00:00
|
|
|
this.app.get("/logo.svg", function (req, res) {
|
2017-12-30 10:46:51 +00:00
|
|
|
res.sendFile(testSvgPath);
|
|
|
|
});
|
2019-10-04 11:19:04 +00:00
|
|
|
this.connection = this.app.listen(0, () => {
|
|
|
|
this.port = this.connection.address().port;
|
|
|
|
done();
|
|
|
|
});
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
after(function (done) {
|
2017-07-06 15:33:09 +00:00
|
|
|
this.connection.close(done);
|
|
|
|
});
|
|
|
|
|
2020-03-16 12:40:21 +00:00
|
|
|
after(function (done) {
|
|
|
|
// After storage tests run, remove the remaining empty
|
|
|
|
// storage folder so we return to the clean state
|
2022-05-01 19:12:39 +00:00
|
|
|
const dir = Config.getStoragePath();
|
2020-03-16 12:40:21 +00:00
|
|
|
fs.rmdir(dir, done);
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
beforeEach(function () {
|
2017-07-06 15:33:09 +00:00
|
|
|
this.irc = util.createClient();
|
|
|
|
this.network = util.createNetwork();
|
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
Config.values.prefetchStorage = true;
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
afterEach(function () {
|
2022-05-01 19:12:39 +00:00
|
|
|
Config.values.prefetchStorage = false;
|
2019-10-31 09:01:44 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should store the thumbnail", function (done) {
|
2019-10-04 11:19:04 +00:00
|
|
|
const port = this.port;
|
2017-07-06 15:33:09 +00:00
|
|
|
const message = this.irc.createMessage({
|
2019-10-04 11:19:04 +00:00
|
|
|
text: "http://localhost:" + port + "/thumb",
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|
|
|
|
|
2020-07-22 15:28:12 +00:00
|
|
|
link(this.irc, this.network.channels[0], message, message.text);
|
2017-07-06 15:33:09 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
this.app.get("/thumb", function (req, res) {
|
2019-07-17 09:33:59 +00:00
|
|
|
res.send(
|
2019-10-04 11:19:04 +00:00
|
|
|
"<title>Google</title><meta property='og:image' content='http://localhost:" +
|
|
|
|
port +
|
|
|
|
"/real-test-image.png'>"
|
2019-07-17 09:33:59 +00:00
|
|
|
);
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
this.irc.once("msg:preview", function (data) {
|
2017-07-06 15:33:09 +00:00
|
|
|
expect(data.preview.head).to.equal("Google");
|
2019-10-04 11:19:04 +00:00
|
|
|
expect(data.preview.link).to.equal("http://localhost:" + port + "/thumb");
|
2017-07-06 15:33:09 +00:00
|
|
|
expect(data.preview.thumb).to.equal(correctImageURL);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should store the image", function (done) {
|
2019-10-04 11:19:04 +00:00
|
|
|
const port = this.port;
|
2017-07-06 15:33:09 +00:00
|
|
|
const message = this.irc.createMessage({
|
2019-10-04 11:19:04 +00:00
|
|
|
text: "http://localhost:" + port + "/real-test-image.png",
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|
|
|
|
|
2020-07-22 15:28:12 +00:00
|
|
|
link(this.irc, this.network.channels[0], message, message.text);
|
2017-07-06 15:33:09 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
this.irc.once("msg:preview", function (data) {
|
2017-07-06 15:33:09 +00:00
|
|
|
expect(data.preview.type).to.equal("image");
|
2019-10-04 11:19:04 +00:00
|
|
|
expect(data.preview.link).to.equal("http://localhost:" + port + "/real-test-image.png");
|
2017-07-06 15:33:09 +00:00
|
|
|
expect(data.preview.thumb).to.equal(correctImageURL);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-12-30 10:46:51 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should lookup correct extension type", function (done) {
|
2019-10-04 11:19:04 +00:00
|
|
|
const port = this.port;
|
2017-12-30 10:46:51 +00:00
|
|
|
const message = this.irc.createMessage({
|
2019-10-04 11:19:04 +00:00
|
|
|
text: "http://localhost:" + port + "/svg-preview",
|
2017-12-30 10:46:51 +00:00
|
|
|
});
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
this.app.get("/svg-preview", function (req: Request, res: Response) {
|
2019-07-17 09:33:59 +00:00
|
|
|
res.send(
|
2019-10-04 11:19:04 +00:00
|
|
|
"<title>test title</title><meta property='og:image' content='http://localhost:" +
|
|
|
|
port +
|
|
|
|
"/logo.svg'>"
|
2019-07-17 09:33:59 +00:00
|
|
|
);
|
2017-12-30 10:46:51 +00:00
|
|
|
});
|
|
|
|
|
2020-07-22 15:28:12 +00:00
|
|
|
link(this.irc, this.network.channels[0], message, message.text);
|
2017-12-30 10:46:51 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
this.irc.once("msg:preview", function (data) {
|
2017-12-30 10:46:51 +00:00
|
|
|
expect(data.preview.type).to.equal("link");
|
2019-10-04 11:19:04 +00:00
|
|
|
expect(data.preview.link).to.equal("http://localhost:" + port + "/svg-preview");
|
2017-12-30 10:46:51 +00:00
|
|
|
expect(data.preview.thumb).to.equal(correctSvgURL);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2020-03-16 12:40:21 +00:00
|
|
|
|
|
|
|
it("should clear storage folder", function () {
|
2022-05-01 19:12:39 +00:00
|
|
|
const dir = Config.getStoragePath();
|
2020-03-16 12:40:21 +00:00
|
|
|
|
|
|
|
expect(fs.readdirSync(dir)).to.not.be.empty;
|
|
|
|
storage.emptyDir();
|
|
|
|
expect(fs.readdirSync(dir)).to.be.empty;
|
|
|
|
expect(fs.existsSync(dir)).to.be.true;
|
|
|
|
});
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|