From f4e631894c1be0c201dd4fad27f031d29e0bdfd0 Mon Sep 17 00:00:00 2001 From: Pierce Date: Mon, 31 Jul 2023 15:12:43 -0700 Subject: [PATCH] Added media.util and downloadSingle --- src/index.ts | 2 +- src/lib/Content.ts | 4 +-- src/lib/Download.ts | 5 ++-- src/lib/Media.ts | 31 +++++++++++++++++++--- src/lib/MediaCategory.ts | 2 +- src/lib/Profile.ts | 2 +- src/lib/utility/{batcher.ts => helpers.ts} | 0 7 files changed, 35 insertions(+), 11 deletions(-) rename src/lib/utility/{batcher.ts => helpers.ts} (100%) diff --git a/src/index.ts b/src/index.ts index 6aa2f95..44ef433 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ export { Client } from './lib/Client.js' export * as Types from './lib/utility/Global.js' -export { batcher, chunk } from './lib/utility/batcher.js' \ No newline at end of file +export { batcher, chunk } from './lib/utility/helpers.js' \ No newline at end of file diff --git a/src/lib/Content.ts b/src/lib/Content.ts index 28fa3e5..30f17b9 100644 --- a/src/lib/Content.ts +++ b/src/lib/Content.ts @@ -10,12 +10,12 @@ export class Content { async getVersions(contentId: number, language: string): Promise { const response = await this.client.call('GET', `${ContentEndpoint}/${contentId}/${language}/version`, null) - return response?.ok ? await response.json() : null + return await response.json() as ContentDTO[] } async get(contentId: number, language: string, sectionId: number): Promise { const response = await this.client.call('GET', `${ContentEndpoint}/${sectionId}/${contentId}/${language}`, null) - return response?.ok ? await response.json() : null + return await response.json() as ContentDTO } async getWithoutSection(contentId: number, language: string, version?: string) { diff --git a/src/lib/Download.ts b/src/lib/Download.ts index 6533d46..a4a4b36 100644 --- a/src/lib/Download.ts +++ b/src/lib/Download.ts @@ -1,7 +1,6 @@ import { Client } from "./Client" import { FileDownload } from './utility/Global.js' - export const DownloadEndpoint = 'download' export class Download { private client: Client @@ -11,12 +10,12 @@ export class Download { async getFileFromElement(element: string, contentId: number, language: string): Promise { const response = await this.client.call('GET', `${DownloadEndpoint}/${contentId}/${language}/${element}`, null) - return response?.ok ? await response.json() : null + return await response.json() as FileDownload } async getFileFromElementVersion(element: string, contentId: number, language: string, version: number): Promise { const response = await this.client.call('GET', `${DownloadEndpoint}/${contentId}/${language}/${version}/${element}`, null) - return response?.ok ? await response.json() : null + return await response.json() as FileDownload } } diff --git a/src/lib/Media.ts b/src/lib/Media.ts index 85834ff..92bffc7 100644 --- a/src/lib/Media.ts +++ b/src/lib/Media.ts @@ -1,14 +1,30 @@ -import { Client } from "./Client" -import { MediaData, MediaItemTableData, MediaUpload, MediaUploadData } from "./utility/Global.js" +import { Client } from './Client.js' +import { Category, MediaData, MediaItemTableData, MediaRow, MediaUpload, MediaUploadData } from './utility/Global.js' import { existsSync, readFileSync } from 'node:fs' +import { batcher } from './utility/helpers.js' import * as path from 'path' - export const MediaEndpoint = 'media' export class Media { client: Client + util: { getMediaIDs: (parentID: number, arrLimit?: number, reqTimeout?: number) => Promise } constructor(client:Client) { this.client = client + this.util = { + getMediaIDs: async (parentID: number, arrLimit: number = 50, reqTimeout: number = 10000) => { + const structure = await this.client.mediaCategory.list(parentID, 'en') + let categoryIds: number[] = [] + const populateIds = (category: Category) => { + if (category?.id) categoryIds.push(category.id) + if (category?.children?.length) category.children.map(cat => populateIds(cat)) + } + populateIds(structure[0]) + const list = async (id: number) => await client.media.list(id, 'en') + const categories = await batcher(categoryIds, arrLimit, reqTimeout, list) + const IdList = categories.map(category => category.mediaRows.map((row: MediaRow) => row.id)) + return IdList.flat(Infinity) + } + } } async add(data: MediaUpload) { @@ -48,4 +64,13 @@ export class Media { }) return await response.json() } + + async downloadSingle(id: number, type: 'media' | 'file' | 'thumbnail', version?: string) { + if (!version) version = (await this.client.content.getVersions(id, 'smxx'))[0].version + const inital = await this.client.call('GET', `${MediaEndpoint}/${id}/smxx/${version}/${type}`, { redirect: 'manual' }) + if (inital.status != 302) return null + const fileURL = await inital.text() + const response = await fetch(fileURL) + return await response.arrayBuffer() + } } \ No newline at end of file diff --git a/src/lib/MediaCategory.ts b/src/lib/MediaCategory.ts index 6178cfd..68e617f 100644 --- a/src/lib/MediaCategory.ts +++ b/src/lib/MediaCategory.ts @@ -20,6 +20,6 @@ export class MediaCategory { "showInactive": true, "showMyMedia": false }}) - return await response.json() + return await response.json() as Category[] } } diff --git a/src/lib/Profile.ts b/src/lib/Profile.ts index b548ce3..2461c98 100644 --- a/src/lib/Profile.ts +++ b/src/lib/Profile.ts @@ -10,7 +10,7 @@ export class Profile { async get(): Promise { const response = await this.client.call('GET', ProfileEndpoint, null) - return response?.ok ? await response.json() : null + return await response.json() as UserProfileView } async update(body: Partial) { diff --git a/src/lib/utility/batcher.ts b/src/lib/utility/helpers.ts similarity index 100% rename from src/lib/utility/batcher.ts rename to src/lib/utility/helpers.ts