-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
151 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Attachment, Collection, Snowflake } from "discord.js"; | ||
|
||
import { Writable } from "node:stream"; | ||
import { createWriteStream } from "fs-extra"; | ||
import { log } from "./log"; | ||
import { resolve as resolvePath } from "node:path"; | ||
|
||
const EXTENSION_PATTERN = /([^./]+)$/; | ||
|
||
export class AttachmentStore { | ||
private readonly attachments = new Map<Snowflake, string>(); | ||
private readonly basePath: string; | ||
|
||
constructor(basePath: string) { | ||
this.basePath = basePath; | ||
} | ||
|
||
private getExtension(url: URL): string { | ||
const match = url.pathname.match(EXTENSION_PATTERN); | ||
|
||
if (match === null) { | ||
log.attachments("Failed to find extension for attachment '%s'.", url); | ||
return ""; | ||
} | ||
|
||
return "." + match[1]; | ||
} | ||
|
||
async dump(shouldDumpAttachments: boolean): Promise<void> { | ||
if (!shouldDumpAttachments) { | ||
log.attachments("Not dumping %d attachments.", this.attachments.size); | ||
return; | ||
} | ||
|
||
log.attachments("Dumping %d attachments.", this.attachments.size); | ||
|
||
for (const [id, urlString] of this.attachments) { | ||
const url = new URL(urlString); | ||
|
||
const extension = this.getExtension(url); | ||
const path = resolvePath(this.basePath, `${id}${extension}`); | ||
|
||
const response = await fetch(url); | ||
|
||
if (response.body) { | ||
const fileStream = createWriteStream(path); | ||
response.body.pipeTo(Writable.toWeb(fileStream)); | ||
} | ||
|
||
log.attachments("Dumped attachment '%s' to '%s'.", urlString, path); | ||
} | ||
|
||
return; | ||
} | ||
|
||
private add(id: Snowflake, attachment: Attachment): void { | ||
this.attachments.set(id, attachment.proxyURL); | ||
} | ||
|
||
addAll(attachments: Collection<Snowflake, Attachment>): void { | ||
for (const [id, attachment] of attachments) { | ||
this.add(id, attachment); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { BaseChannel, GuildChannel, ThreadChannel } from "discord.js"; | ||
|
||
import { resolve } from "node:path"; | ||
|
||
/** | ||
* The timestamp used in part of the dump's path. | ||
*/ | ||
const dumpDate = Date.now().toString(); | ||
|
||
function getGuildIdPart(channel: BaseChannel | null): string { | ||
if (channel instanceof GuildChannel || channel instanceof ThreadChannel) { | ||
return channel.guild.id; | ||
} | ||
|
||
return "guildless"; | ||
} | ||
|
||
export class GuildPaths { | ||
private readonly channel: BaseChannel | null; | ||
private readonly base: string; | ||
|
||
constructor(channel: BaseChannel | null) { | ||
this.channel = channel; | ||
this.base = resolve(`./dumps/${getGuildIdPart(channel)}/${dumpDate}`); | ||
} | ||
|
||
private resolve(...segments: string[]) { | ||
return resolve(this.base, ...segments); | ||
} | ||
|
||
getAttachments(): string { | ||
return this.resolve("./attachments"); | ||
} | ||
|
||
getChannel(): string { | ||
if (this.channel === null) { | ||
throw new Error("Cannot get channel path in non-channel context"); | ||
} | ||
|
||
return this.resolve(`./channels/${this.channel.id}.txt`); | ||
} | ||
|
||
getMemberHierarchy(): string { | ||
return this.resolve("./member_hierarchy.txt"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import debug from "debug"; | ||
|
||
export const log = { | ||
attachments: debug("discord-dumper:attachments"), | ||
dumper: debug("discord-dumper:dumper"), | ||
path: debug("discord-dumper:path"), | ||
prepare: debug("discord-dumper:prepare"), | ||
}; |