Skip to content

Commit

Permalink
optimize version downloading for #17
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Sep 13, 2024
1 parent 122113c commit 6ea52f7
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/events/ready.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const DEV = process.env.DEV.toLowerCase() == "true";
const MAINTENANCE = process.env.MAINTENANCE.toLowerCase() == "true";
const DEV = process.env.DEV.toLowerCase() === "true";
const MAINTENANCE = process.env.MAINTENANCE.toLowerCase() === "true";

import { loadCommands } from "@functions/commandHandler";
import { fetchSettings } from "@functions/fetchSettings";
Expand Down
2 changes: 1 addition & 1 deletion src/functions/fetchSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join } from "path";
import axios from "axios";

const FETCH_SETTINGS = process.env.FETCH_SETTINGS.toLowerCase() === "true";
const DEV = process.env.DEV === "true";
const DEV = process.env.DEV.toLowerCase() === "true";

/**
* Download JSON files for local usage
Expand Down
2 changes: 1 addition & 1 deletion src/functions/handleError.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import devLogger from "@helpers/devLogger";
import { Client, DiscordAPIError } from "discord.js";

const DEV = process.env.DEV.toLowerCase() == "true";
const DEV = process.env.DEV.toLowerCase() === "true";

/**
* Handle and log errors
Expand Down
24 changes: 11 additions & 13 deletions src/functions/images/difference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ export async function difference(
secondUrl: string,
tolerance = 0,
): Promise<AttachmentBuilder> {
const first = await mapURL(firstUrl);
const second = await mapURL(secondUrl);
const images = [first, second];

// could not be loaded
if (images.some((i) => !i)) return null;
let images: MappedURL[];
try {
images = await Promise.all([mapURL(firstUrl), mapURL(secondUrl)]);
} catch {
// could not be loaded
return null;
}

// take biggest dimensions regardless of final image size
const finalWidth = Math.max(...images.map((i) => i.width));
Expand All @@ -60,7 +61,7 @@ export async function difference(
for (let i = 0; i < bufferLength; i += 4) {
const x = (i / 4) % finalWidth;
const y = Math.floor(i / 4 / finalWidth);
const type = diffPixel(first, second, x, y, tolerance);
const type = diffPixel(images[0], images[1], x, y, tolerance);

// out of bounds, skip
if (!type) continue;
Expand All @@ -77,8 +78,8 @@ export async function difference(
continue;
default:
// set to original pixel
data.set(first.pixels.slice(i, i + 3), i);
data[i + 3] = first.pixels[i + 3] * TRANSPARENCY_FACTOR;
data.set(images[0].pixels.slice(i, i + 3), i);
data[i + 3] = images[0].pixels[i + 3] * TRANSPARENCY_FACTOR;
}
}

Expand Down Expand Up @@ -155,10 +156,7 @@ function diffPixel(
* @returns Mapped URL
*/
async function mapURL(url: string): Promise<MappedURL> {
const res = await magnify(url).catch(() => null);

// null values are handled outside
if (!res) return null;
const res = await magnify(url);

// we can only destructure after null check
const { magnified, width, height } = res;
Expand Down
4 changes: 2 additions & 2 deletions src/functions/saveDB.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const DEV = process.env.DEV.toLowerCase() == "true";
const DEBUG = process.env.DEBUG.toLowerCase() == "true";
const DEV = process.env.DEV.toLowerCase() === "true";
const DEBUG = process.env.DEBUG.toLowerCase() === "true";

import pushToGitHub from "@functions/pushToGitHub";
import { join } from "path";
Expand Down
27 changes: 14 additions & 13 deletions src/functions/submission/handleResults.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import settings from "@resources/settings.json";
const DEBUG = process.env.DEBUG.toLowerCase() == "true";
const DEBUG = process.env.DEBUG.toLowerCase() === "true";

import getMessages from "@helpers/getMessages";
import getPackByChannel from "@submission/utility/getPackByChannel";
import handleError from "@functions/handleError";

import { mkdirSync, writeFile } from "fs";
import axios from "axios";
import type { Contribution, Pack, PackFile, Texture } from "@interfaces/database";
import { TextChannel, Client, Message } from "discord.js";
import { join, sep } from "path";
import { mkdir, writeFile } from "fs/promises";

export interface DownloadableMessage {
url: string;
Expand Down Expand Up @@ -123,17 +123,18 @@ export async function downloadTexture(

for (const path of paths) {
// write file to every version of a path
for (const version of path.versions) {
const fullPath = join(baseFolder, packFolder, version, path.name);

// trim last bit to get folder tree
mkdirSync(fullPath.slice(0, fullPath.lastIndexOf(sep)), { recursive: true });

// something is always being logged when debugging so the callback version is simpler
writeFile(fullPath, imageFile, (err) => {
if (DEBUG) return console.log(err ?? `Added texture to path: ${fullPath}`);
});
}
await Promise.all(
path.versions.map(async (version) => {
const fullPath = join(baseFolder, packFolder, version, path.name);
try {
await mkdir(fullPath.slice(0, fullPath.lastIndexOf(sep)), { recursive: true });
await writeFile(fullPath, imageFile);
if (DEBUG) console.log(`Added texture to path ${fullPath}`);
} catch (err) {
console.error(err);
}
}),
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/functions/submission/makeEmbed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import settings from "@resources/settings.json";
import strings from "@resources/strings.json";
const DEBUG = process.env.DEBUG.toLowerCase() == "true";
const DEBUG = process.env.DEBUG.toLowerCase() === "true";

import minecraftSorter from "@helpers/minecraftSorter";
import getPackByChannel from "@submission/utility/getPackByChannel";
Expand Down
8 changes: 4 additions & 4 deletions src/functions/submission/pushTextures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import formattedDate from "@helpers/formattedDate";
import pushToGitHub from "@functions/pushToGitHub";
import type { PackFile } from "@interfaces/database";
import { join } from "path";
const DEBUG = process.env.DEBUG.toLowerCase() == "true";

const DEBUG = process.env.DEBUG.toLowerCase() === "true";
const DEV = process.env.DEV.toLowerCase() === "true";
/**
* Push textures to all versions of a given pack
* @author Juknum, Evorp
Expand Down Expand Up @@ -47,6 +47,6 @@ export default async function pushTextures(
}
}

// delete the random hash folder as well so you don't end up with 5 morbillion empty folders
if (basePath.includes("instapass")) rmSync(basePath, { recursive: true });
// prevent 5 morbillion empty hash folders (disabled in dev for download testing)
if (basePath.includes("instapass") && !DEV) rmSync(basePath, { recursive: true });
}
2 changes: 1 addition & 1 deletion src/functions/submission/reactionMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { hasPermission } from "@helpers/permissions";
import type { PackFile } from "@interfaces/database";
import { GuildMember, Message, MessageReaction, User } from "discord.js";

const DEBUG = process.env.DEBUG.toLowerCase() == "true";
const DEBUG = process.env.DEBUG.toLowerCase() === "true";

/**
* Opens reaction tray, listens for reaction, and closes tray
Expand Down
2 changes: 1 addition & 1 deletion src/functions/submission/sendToChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { imageButtons, submissionReactions } from "@helpers/interactions";
import { Client, EmbedBuilder, MessageReaction, TextChannel } from "discord.js";
import type { Submission } from "@interfaces/database";

const DEBUG = process.env.DEBUG.toLowerCase() == "true";
const DEBUG = process.env.DEBUG.toLowerCase() === "true";

/**
* Send textures to a new channel following council rules
Expand Down
2 changes: 1 addition & 1 deletion src/functions/submission/submitTexture.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import strings from "@resources/strings.json";

const DEBUG = process.env.DEBUG.toLowerCase() == "true";
const DEBUG = process.env.DEBUG.toLowerCase() === "true";

import choiceEmbed from "@submission/utility/choiceEmbed";
import makeEmbed from "@submission/makeEmbed";
Expand Down
2 changes: 1 addition & 1 deletion src/functions/submission/utility/cancelSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import addDeleteButton from "@helpers/addDeleteButton";
import { EmbedBuilder, Message } from "discord.js";
import { hasPermission } from "@helpers/permissions";

const DEBUG = process.env.DEBUG.toLowerCase() == "true";
const DEBUG = process.env.DEBUG.toLowerCase() === "true";

/**
* Logic for handling an invalid submission
Expand Down
2 changes: 1 addition & 1 deletion src/functions/submission/utility/changeStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
TextChannel,
} from "discord.js";

const DEBUG = process.env.DEBUG.toLowerCase() == "true";
const DEBUG = process.env.DEBUG.toLowerCase() === "true";

export interface StatusParams {
status: string;
Expand Down
2 changes: 1 addition & 1 deletion src/functions/submission/utility/choiceEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { hasPermission } from "@helpers/permissions";
import axios from "axios";
import type { Texture } from "@interfaces/database";

const DEBUG = process.env.DEBUG.toLowerCase() == "true";
const DEBUG = process.env.DEBUG.toLowerCase() === "true";

/**
* Selection menu for dealing with multiple valid options
Expand Down
2 changes: 1 addition & 1 deletion src/functions/submission/utility/instapass.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import settings from "@resources/settings.json";

const DEBUG = process.env.DEBUG.toLowerCase() == "true";
const DEBUG = process.env.DEBUG.toLowerCase() === "true";
import { randomBytes } from "crypto";

import {
Expand Down
2 changes: 1 addition & 1 deletion src/functions/submission/utility/invalidate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import settings from "@resources/settings.json";

const DEBUG = process.env.DEBUG.toLowerCase() == "true";
const DEBUG = process.env.DEBUG.toLowerCase() === "true";

import changeStatus from "@submission/utility/changeStatus";
import { Message, User, GuildMember } from "discord.js";
Expand Down

0 comments on commit 6ea52f7

Please sign in to comment.