2020-08-26 19:12:43 -07:00
|
|
|
const beeline = require("honeycomb-beeline")({
|
|
|
|
writeKey: process.env["HONEYCOMB_WRITE_KEY"],
|
|
|
|
dataset:
|
|
|
|
process.env["NODE_ENV"] === "production"
|
|
|
|
? "Dress to Impress (2020)"
|
|
|
|
: "Dress to Impress (2020, dev)",
|
|
|
|
serviceName: "impress-2020-gql-server",
|
|
|
|
});
|
2020-08-04 18:39:31 -07:00
|
|
|
const AWS = require("aws-sdk");
|
|
|
|
const Jimp = require("jimp");
|
|
|
|
|
2021-11-01 22:25:43 -07:00
|
|
|
const connectToDb = require("../../src/server/db");
|
|
|
|
const buildLoaders = require("../../src/server/loaders");
|
2020-08-26 19:12:43 -07:00
|
|
|
const {
|
|
|
|
loadBodyName,
|
|
|
|
logToDiscord,
|
|
|
|
normalizeRow,
|
2021-11-01 22:25:43 -07:00
|
|
|
} = require("../../src/server/util");
|
2020-08-04 18:39:31 -07:00
|
|
|
|
2020-08-05 14:12:44 -07:00
|
|
|
if (
|
|
|
|
!process.env["DTI_AWS_ACCESS_KEY_ID"] ||
|
|
|
|
!process.env["DTI_AWS_SECRET_ACCESS_KEY"]
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
`must provide DTI_AWS_ACCESS_KEY_ID and DTI_AWS_SECRET_ACCESS_KEY ` +
|
Stop referencing the {color,species,zone}_translations tables
We're in the process of migrating away from translating these records,
because Neopets hasn't supported non-English languages in many years,
and it'll simplify our code and database lookups.
In Main DTI, we already wrote code to copy these fields onto the main
records and keep them in sync for now; now, once DTI 2020 isn't
referencing them anymore, it should be safe for the main app to drop
the tables altogether.
Note that some Prettier changes got mixed in here and that's fine!
I also wasn't suuuper careful testing these, most of them seem to be
trivially testable by just loading the homepage or doing a few basic
wardrobe actions, and the others are in Discord support log actions
that aren't enabled in development mode, so I'm just like… ehh I'll do
a couple support actions after deploy and see that they don't crash!
2024-02-03 08:05:15 -08:00
|
|
|
`environment variables`,
|
2020-08-05 14:12:44 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-04 18:39:31 -07:00
|
|
|
const s3 = new AWS.S3({
|
|
|
|
accessKeyId: process.env["DTI_AWS_ACCESS_KEY_ID"],
|
|
|
|
secretAccessKey: process.env["DTI_AWS_SECRET_ACCESS_KEY"],
|
|
|
|
});
|
|
|
|
|
|
|
|
async function upload(bucket, key, imageData) {
|
|
|
|
await s3
|
|
|
|
.putObject({
|
|
|
|
Bucket: bucket,
|
|
|
|
Key: key,
|
|
|
|
Body: imageData,
|
|
|
|
ContentType: "image/png",
|
|
|
|
ACL: "public-read",
|
|
|
|
})
|
|
|
|
.promise();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function resize(imageData, size) {
|
|
|
|
const image = await Jimp.read(imageData);
|
|
|
|
const resizedImage = image.resize(size, size);
|
|
|
|
const resizedImageData = await resizedImage.getBufferAsync("image/png");
|
|
|
|
return resizedImageData;
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:00:53 -07:00
|
|
|
async function processImage(assetType, remoteId, size, imageData) {
|
2020-08-04 18:39:31 -07:00
|
|
|
if (size !== 600) {
|
|
|
|
imageData = await resize(imageData, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
const paddedId = String(remoteId).padStart(12, "0");
|
|
|
|
const id1 = paddedId.slice(0, 3);
|
|
|
|
const id2 = paddedId.slice(3, 6);
|
|
|
|
const id3 = paddedId.slice(6, 9);
|
|
|
|
const key = `${assetType}/${id1}/${id2}/${id3}/${remoteId}/${size}x${size}.png`;
|
|
|
|
|
2020-08-05 14:03:14 -07:00
|
|
|
await upload("impress-asset-images", key, imageData);
|
2021-05-13 01:13:21 -07:00
|
|
|
console.info(`Successfully uploaded ${key} to impress-asset-images`);
|
2020-08-04 18:39:31 -07:00
|
|
|
}
|
|
|
|
|
2020-08-26 19:12:43 -07:00
|
|
|
async function handle(req, res) {
|
2020-08-04 18:39:31 -07:00
|
|
|
if (req.headers["dti-support-secret"] !== process.env["SUPPORT_SECRET"]) {
|
|
|
|
res.status(401).send(`Support secret is incorrect. Try setting up again?`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let imageData = Buffer.alloc(0);
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
req.on("data", (chunk) => {
|
|
|
|
imageData = Buffer.concat([imageData, chunk]);
|
|
|
|
});
|
|
|
|
req.on("end", () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const db = await connectToDb();
|
|
|
|
|
|
|
|
const { layerId } = req.query;
|
|
|
|
const [layerRows] = await db.execute(
|
|
|
|
`SELECT * FROM swf_assets WHERE id = ?`,
|
Stop referencing the {color,species,zone}_translations tables
We're in the process of migrating away from translating these records,
because Neopets hasn't supported non-English languages in many years,
and it'll simplify our code and database lookups.
In Main DTI, we already wrote code to copy these fields onto the main
records and keep them in sync for now; now, once DTI 2020 isn't
referencing them anymore, it should be safe for the main app to drop
the tables altogether.
Note that some Prettier changes got mixed in here and that's fine!
I also wasn't suuuper careful testing these, most of them seem to be
trivially testable by just loading the homepage or doing a few basic
wardrobe actions, and the others are in Discord support log actions
that aren't enabled in development mode, so I'm just like… ehh I'll do
a couple support actions after deploy and see that they don't crash!
2024-02-03 08:05:15 -08:00
|
|
|
[layerId],
|
2020-08-04 18:39:31 -07:00
|
|
|
);
|
2020-08-26 19:12:43 -07:00
|
|
|
const layer = normalizeRow(layerRows[0]);
|
2020-08-04 18:39:31 -07:00
|
|
|
if (!layer) {
|
|
|
|
res.status(404).send(`Layer not found`);
|
|
|
|
}
|
|
|
|
|
2020-08-26 19:12:43 -07:00
|
|
|
const { remoteId, type: assetType } = layer;
|
|
|
|
await Promise.all([
|
2020-08-05 14:00:53 -07:00
|
|
|
processImage(assetType, remoteId, 600, imageData),
|
|
|
|
processImage(assetType, remoteId, 300, imageData),
|
|
|
|
processImage(assetType, remoteId, 150, imageData),
|
2020-08-04 18:39:31 -07:00
|
|
|
]);
|
|
|
|
|
2020-08-05 14:00:53 -07:00
|
|
|
const now = new Date().toISOString().slice(0, 19).replace("T", " ");
|
2020-08-04 18:39:31 -07:00
|
|
|
const [result] = await db.execute(
|
|
|
|
`UPDATE swf_assets SET image_manual = 1, converted_at = ?
|
|
|
|
WHERE type = ? AND remote_id = ? LIMIT 1`,
|
Stop referencing the {color,species,zone}_translations tables
We're in the process of migrating away from translating these records,
because Neopets hasn't supported non-English languages in many years,
and it'll simplify our code and database lookups.
In Main DTI, we already wrote code to copy these fields onto the main
records and keep them in sync for now; now, once DTI 2020 isn't
referencing them anymore, it should be safe for the main app to drop
the tables altogether.
Note that some Prettier changes got mixed in here and that's fine!
I also wasn't suuuper careful testing these, most of them seem to be
trivially testable by just loading the homepage or doing a few basic
wardrobe actions, and the others are in Discord support log actions
that aren't enabled in development mode, so I'm just like… ehh I'll do
a couple support actions after deploy and see that they don't crash!
2024-02-03 08:05:15 -08:00
|
|
|
[now, assetType, remoteId],
|
2020-08-04 18:39:31 -07:00
|
|
|
);
|
|
|
|
if (result.affectedRows !== 1) {
|
|
|
|
res
|
|
|
|
.status(500)
|
|
|
|
.send(`expected 1 affected row but found ${result.affectedRows}`);
|
|
|
|
}
|
|
|
|
|
2020-08-26 19:12:43 -07:00
|
|
|
if (process.env["SUPPORT_TOOLS_DISCORD_WEBHOOK_URL"]) {
|
|
|
|
try {
|
Stop referencing the {color,species,zone}_translations tables
We're in the process of migrating away from translating these records,
because Neopets hasn't supported non-English languages in many years,
and it'll simplify our code and database lookups.
In Main DTI, we already wrote code to copy these fields onto the main
records and keep them in sync for now; now, once DTI 2020 isn't
referencing them anymore, it should be safe for the main app to drop
the tables altogether.
Note that some Prettier changes got mixed in here and that's fine!
I also wasn't suuuper careful testing these, most of them seem to be
trivially testable by just loading the homepage or doing a few basic
wardrobe actions, and the others are in Discord support log actions
that aren't enabled in development mode, so I'm just like… ehh I'll do
a couple support actions after deploy and see that they don't crash!
2024-02-03 08:05:15 -08:00
|
|
|
const { itemLoader, itemTranslationLoader, zoneLoader } =
|
|
|
|
buildLoaders(db);
|
2020-08-26 19:12:43 -07:00
|
|
|
|
|
|
|
// Copied from setLayerBodyId mutation
|
|
|
|
const itemId = await db
|
|
|
|
.execute(
|
|
|
|
`SELECT parent_id FROM parents_swf_assets
|
|
|
|
WHERE swf_asset_id = ? AND parent_type = "Item" LIMIT 1;`,
|
Stop referencing the {color,species,zone}_translations tables
We're in the process of migrating away from translating these records,
because Neopets hasn't supported non-English languages in many years,
and it'll simplify our code and database lookups.
In Main DTI, we already wrote code to copy these fields onto the main
records and keep them in sync for now; now, once DTI 2020 isn't
referencing them anymore, it should be safe for the main app to drop
the tables altogether.
Note that some Prettier changes got mixed in here and that's fine!
I also wasn't suuuper careful testing these, most of them seem to be
trivially testable by just loading the homepage or doing a few basic
wardrobe actions, and the others are in Discord support log actions
that aren't enabled in development mode, so I'm just like… ehh I'll do
a couple support actions after deploy and see that they don't crash!
2024-02-03 08:05:15 -08:00
|
|
|
[layerId],
|
2020-08-26 19:12:43 -07:00
|
|
|
)
|
|
|
|
.then(([rows]) => normalizeRow(rows[0]).parentId);
|
|
|
|
|
Stop referencing the {color,species,zone}_translations tables
We're in the process of migrating away from translating these records,
because Neopets hasn't supported non-English languages in many years,
and it'll simplify our code and database lookups.
In Main DTI, we already wrote code to copy these fields onto the main
records and keep them in sync for now; now, once DTI 2020 isn't
referencing them anymore, it should be safe for the main app to drop
the tables altogether.
Note that some Prettier changes got mixed in here and that's fine!
I also wasn't suuuper careful testing these, most of them seem to be
trivially testable by just loading the homepage or doing a few basic
wardrobe actions, and the others are in Discord support log actions
that aren't enabled in development mode, so I'm just like… ehh I'll do
a couple support actions after deploy and see that they don't crash!
2024-02-03 08:05:15 -08:00
|
|
|
const [item, itemTranslation, zone, bodyName] = await Promise.all([
|
2020-08-26 19:12:43 -07:00
|
|
|
itemLoader.load(itemId),
|
|
|
|
itemTranslationLoader.load(itemId),
|
Stop referencing the {color,species,zone}_translations tables
We're in the process of migrating away from translating these records,
because Neopets hasn't supported non-English languages in many years,
and it'll simplify our code and database lookups.
In Main DTI, we already wrote code to copy these fields onto the main
records and keep them in sync for now; now, once DTI 2020 isn't
referencing them anymore, it should be safe for the main app to drop
the tables altogether.
Note that some Prettier changes got mixed in here and that's fine!
I also wasn't suuuper careful testing these, most of them seem to be
trivially testable by just loading the homepage or doing a few basic
wardrobe actions, and the others are in Discord support log actions
that aren't enabled in development mode, so I'm just like… ehh I'll do
a couple support actions after deploy and see that they don't crash!
2024-02-03 08:05:15 -08:00
|
|
|
zoneLoader.load(layer.zoneId),
|
2020-08-26 19:12:43 -07:00
|
|
|
loadBodyName(layer.bodyId, db),
|
|
|
|
]);
|
|
|
|
|
|
|
|
await logToDiscord({
|
|
|
|
embeds: [
|
|
|
|
{
|
|
|
|
title: `🛠 ${itemTranslation.name}`,
|
|
|
|
thumbnail: {
|
|
|
|
url: item.thumbnailUrl,
|
|
|
|
height: 80,
|
|
|
|
width: 80,
|
|
|
|
},
|
|
|
|
fields: [
|
|
|
|
{
|
Stop referencing the {color,species,zone}_translations tables
We're in the process of migrating away from translating these records,
because Neopets hasn't supported non-English languages in many years,
and it'll simplify our code and database lookups.
In Main DTI, we already wrote code to copy these fields onto the main
records and keep them in sync for now; now, once DTI 2020 isn't
referencing them anymore, it should be safe for the main app to drop
the tables altogether.
Note that some Prettier changes got mixed in here and that's fine!
I also wasn't suuuper careful testing these, most of them seem to be
trivially testable by just loading the homepage or doing a few basic
wardrobe actions, and the others are in Discord support log actions
that aren't enabled in development mode, so I'm just like… ehh I'll do
a couple support actions after deploy and see that they don't crash!
2024-02-03 08:05:15 -08:00
|
|
|
name: `Layer ${layerId} (${zone.label})`,
|
2020-08-26 19:12:43 -07:00
|
|
|
value: `🎨 Uploaded new PNG for ${bodyName}`,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
url: `https://impress.openneo.net/items/${itemId}`,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error sending Discord support log", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res.status(200).send();
|
|
|
|
}
|
|
|
|
|
2021-05-13 01:13:21 -07:00
|
|
|
async function handleWithBeeline(req, res) {
|
2021-04-23 12:31:41 -07:00
|
|
|
beeline.withTrace(
|
|
|
|
{ name: "api/uploadLayerImage", operation_name: "api/uploadLayerImage" },
|
Stop referencing the {color,species,zone}_translations tables
We're in the process of migrating away from translating these records,
because Neopets hasn't supported non-English languages in many years,
and it'll simplify our code and database lookups.
In Main DTI, we already wrote code to copy these fields onto the main
records and keep them in sync for now; now, once DTI 2020 isn't
referencing them anymore, it should be safe for the main app to drop
the tables altogether.
Note that some Prettier changes got mixed in here and that's fine!
I also wasn't suuuper careful testing these, most of them seem to be
trivially testable by just loading the homepage or doing a few basic
wardrobe actions, and the others are in Discord support log actions
that aren't enabled in development mode, so I'm just like… ehh I'll do
a couple support actions after deploy and see that they don't crash!
2024-02-03 08:05:15 -08:00
|
|
|
() => handle(req, res),
|
2021-04-23 12:31:41 -07:00
|
|
|
);
|
2021-05-13 01:13:21 -07:00
|
|
|
}
|
|
|
|
|
2022-09-24 23:10:34 -07:00
|
|
|
export const config = {
|
|
|
|
api: {
|
|
|
|
bodyParser: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-05-13 01:13:21 -07:00
|
|
|
export default handleWithBeeline;
|