port readFileStream

This commit is contained in:
May 2023-11-02 17:35:02 +00:00
parent 107709a98a
commit 739f5b4e3f
2 changed files with 10 additions and 6 deletions

View file

@ -7,8 +7,6 @@ import { getAccount } from "./lib/middleware"
import APIRouter from "./routes/api" import APIRouter from "./routes/api"
import preview from "./routes/preview" import preview from "./routes/preview"
require("dotenv").config()
const pkg = require(`${process.cwd()}/package.json`) const pkg = require(`${process.cwd()}/package.json`)
const app = new Hono() const app = new Hono()
let config = require(`${process.cwd()}/config.json`) let config = require(`${process.cwd()}/config.json`)

View file

@ -4,6 +4,8 @@ import { readFile, writeFile } from "node:fs/promises"
import { Readable } from "node:stream" import { Readable } from "node:stream"
import crypto from "node:crypto" import crypto from "node:crypto"
import { files } from "./accounts" import { files } from "./accounts"
import * as API from "./DiscordAPI"
import type {APIAttachment} from "discord-api-types/v10"
import * as Accounts from "./accounts" import * as Accounts from "./accounts"
@ -12,6 +14,8 @@ export let alphanum = Array.from(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
) )
require("dotenv").config()
// bad solution but whatever // bad solution but whatever
export type FileVisibility = "public" | "anonymous" | "private" export type FileVisibility = "public" | "anonymous" | "private"
@ -71,11 +75,13 @@ export interface StatusCodeError {
export default class Files { export default class Files {
config: Configuration config: Configuration
client: Client client: Client
api: API.Client
files: { [key: string]: FilePointer } = {} files: { [key: string]: FilePointer } = {}
uploadChannel?: TextBasedChannel uploadChannel?: TextBasedChannel
constructor(config: Configuration) { constructor(config: Configuration) {
this.config = config this.config = config
this.api = new API.Client(process.env.TOKEN!, config.targetChannel)
this.client = new Client({ this.client = new Client({
intents: [ intents: [
IntentsBitField.Flags.GuildMessages, IntentsBitField.Flags.GuildMessages,
@ -290,7 +296,7 @@ export default class Files {
* @description Read a file * @description Read a file
* @param uploadId Target file's ID * @param uploadId Target file's ID
* @param range Byte range to get * @param range Byte range to get
* @returns A `Readable` containing the file's contents * @returns A {@link Readable} containing the file's contents
*/ */
async readFileStream( async readFileStream(
uploadId: string, uploadId: string,
@ -325,7 +331,7 @@ export default class Files {
scan_msg_end = Math.ceil(scan_files_end / 10) scan_msg_end = Math.ceil(scan_files_end / 10)
} }
let attachments: Discord.Attachment[] = [] let attachments: APIAttachment[] = []
/* File updates */ /* File updates */
let file_updates: Pick<FilePointer, "chunkSize" | "sizeInBytes"> = let file_updates: Pick<FilePointer, "chunkSize" | "sizeInBytes"> =
@ -333,8 +339,8 @@ export default class Files {
let atSIB: number[] = [] // kepes track of the size of each file... let atSIB: number[] = [] // kepes track of the size of each file...
for (let xi = scan_msg_begin; xi < scan_msg_end + 1; xi++) { for (let xi = scan_msg_begin; xi < scan_msg_end + 1; xi++) {
let msg = await this.uploadChannel.messages let msg = await this.api
.fetch(file.messageids[xi]) .fetchMessage(file.messageids[xi])
.catch(() => { .catch(() => {
return null return null
}) })