Skip to content

Commit

Permalink
Added media.util and downloadSingle
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierce01 committed Jul 31, 2023
1 parent 334622b commit f4e6318
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
export { batcher, chunk } from './lib/utility/helpers.js'
4 changes: 2 additions & 2 deletions src/lib/Content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export class Content {

async getVersions(contentId: number, language: string): Promise<ContentDTO[]> {
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<ContentDTO> {
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) {
Expand Down
5 changes: 2 additions & 3 deletions src/lib/Download.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Client } from "./Client"
import { FileDownload } from './utility/Global.js'


export const DownloadEndpoint = 'download'
export class Download {
private client: Client
Expand All @@ -11,12 +10,12 @@ export class Download {

async getFileFromElement(element: string, contentId: number, language: string): Promise<FileDownload> {
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<FileDownload> {
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
}
}

31 changes: 28 additions & 3 deletions src/lib/Media.ts
Original file line number Diff line number Diff line change
@@ -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<number[]> }
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) {
Expand Down Expand Up @@ -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()
}
}
2 changes: 1 addition & 1 deletion src/lib/MediaCategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export class MediaCategory {
"showInactive": true,
"showMyMedia": false
}})
return await response.json()
return await response.json() as Category[]
}
}
2 changes: 1 addition & 1 deletion src/lib/Profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Profile {

async get(): Promise<UserProfileView> {
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<UserProfileView>) {
Expand Down
File renamed without changes.

0 comments on commit f4e6318

Please sign in to comment.